Create a new validator
Just override _isValid
To create a new validator, inherit from the Base validator, or one of the bundled validators, and override the _isValid
method. Note that _isValid
must return a Result object.
Javascript is not a strictly object oriented language. However Mystique gives the Base object class like behavior. To subclass Base, pass the extend
function and object to be mixed into the Base prototype.
define([ 'mystique/Base', 'mystique/Result' ], function( Base, Result ){ // module: // mystique/Alpha return Base.extend({ regex: /^[a-zA-Z]+$/, _isValid: function(value){ var result = new Result({value: true}); if ( ! this.regex.test(value)){ result.set('value', false); result.addMessage('alpha'); } return result; } }); });
Javascript is not a strictly object oriented language. However Mystique gives the Base object class like behavior. To subclass Base, pass the extend
function and object to be mixed into the Base prototype.
var Alpha = mystique.Base.extend({ regex: /^[a-zA-Z]+$/, _isValid: function(value){ var result = new mystique.Result({value: true}); if ( ! this.regex.test(value)){ result.set('value', false); result.addMessage('alpha'); } return result; } });
namespace Zoop\Mystique; class Alpha extends Base { protected $regex = '/^[a-zA-Z]+$/'; protected function _isValid($value){ $result = new Result(["value" => true]); if ( ! preg_match($this->regex, $value)){ $result->setValue(false); $result->addMessage('alpha'); } return $result; } }
Unit Testing
One set of test data to rule them all
When creating validators it's a really really good idea to write unit tests for them. All bundled validators use a common set of json formatted test data located in mystique/common/testdata
. Contributions and improvements to this test data are much appreciated.
Javascript
The javascript test suite is located under mystique/js/mystique/test
and uses the dojo's doh unit testing framework. If you have dojo and dojo/utils installed, then you can run the tests with runTests.html
.
PHP
The PHP test suite is located under mystique/php/test
and uses phpUnit.