Symfony2:为什么我的表单在提交之前就显示错误?
我为注册表创建了一个FormType。验证工作应该是这样的。奇怪的是: 错误会立即打印出来。当显示(加载页面时)时,表单立即告诉我某些字段无效,尽管此时我还没有开始填写字段 我的formtype类:Symfony2:为什么我的表单在提交之前就显示错误?,symfony,Symfony,我为注册表创建了一个FormType。验证工作应该是这样的。奇怪的是: 错误会立即打印出来。当显示(加载页面时)时,表单立即告诉我某些字段无效,尽管此时我还没有开始填写字段 我的formtype类: <?php namespace MyBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; use Symfony\Component\Validator\Co
<?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';
}
}
我变了
$form->bindRequest($this->getRequest());
if ($this->getRequest()->getMethod() == 'POST' && $form->isValid()) {
到
谢谢@meze的提示 请出示你的行动代码。谢谢,这是我所需要的全部提示,我自己想出来的。。。
if ($this->getRequest()->getMethod() == 'POST') {
$form->bindRequest($this->getRequest());
if($form->isValid()){