Last active
August 29, 2015 14:07
-
-
Save kbariotis/dfba7e1b576954232cd5 to your computer and use it in GitHub Desktop.
Respect/Validation Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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