我对 Symfony 2.2 中的那些验证约束有疑问。
似乎没有考虑“用户名”和“通过”的验证约束。但是“UniqueEntity”约束有效。
我不认为我在 yaml 语法中犯了错误。
这是 yml 语法:
Fastre\PhpFtpAdminBundle\Entity\Account:
properties:
username:
- Regex: {pattern: "/[a-zA-Z0-9]+/", message: Le nom d'utilisateur ne peut contenir que des lettres minuscules et majuscules et des chiffres.}
pass:
- Length: {min: 8, minMessage: "Your name must have at least {{ limit }} characters."}
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: {fields: [username], groups: [registration]}
在控制器中:
if ($request->getMethod() === 'POST') {
$form->bind($request);
$data = $form->getData();
$errors = $this->get('validator')->validate($data, array('Default'));
//throw new \Exception($errors->count());
if ($errors->count() == 0) { //the errors->count() is always set to 0!
$em->flush();
//TODO: i18n
$this->get('session')
->getFlashBag()
->add('notice', 'Compte '.$account->getUsername().' modifié');
return $this->redirect($this->generateUrl('account_list'));
} else {
foreach ($errors as $error) {
$this->get('session')
->getFlashBag()
->add('warning', $error->getMessage());
}
return $this->render('FastrePhpFtpAdminBundle:Accounts:form.html.twig',
array(
'form' => $form->createView(),
'action_path' => $this->generateUrl('account_view', array('id' => $id))
)
);
}
}