我正在尝试在symfony 2中创建自定义验证器,但它从错误的命名空间加载。错误说:
尝试加载课程" DateChecker"来自命名空间" Symfony \ Component \ Validator \ Constraints \ Errand \ MainBundle \ Validator \ Constraints。
这是我的代码:
MainBundle /验证/约束/ DateCheker.php:
<?php
namespace Errand\MainBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class DateChecker extends Constraint
{
/**
*
* @var string
*/
public $message = 'some message';
/**
*
* @return string
*/
public function validatedBy()
{
return get_class($this).'Validator';
}
/**
* Get class constraints and properties
*
* @return array
*/
public function getTargets()
{
return array(self::CLASS_CONSTRAINT, self::PROPERTY_CONSTRAINT);
}
}
MainBundle /验证/约束/ DateCheckerValidator.php:
namespace Errand\MainBundle\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class DateCheckerValidator extends ConstraintValidator
{
/**
* Method to validate
*
* @param string $value Property value
*
*
* @return boolean
*/
public function validate($value, Constraint $constraint)
{
//validation
}
}
MainBundle /资源/配置/ validation.yml:
Errand\MainBundle\Entity\Task:
constraints:
- Errand\MainBundle\Validator\Constraints\DateChecker: ~