namespace Flikore\Validator; require '../autoload.php'; use Flikore\Validator\Validators as v; // Create a set that can validate arrays (and objects too) $set = new ValidationSet(); // Add a single rule to a key $set->addRule('name', new v\NotEmptyValidator()); // This rule is chained with the previous one, both must be valid $set->addRule('name', new v\MinLengthValidator(5)); // Multiple rules can be added at once with an array // Those rules will be *added* to the "name" key, and will not exclude the others. $set->addRules('name', array(new v\MaxLengthValidator(30), new v\RegexValidator('/^[a-z ]+$/i'))); // Just call validate (or assert) to check if it's ok var_dump($set->validate(array('name' => 'Cool Name'))); // bool(true) var_dump($set->validate(array('name' => 'aaa'))); // bool(false) The minimum length is 5 var_dump($set->validate(array('name' => 'aa4a5'))); // bool(false) Doesn't match regex var_dump($set->validate(array('name' => ''))); // bool(false) Can't be empty // Another way is to construct the set with all the rules: // ***Note: Even in this way, new rules can also be added later with the add methods. $set = new ValidationSet(array('name' => array(new v\NotEmptyValidator(), new v\MinLengthValidator(5)), 'age' => new v\MinValueValidator(13))); var_dump($set->validate(array('name' => 'this is ok', 'age' => 14))); // bool(true) var_dump($set->validate(array('name' => 'oops', 'age' => 14))); // bool(false) var_dump($set->validate(array('name' => 'the age is not good', 'age' => 12))); // bool(false)
* * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ namespace Flikore\Validator; require_once '../autoload.php'; use Flikore\Validator\Validators as v; // To validate arrays inside arrays, the validation sets can be nested. $v = new ValidationSet(); // You may add a ValidationSet as a rule to some field. // Here, let's create a set to validate the user name and email $inner = new ValidationSet(); // And add the rules $inner->addRule('name', new v\AlphaValidator()); $inner->addRule('email', new v\EmailValidator()); // Then, use this as the rule for the main set. $v->addRule('user', $inner); // Now, take this array: $value = array('user' => array('name' => 'Ok name', 'email' => '*****@*****.**')); // And validate it var_dump($v->validate($value)); //bool(true)
* The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ namespace Flikore\Validator; require '../autoload.php'; use Flikore\Validator\Validators as v; // Create a set of rules. $set = new ValidationSet(array('name' => array(new v\NotEmptyValidator(), new v\MinLengthValidator(5)), 'age' => new v\MinValueValidator(13), 'email' => new v\EmailValidator())); // Creating a value $value = array('name' => 'this is ok', 'age' => 12, 'email' => '*****@*****.**', 'no_rule' => 'whatever'); // Validates only the name, so it's ok var_dump($set->validate($value, 'name')); // bool(true) // Validates the name and email var_dump($set->validate($value, array('name', 'email'))); // bool(true) // Validates the no_rule var_dump($set->validate($value, 'no_rule')); // bool(true) // Validates the name and age, so there's error var_dump($set->validate($value, array('name', 'age'))); // bool(false)
* * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ namespace Flikore\Validator; require '../autoload.php'; use Flikore\Validator\Validators as v; // This only works with a set, because it's the only way // to have other keys to compare with $set = new ValidationSet(); // Add some rule $set->addRule('key1', new ValidationValue(new v\EqualsValidator('dummy'), new ValidationKey('key2'), true)); // end addRule) // Equal values $ok = array('key1' => 'equal', 'key2' => 'equal'); var_dump($set->validate($ok)); // bool(true) // Not equal values $notOk = array('key1' => 'equal', 'key2' => 'not equal'); var_dump($set->validate($notOk)); // bool(false) // Not strictly equal values $notStrict = array('key1' => 5, 'key2' => '5'); var_dump($set->validate($notStrict)); // bool(false)
// Create the object $t = new Tested(); // Just set the value and call validate (or assert) to check if it's ok $t->name = 'Cool Name'; var_dump($set->validate($t)); // bool(true) $t->name = 'aaa'; var_dump($set->validate($t)); // bool(false) The minimum length is 5 $t->name = 'aa4a5'; var_dump($set->validate($t)); // bool(false) Doesn't match regex $t->name = ''; var_dump($set->validate($t)); // bool(false) Can't be empty // Another way is to construct the set with all the rules: // ***Note: Even in this way, new rules can also be added later with the add methods. $set = new ValidationSet(array('name' => array(new v\NotEmptyValidator(), new v\MinLengthValidator(5)), 'age' => new v\MinValueValidator(13))); // Then validate it $t->name = 'this is ok'; $t->setAge(14); var_dump($set->validate($t)); // bool(true) $t->name = 'oops'; $t->setAge(14); var_dump($set->validate($t)); // bool(false) $t->name = 'the age is not good'; $t->setAge(12); var_dump($set->validate($t)); // bool(false)