Introduction

Validator is the base class of all serverside-validation. It also controls inclusion of clientside-valiation (through Validator.js).

Usage

Validators are implemented as an argument to the Form constructor. You create a required fields validator like so. In this case, we’re creating a RequiredFields validator - Validator itself is an abstract class.

$validator = new RequiredFields("Email","FirstName");
return new Form($this, "FormName", $fields, $actions, $validator);

To create your own validator, you need to subclass validator and define two methods:

  • javascript() Should output a snippet of JavaScript that will get called to perform javascript validation.
  • php($data) Should return true if the given data is valid, and call $this→validationError() if there were any errors.

Disable JavaScript Validation

By default, SilverStripe forms with an attached Validator instance use the custom Validator.js clientside logic. It is quite hard to customize, and might not be appropriate for all use-cases. You can disable integrated clientside validation, and use your own (e.g. jquery.validate).

Disable for all forms (in mysite/_config.php):

Validator::set_javascript_validation_handler('none');

Disable for a specific form:

$myForm->getValidator()->setJavascriptValidationHandler('none');

Implementations

API Documentation

 
validator.txt · Last modified: 2009/08/24 10:10 by ischommer

Please use comments for notes, tips and corrections about the described functionality.
Use the Silverstripe Forum to ask questions.

blog comments powered by Disqus
 
Creative Commons LicenseSilverStripe Documentation by SilverStripe is licensed under a Creative Commons Attribution 3.0 New Zealand License.
Based on a work at doc.silverstripe.com
Recent changes RSS feed Driven by DokuWiki