我需要在控制器动作中动态添加约束,所以我这样做:
use Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique;
// ...
$form = $this->createFormBuilder($user)
->add('email', 'email', array(
'constraints' => array(
new NotBlank(),
new MinLength(8),
new MaxLength(100),
new Email(),
new Unique(),
),
))
->getForm();
但我得到这个错误:
The options "fields" must be set for constraint Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique
我尝试将array('fields' => 'email')
and传递array('fields' => array('email'))
给构造函数,但没有奏效:Warning: get_class() expects parameter 1 to be object, string given in /home/www/dev/public/pixfeed/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php line 63
那么如何使用这个类呢?