mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 03:42:25 +02:00
New Sf valitador that uses Symfony 2.0 Validators and Constraints. Moved Zend validator to support only the 2.x ZF.
This commit is contained in:
parent
fc661bcfec
commit
a2fbdc340f
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
library/Zend/
|
||||
library/Symfony/
|
52
library/Respect/Validation/Rules/Sf.php
Normal file
52
library/Respect/Validation/Rules/Sf.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use ReflectionClass;
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\CallbackException;
|
||||
use Respect\Validation\Exceptions\InvalidException;
|
||||
use Symfony\Component\Validator\ConstraintViolation;
|
||||
|
||||
class Sf extends AbstractRule implements Validatable
|
||||
{
|
||||
|
||||
protected $messages = array();
|
||||
protected $constraint;
|
||||
protected $validator;
|
||||
protected $name;
|
||||
|
||||
public function __construct($name, $params=array())
|
||||
{
|
||||
$this->name = ucfirst($name);
|
||||
$sfMirrorConstraint = new ReflectionClass(
|
||||
'Symfony\Component\Validator\Constraints\\' . $this->name
|
||||
);
|
||||
$this->constraint = $sfMirrorConstraint->newInstanceArgs($params);
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$validatorName = 'Symfony\Component\Validator\Constraints\\'
|
||||
. $this->name . 'Validator';
|
||||
$this->validator = new $validatorName;
|
||||
return $this->validator->isValid($input, $this->constraint);
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input)) {
|
||||
$violation = new ConstraintViolation(
|
||||
$this->validator->getMessageTemplate(),
|
||||
$this->validator->getMessageParameters(),
|
||||
'',
|
||||
'',
|
||||
$input
|
||||
);
|
||||
throw new InvalidException($violation->getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -16,7 +16,7 @@ class Zend extends AbstractRule implements Validatable
|
|||
|
||||
public function __construct($name, $params=array())
|
||||
{
|
||||
$zendMirror = new ReflectionClass('Zend_Validate_' . ucfirst($name));
|
||||
$zendMirror = new ReflectionClass('Zend\Validator\\' . ucfirst($name));
|
||||
$this->zendValidator = $zendMirror->newInstanceArgs($params);
|
||||
}
|
||||
|
||||
|
|
24
tests/library/Respect/Validation/Rules/SfTest.php
Normal file
24
tests/library/Respect/Validation/Rules/SfTest.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class SfTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
|
||||
public function testParamsOk()
|
||||
{
|
||||
$v = new Sf('minLength', array('limit'=>3));
|
||||
$this->assertTrue($v->assert('wp2oiur'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\InvalidException
|
||||
*/
|
||||
public function testParamsNot()
|
||||
{
|
||||
$v = new Sf('minLength', array('limit'=>3));
|
||||
$this->assertTrue($v->assert('a'));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue