我为注册表单创建了一个FormType。验证工作应该如此。奇怪的是: 错误立即打印出来。当显示时(在页面加载时),表单会立即告诉我某些字段无效,尽管我还没有开始填写字段。
我的formtype类:
<?php
namespace MyBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\MaxLength;
use Symfony\Component\Validator\Constraints\MinLength;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use MyBundle\Validation\Constraint\Unique;
use MyBundle\Validation\Constraint\InvitationCode;
use MyBundle\Validation\Constraint\Username;
class RegistrationFormType extends AbstractType
{
/**
*
* @var FormBuilder
*/
private $builder;
public function buildForm(FormBuilder $builder, array $options)
{
$this->builder = $builder;
$this->builder
->add('code','text', array(
'label' => 'Einladungscode'
))->add('username','text',array(
'label' => 'Benutzername',
))
->add('email', 'email',array(
'label' => 'E-Mail'
))
->add('plainPassword', 'repeated', array('type' => 'password',
'first_name'=>'Passwort',
'second_name'=> 'Passwort wiederholen',
)
);
}
public function showRegistrationFields(){
}
public function getDefaultOptions(array $options)
{
$collectionConstraint = new Collection(array(
'email' => array(
new NotBlank(),
new Unique(array(
'entityName' => 'AjadoEventHubBundle:User',
'propertyName' => 'email')),
new Email(array(
'message' => 'Ungültige E-Mail Adresse',
)),
),
'username' => array(
new NotBlank(),
new Unique(array(
'entityName' => 'AjadoEventHubBundle:User',
'propertyName' => 'username')),
new Username(),
new MinLength(array('limit' => 5)),
new MaxLength(array('limit' => 40)),
),
'code' => array(
new MaxLength(array('limit'=>200)),
new InvitationCode(),
),
'plainPassword' => array(
new MaxLength(array('limit'=>20)),
new MinLength(array('limit' => 5))
),
));
return array(
'csrf_protection' => false,
'validation_constraint' => $collectionConstraint,
);
}
public function getName()
{
return 'registration';
}
}
答案 0 :(得分:2)
我改变了
$form->bindRequest($this->getRequest());
if ($this->getRequest()->getMethod() == 'POST' && $form->isValid()) {
到
if ($this->getRequest()->getMethod() == 'POST') {
$form->bindRequest($this->getRequest());
if($form->isValid()){
感谢@meze的提示!