JS Vaildator

2 : API for JsvalidHelper

By Garth Braithwaite (Garth)
This helper generates JavaScript validation based on the model's validate array, and also offers a few extra features.
These features include:
Server: a Ajax script to validate one field server-side (this was originally designed to make sure a field is unique on the table ie username or email)
Confirm: to check that two or more fields are equal (designed for password fields or to confirm email accounts)
Custom required fields: if you don't want to use strictly the model's validate array or if you want to append a few fields that may not actually appear in the array

Currently the validator uses a JavaScript alert to let user know what changes need to be made, but I am going to make it optional to have the results returned in a div tag of your choice as a unordered list. Until I finish that this will get you started.

Please let me know if this helps anyone, or how I could improve it.
I thought an api of sorts would be useful:
JsvalidHelper::confirm($field,$confirms)
Compares two or more fields to ensure that the values are equal (used for password and email fields)

Parameters

string $field Name of the field that will be the standard of comparison

array $confirms Use the other field names as the keys in this array, and the values are the feedback.


JsvalidHelper::form($url = null, $name = null, $method = 'post')
Returns an opening form tag with the onSubmit function needed to execute the jsvalid's validation function

Parameters

string $url Upon successful validation data will be passed to this url

string $name You can include a name property in the tag if desired.

string $method Either Post or Get to send the data upon passing the validation.


JsvalidHelper::input($fieldName,$label,$fieldAtt = null)
Returns a label tag and a text input tag formatted to work with the rest of the helper

Parameters

string $fieldName The name of the field ('Model/field')

string $label The text to be displayed in the label tag

array $fieldAtt Html attributes for the input tag


JsvalidHelper::password($fieldName,$label,$fieldAtt = null)
Returns a label tag and a password input tag formatted to work with the rest of the helper

Parameters

string $fieldName The name of the field ('Model/field')

string $label The text to be displayed in the label tag

array $fieldAtt Html attributes for the input tag


JsvalidHelper::returnScript()
Returns the JavaScript code block that contains the validation function.
JsvalidHelper::server($field, $label, $url, $divClass = 'jsunique',$fieldAtt = null)
Returns a label tag, a text input tag, a button, a JavaScript function, and an empty div tag. The JavaScript function submits the value in the text input to the url via ajax and puts the results in the empty div tag.

Parameters

string $field Name of a field, like this "Modelname/fieldname"

string $label Text that will appear in the label field.

string $url The URL where the data will be sent to be checked.

string $divClass The CSS class for the empty div tag

array $htmlAttributes Array of HTML attributes for the input field.


JsvalidHelper::setModel ($model,$validate = null)
Defines what model will be used to get access to the validate and jsFeeback (optional) arrays from the model.

Parameters

string $model Name of the model that will be used to validate

array $validate An option array that of RegEx that can be used to validate form. If null the validate array from the model will be used.


JsvalidHelper::textarea($fieldName,$label,$fieldAtt = null)
Returns a textarea tag and a text input tag formatted to work with the rest of the helper

Parameters

string $fieldName The name of the field ('Model/field')

string $label The text to be displayed in the label tag

array $fieldAtt Html attributes for the input tag

Page 3: JsvalidHelper in Action