Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2, Error "The uploaded file was too large." without file field

Tags:

symfony

So i have unknown error when start use 2.3 version of 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;
    }
}

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

Action

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

If input valid data i still will have error on 'file type' which i didn't use!

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

Where is the problem? I didn't sending file at all. It appears in 2.3 version i start to use.

var_dump($formRegister->getErrorsAsString());

Give me such

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)

UPDATED

Template

<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>
like image 437
user1954544 Avatar asked Jun 09 '13 18:06

user1954544


2 Answers

You should increase both values in php.ini

 post_max_size = 50M    
 upload_max_filesize = 50M

you can see your php.ini configuration:

 php -i | grep "your text to find"

I hope to help you

like image 141
Freenando Avatar answered Sep 28 '22 22:09

Freenando


It seems you are trying to upload a file in some point that is too large. Do you use some other form in that action?

You can read in the Symfony 2.3 documentation: File validation

uploadIniSizeErrorMessage

type: string default: The file is too large. Allowed maximum size is {{ limit }}

The message that is displayed if the uploaded file is larger than the upload_max_filesize PHP.ini setting.

like image 36
dgil Avatar answered Sep 28 '22 22:09

dgil