Skip to content

Instantly share code, notes, and snippets.

@kbariotis
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kbariotis/dfba7e1b576954232cd5 to your computer and use it in GitHub Desktop.
Save kbariotis/dfba7e1b576954232cd5 to your computer and use it in GitHub Desktop.
Respect/Validation Example
<?php
require_once __DIR__.'/your/vendor/autoload.php';
use Respect\Validation\Validator;
/**
* This would be your Model class
**/
class User {
private static $validator;
public function __construct() {
self::$validator = new Validator();
self::applyValidationRules();
}
public static function applyValidationRules() {
/**
* Apply your rulez here.
**/
self::$validator
->attribute('firstname', self::$validator->string()->length(1,2))
->attribute('lastname', self::$validator->string()->notEmpty());
}
public static function v($subject) {
self::$validator->assert($subject);
}
public function validate() {
self::v($this);
}
}
$o = new User();
$o->firstname = "Kostas";
$o->lastname = "Bariotis";
$o->validate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment