2

I have a form based on Zend_Form.
When form isn't valid, my inputs contain data.
But if all OK, after submiting form conains all data.

I know how clear form with help of jQuery.
But how I make it in Zend Framework?

3 Answers 3

13

In your controller, after you have checked that your form is submitted and valid, and you have handled the data, you can try:

$form->reset();

in order to clean the form. More info in the ZF manual here: http://framework.zend.com/manual/en/zend.form.forms.html#zend.form.forms.elements.values

Sign up to request clarification or add additional context in comments.

1 Comment

You can also try <? = $this->formText ?> and use jQuery if you are lost.
3

Unfortunately this does not work in ZEND 2. See the code below to clear a form in ZEND 2

$elements = $form->getElements();

foreach ($elements as $element) {
  if ($element instanceof \Zend\Form\Element\Text) {
    $element->setValue('');
  }
  // Other element types here
}

Credits

2 Comments

What if I want to reset the form to its default values, instead of just empty values?
@Aise, create a new form object, reusing your original form variable. As in... $form = new YourFormClass();
0

Like this:

$form = new AlbumForm();
$request = $this->getRequest();
if ($request->isPost())
{
    $album = new Album();
    $form->setData($request->getPost());
    if ($form->isValid())
    {
        /*
         * Process Form
         */
        ...

        /*
         * Create new Form
         */
        $form = new AlbumForm();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.