diff --git a/README.textile b/README.textile index 7fa0a658..0b58bc5f 100644 --- a/README.textile +++ b/README.textile @@ -1,3 +1,74 @@ h2. About -Respect\Validation aims to be the most awesome validation toolkit ever created +Respect\Validation aims to be the most awesome validation toolkit ever created. + +Here some nice little things you can do using it: + + noWhitespace() + ->stringLength(1,15) + ->validate($username); + + //Date between two ranges using a specific format + $someDate = '2010-10-15'; + $validDate = v::date('Y-m-d') + ->dateBetween('2009-01-01', 2011-01-01') + ->validate($someDate); + + //Validating objects attributes + $user = new \stdClass; + $user->name = 'Alexandre'; + $user->birthdate = '1987-07-01'; + + $validUser = v::all( + v::hasAttribute('name', v::stringNotEmpty()), + v::hasAttribute('birthdate, v::date('Y-m-d')) + ); + + //Combinating rules, at least one rule must pass + $validDate = v::one( + v::date('Y-m-d'), + v::date('y-m-d'), //two-digit year + v::date('d/m/Y') + ); + + //Must satisfy at least two of the three conditions: + //a) A numeric or alphanumeric value + //b) A date in the format Ymd (20091009) + //c) A string between 3 and 12 characters without whitespace + $valid = v::atLeast(2, array( + v::one(v::numeric(), v::alnum()), + v::date('Ymd'), + v::stringLength(3,12)->noWhitespace() + )); + + //Cool, informative exceptions: + try { + $username = '#$% #odjfubgihdbfgihbdfighb'; + $validUsername = v::alnum() + ->noWhitespace() + ->stringLength(1,15) + ->assert($username); + } catch(v\Exceptions\InvalidException $e) { + /* + Respect\Validation\Exceptions\InvalidException: + #$% #odjfubgihdbfgihbdfighb does not contains only letters and digits (including _) + #$% #odjfubgihdbfgihbdfighb contains spaces, tabs, line breaks or other not allowed charaters. + #$% #odjfubgihdbfgihbdfighb exceeds the maximum of 15 characters + */ + echo $e->getMessage(); + } + + //Non-fluent interface for Dependency Injection maniacs + $validator = new v\Rules\Numeric(); + $validator->validate(123); + + + + diff --git a/library/Respect/Validation/Rules/AbstractComposite.php b/library/Respect/Validation/Rules/AbstractComposite.php index 6a4321c8..9c7147d2 100644 --- a/library/Respect/Validation/Rules/AbstractComposite.php +++ b/library/Respect/Validation/Rules/AbstractComposite.php @@ -7,6 +7,7 @@ use Respect\Validation\Validator; use Respect\Validation\Rules\AbstractRule; use Respect\Validation\Exceptions\ComponentException; use Respect\Validation\Exceptions\InvalidException; +use Exception; abstract class AbstractComposite extends AbstractRule implements Validatable { @@ -85,7 +86,7 @@ abstract class AbstractComposite extends AbstractRule implements Validatable foreach ($validators as $v) try { $v->assert($input); - } catch (InvalidException $e) { + } catch (Exception $e) { $exceptions[] = $e; } return $exceptions; diff --git a/library/Respect/Validation/Rules/StringLength.php b/library/Respect/Validation/Rules/StringLength.php index fd248f10..6fbe873d 100644 --- a/library/Respect/Validation/Rules/StringLength.php +++ b/library/Respect/Validation/Rules/StringLength.php @@ -60,11 +60,11 @@ class StringLength extends AbstractRule implements Validatable { if (!$this->validateMin($input)) throw new StringLengthException( - $this->getMessage(self::MSG_LENGTH_MIN, $input, $this->min) + sprintf($this->getMessage(self::MSG_LENGTH_MIN), $input, $this->min) ); if (!$this->validateMax($input)) throw new StringLengthException( - $this->getMessage(self::MSG_LENGTH_MAX, $input, $this->max) + sprintf($this->getMessage(self::MSG_LENGTH_MAX), $input, $this->max) ); return true; } diff --git a/tests/library/Respect/Validation/ValidatorTest.php b/tests/library/Respect/Validation/ValidatorTest.php index 4ca663de..1e937682 100644 --- a/tests/library/Respect/Validation/ValidatorTest.php +++ b/tests/library/Respect/Validation/ValidatorTest.php @@ -57,4 +57,25 @@ class ValidatorTest extends ValidatorTestCase $this->assertTrue($v); } + public function testValidatorCompositeTwitterUsername() + { + $v = Validator::alnum('_') + ->noWhitespace() + ->stringLength(1, 15) + ->assert('alganet'); + $this->assertTrue($v); + } + + /** + * @expectedException Respect\Validation\Exceptions\InvalidException + */ + public function testValidatorCompositeTwitterUsernameInvalid() + { + $v = Validator::alnum('_') + ->noWhitespace() + ->stringLength(1, 15) + ->assert('#$% #odjfubgihdbfgihbdfighb'); + $this->assertTrue($v); + } + } \ No newline at end of file