7

所以当我开始使用 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>
4

5 回答 5

13

您应该增加php.ini 中的两个值

 post_max_size = 50M    
 upload_max_filesize = 50M

你可以看到你的 php.ini 配置:

 php -i | grep "your text to find"

我希望能帮助你

于 2013-11-05T11:35:09.490 回答
1

您似乎正在尝试上传某个太大的文件。您在该操作中使用其他形式吗?

您可以阅读 Symfony 2.3 文档:文件验证

uploadIniSizeErrorMessage

类型string 默认值The file is too large. Allowed maximum size is {{ limit }}

如果上传的文件大于upload_max_filesize PHP.ini 设置时显示的消息。

于 2013-06-09T20:45:42.743 回答
1

在您的Entity.php 中,您需要为您的Assert 文件指定“ maxSize ”属性。

这个值的单位是字节。

2GB 为“2147483648”。

/**
* @ORM\Column(type="string", length=255)
*/
public $path;

/**
 * @Assert\File(maxSize="2147483648")
 */
public $file;
于 2014-04-04T08:50:13.570 回答
-2

在 symfony.com 2.3 版本更改为 BETA,将回滚到 2.2 稳定版,麻烦因为它没有经过测试并且是 BETA

于 2013-06-10T18:50:27.643 回答
-2

是 Symfony2.3 上的一个错误

固定在那里:https ://github.com/symfony/symfony/pull/9048

于 2013-09-16T09:30:59.097 回答