Symfony2,错误“上传的文件太大了。”没有文件字段

时间:2013-06-09 18:25:05

标签: symfony

所以当我开始使用2.3版本的symfony时,我有未知的错误 断言实体

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;
    }
}

FormType

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>

5 个答案:

答案 0 :(得分:11)

你应该在php.ini

中增加两个值
 post_max_size = 50M    
 upload_max_filesize = 50M

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

 php -i | grep "your text to find"

我希望能帮到你

答案 1 :(得分:1)

您似乎正在尝试上传某个太大的文件。你在那次行动中使用了其他形式吗?

您可以阅读Symfony 2.3文档:File validation

  

uploadIniSizeErrorMessage

     

输入string 默认The file is too large. Allowed maximum size is {{ limit }}

     

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

答案 2 :(得分:1)

Entity.php 中,您需要为maxSize指定“Assert file”属性。

该值的单位是字节。

“2147483648”表示2GB。

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

/**
 * @Assert\File(maxSize="2147483648")
 */
public $file;

答案 3 :(得分:-2)

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

答案 4 :(得分:-2)

是Symfony2.3的错误

修正了:https://github.com/symfony/symfony/pull/9048