所以当我开始使用 2.3 版本的 symfony Assert Entity 时出现未知错误
class FormRegister
{
/**
* @Assert\Regex
* (
* pattern="/^[a-zA-Z0-9]{1,}$/i",
* message="You use illegal character(s). Must be a-z, A-Z and 0-9 symbols."
* )
* @Assert\Length
* (
* min="4",
* minMessage="User name must be more then 3 characters.",
* max="15",
* maxMessage="User name must be less then 16 characters."
* )
*/
private $username;
public function setUsername($username)
{
$this->username = $username;
return $this;
}
public function getUserName()
{
return $this->username;
}
/**
* @Assert\NotBlank
* (
* message="Password is required. :)"
* )
* @Assert\Length
* (
* min="4",
* minMessage="Password must be more then 3 characters.",
* max="25",
* maxMessage="Password must be less then 255 characters."
* )
*/
private $password;
public function setPassword($password)
{
$this->password = $password;
return $this;
}
public function getPassword()
{
return $this->password;
}
/**
* @Assert\NotBlank
* (
* message="Email is required."
* )
* @Assert\Email
* (
* message="Email address has incorrect type, try re-type or use another mail."
* )
*/
private $email;
public function setEmail($email)
{
$this->email = $email;
return $this;
}
public function getEmail()
{
return $this->email;
}
/**
* @Assert\True
* (
* message="We can't register if you will not accept our terms."
* )
*/
private $terms;
public function setTerms($terms)
{
$this->terms = $terms;
return $this;
}
public function getTerms()
{
return $this->terms;
}
}
表单类型
class FormRegisterType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('username', 'text', array('label' => 'User Name :'))
->add('password', 'repeated',
array(
'type' => 'password',
'first_name' => 'pass1',
'second_name' => 'pass2',
'first_options' => array('label' => 'Password:',),
'second_options' => array('label' => 'Confirm Password:'),
'error_bubbling' => true,
'invalid_message' => 'Password not matched.'))
->add('email', 'text', array('label' => 'E-Mail'))
->add('terms', 'checkbox', array('attr' => array('class' => 'checkBoxTerms')));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Main\SiteBundle\Form\FormRegister',
'required' => false));
}
public function getName()
{
return 'FormRegisterType';
}
}
行动
public function registerAction()
{
$formRegister = $this->createForm(new FormRegisterType(), new FormRegister());
if ($this->getRequest()->isMethod('post'))
{
$em = $this->getDoctrine()->getManager();
$formRegister->handleRequest($this->getRequest());
$formData = $formRegister->getData();
if ($formRegister->get('username')->isValid())
{
$exist = $em->getRepository('MainSiteBundle:User')
->findOneByUsername($formData->getUserName());
if ($exist)
{
$error = new FormError('Such account name as "'.$formData->getUserName().'" already exists');
$formRegister->get('username')->addError($error);
}
}
if ($formRegister->isValid())
{
echo 'form is valid';
$ef = $this->get('security.encoder_factory');
$user = new User();
$pass = $ef->getEncoder($user)->encodePassword($formData->getPassword(), $user->getSalt());
$user->setPassword($pass)
->setUsername($formData->getUsername())
->setEmail($formData->getEmail);
$em->persist($user);
$em->flush();
}
else
{
var_dump($formRegister->getErrors());
}
}
$twig = 'MainSiteBundle:Default:register.html.twig';
$data = array(
'formRegister' => $formRegister->createView());
return $this->render($twig, $data);
}
如果输入有效数据,我仍然会在我没有使用的“文件类型”上出现错误!
array (size=1)
0 =>
object(Symfony\Component\Form\FormError)[1418]
private 'message' => string 'The uploaded file was too large. Please try to upload a smaller file.' (length=69)
protected 'messageTemplate' => string 'The uploaded file was too large. Please try to upload a smaller file.' (length=69)
protected 'messageParameters' =>
array (size=1)
'{{ max }}' => string '2048M' (length=5)
protected 'messagePluralization' => null
问题出在哪里?我根本没有发送文件。它出现在我开始使用的 2.3 版本中。
var_dump($formRegister->getErrorsAsString());
给我这样
string 'ERROR: The uploaded file was too large. Please try to upload a smaller file.
username:
No errors
password:
first:
No errors
second:
No errors
email:
No errors
terms:
No errors
' (length=212)
更新
模板
<form method="POST" action="{{ path ('site_register') }}" {{ form_enctype(formRegister)}}>
{{ form_errors(formRegister.username) }}
{{ form_label(formRegister.username) }}
{{ form_widget(formRegister.username) }}
<br />
{{ form_errors(formRegister.password.first) }}
{{ form_label(formRegister.password.first) }}
{{ form_widget(formRegister.password.first) }}
<br />
{{ form_errors(formRegister.password.second) }}
{{ form_label(formRegister.password.second) }}
{{ form_widget(formRegister.password.second) }}
<br />
{{ form_errors(formRegister.email) }}
{{ form_label(formRegister.email) }}
{{ form_widget(formRegister.email) }}
<br />
{{ form_errors(formRegister.terms) }}
<div class="terms">
{{ form_widget(formRegister.terms) }} I read <a href="#">this terms</a> and accept.
</div>
<input type="submit" value="Register"/>
</form>