dev-master
9999999-devAdds jquery.validation functionality to silverstripe 3.0+ forms.
The Requires
by Bumbus@Arillo
Wallogit.com
2017 © Pedro Pelรกez
Adds jquery.validation functionality to silverstripe 3.0+ forms.
Adds jquery.validation functionality to silverstripe 3.0+ forms. Visit http://jqueryvalidation.org/ for more information., (*1)
To make it work, make sure your page has jquery included. For sake of backward compartibilty for old browsers you also might load json2.js. Both files can be found in modulefolder/javascript/libs., (*2)
Start up validation on a form like this:, (*3)
$form = new Form(
$this,
'Form',
$fields,
new FieldList(
new FormAction(
'FormHandler',
'Submit'
)
),
RequiredFields::create(....)
);
Expects a form and an optional config array, which overrides values from the base config. Take a look into JQueryValidation::$base_config for available key / value pairs., (*4)
JQueryValidation::create(
$form,
array(
'validator' => array(
'errorElement' => 'div'
)
...
..
.
)
);
For automated creation use:, (*5)
// This will inspect all form fields and add their validation methods.
// It also will add required checks provided by the form's RequiredFields.
JQueryValidation::create($form)->generate();
// It is also possible to provide custom error messages and behaviour through passing a config array like this:
JQueryValidation::create($form)->generate(array(
'messages' => array(
'CheckboxField_DATA' => array(
'required' => 'Custom message'
)
)
));
// Expected hooks are messages, rules, groups.
If you want to provide your own validation file you can use this:, (*6)
JQueryValidation::create($form)->custom('path/to/your.js');
// It is also possible to provide information about which additional js files should be loaded, like this:
JQueryValidation::create($form)->custom(
'path/to/your.js',
array(
'additionalMethods', // load additional-methods.min.js
'metadata', // load jquery.metadata.js
'moment', // load moment.min.js
'date' // load date.js
)
);
Standard:, (*7)
Additional methods:, (*8)
See http://jqueryvalidation.org/documentation/ for more info., (*9)
Some of this methods are automatically invoked if JQueryValidation->generate() is used. Extra functionality can be added by adding them into $custom parameter. The following example creates a field which validates for an url:, (*10)
// adding a simple textfield to the field list
$fields->push(
TextField::create(
'URL',
'Your website'
)
);
...
..
.
// later, when starting the validation you can add url validtition to this field like this:
JQueryValidation::create($form)
->generate(
array(
'messages' => array(
'URL' => array(
'url' => 'Please enter a valid url.' // add error message
)
),
'rules' => array(
'URL' => array(
'url' => true // add url validation
)
)
)
);
Adds jquery.validation functionality to silverstripe 3.0+ forms.