我有一个不使用实体类的简单表单。
发布后我想使用验证器验证值,但即使值为空或无效,错误列表的计数也始终为零。
这是(或多或少)我正在执行的代码:
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\Collection;
public function formAction(){
$collectionConstraint = new Collection(array(
'name' => array(new MinLength(5)),
'email' => array(new Email(array('message' => 'Invalid email address'))),
));
$options = array('validation_constraint' => $collectionConstraint);
$form = $this->createFormBuilder(null, $options)
->add('name', 'text', array('label' => '', 'attr' => array('placeholder' => 'Your name')))
->add('email', 'email', array('label' => '', 'attr' => array('placeholder' => 'E-mail')))
->getForm();
$request = $this->getRequest();
$error = false;
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$errorList = $this->get('validator')->validateValue($data, $collectionConstraint);
// count($errorList) is always zero even when the values are empty or invalid…
}
else {
$error = true;
}
}
// ... snip ...
}