开发者

zend framework empty form after submit

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?


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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜