Search code examples
formssymfony1actionfieldreset

symfony 1.4 resett values from fields after fail


How can i reset my formfield values ?

action:
            $this->form = new $class();
            $this->form->bind($request->getParameter('signin'));
            if ($this->form->isValid())
            {
                $values = $this->form->getValues();
                            $this->redirect('@example');
                    }
            else
            {
                if ($this->form->hasGlobalErrors())
                    $this->errorclass = 'error';
            }
            $this->form->resetFormFields();
            return $this->renderPartial('ajax/example);

I have 2 fields email and pw: want to resett them after error that they empty

This dont work :(

$this->form->resetFormFields();

Any solutions? Thx in regards


Solution

  • If your form has only 2 fields (email and pw), you can simply reinit form.

    else
    {
      if ($this->form->hasGlobalErrors())
        $this->errorclass = 'error';
      $this->form = new $class();
    }
    

    If your form has more fields (no only email and pw) and you want to preserve other field values, you can remove email and pw values and bind new form.

    $values = $request->getParameter('signin');
    $this->form->bind($values);
    if ($this->form->isValid())
    {
      $this->redirect('@example');
    }
    else
    {
      if ($this->form->hasGlobalErrors())
        $this->errorclass = 'error';
      $values['pw'] = '';
      $values['email'] = '';
      $this->form = new $class();
      $this->form->bind($values);
    }