duandan1995 2016-01-30 03:47
浏览 26
已采纳

Zend Framework 2使用经过验证的值重新填充表单

I have a simple form class setup along with a filter. After submitting the form, if there's a validation error, the validation/filter works and I can dump the filtered values, but the form does not display the cleaned data. In particular, I am testing with StringTrim and StripTags. I can see the trimmed value, but the final form output still shows the original value submitted. How do I use the validated values instead when the form is repopulated?

An example:
Form data submitted string " asdf ".

Dumping form data, $regform->getData() : "asdf"

The above is expected, but the output in the view still shows the spaces: " asdf ".

I appreciate any input. Code is below. Thank you!

Controller code:

public function indexAction ()
{
    $this->layout()->pageTitle = "Account Registration";
    $regform = new RegForm($data=null);
    if($this->request->isPost()){
        $data = $this->post;
        $regform->setData($data);
        $ufilter = new RegFilter();
        $regform->setInputFilter($ufilter->getInputFilter());
        if($regform->isValid()){
            $this->view->result = "ok";
        }
        else {
            $this->view->result = "Not good";
        }
        var_dump($regform->getData());
    }
    $this->view->regform = $regform;
    return $this->view;
}

RegForm.php

<?php
namespace GWMvc\Form;
use Zend\Form\Form;
use Zend\Form\Element;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;
use Zend\Session\Container;
class RegForm extends Form
{
    public function __construct($data = null, $args = array())
    {
        parent::__construct('reg-form');
        $this->setAttribute('class', 'form form-inline');
        $this->setAttribute('role', 'form');
        $this->setAttribute('method', 'post');
        $this->setAttribute('action','/app/registration/index');

        $this->add(array(
            'name' => 'firstname',
            'type' => 'Zend\Form\Element\Text',

            'options' => array(
                'label' => 'First Name:',

            ),
            'attributes' => array('id' => 'firstname', 'type' => 'text',
                'class' => 'regformitem regtextfield')));

        $this->add(array(
            'name' => 'lastname',
            'type' => 'Zend\Form\Element\Text',
            'options' => array(
                'label' => 'Last Name:'
            ),
            'attributes' => array('id' => 'lastname', 'type' => 'text',
                'required' => true,'class' => 'regformitem regtextfield')));

        $this->add(array(
             'type' => 'Zend\Form\Element\Csrf',
             'name' => 'csrf',
             'options' => array(
                     'csrf_options' => array(
                             'timeout' => 600
                     )
             )
         ));

        $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type'  => 'submit',
                'value' => 'Submit',
                'class' => 'btn btn-default',
            ),
        ));
    }
}

RegFilter.php

<?php
namespace GWMvc\Form;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class RegFilter implements InputFilterAwareInterface
{
    public $username;
    public $password;
    protected $inputFilter;
    public function setInputFilter(InputFilterInterface $inputFilter)
    {
        throw new \Exception("Not used");
    }
    public function getInputFilter()
    {
        if (!$this->inputFilter) {
            $this->inputFilter = new InputFilter();
            $this->factory     = new InputFactory();

            $this->inputFilter->add($this->factory->createInput(array(
                'name'     => 'firstname',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StringTrim'),
                    array('name' => 'StripTags'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 1,
                            'max'      => 50,
                        ),
                    ),
                ),
            )));
            $this->inputFilter->add($this->factory->createInput(array(
                'name'     => 'lastname',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 1,
                            'max'      => 50,
                        ),
                    ),
                ),
            )));
        }
        return $this->inputFilter;
    }
}  

View Script:

<?php
    $form = &$this->regform;
    $form->prepare();
    echo $this->form()->openTag($form);
    echo $this->formElement($form->get('csrf'));?>
    <div class="form" gwc="regitem">
    <?php echo $this->formRow($form->get('firstname')); ?>
    </div>
    <div class="form" gwc="regitem">
    <?php echo $this->formRow($form->get('lastname')); ?>
    </div>

EDIT (SOLUTION) As per the accepted answer below, it was this easy. Here's what I added.

$valid = $regform->isValid();
$regform->setData($regform->getData());
if($valid){
    $this->view->result = "ok";
} else {
    // not ok, show form again
}
  • 写回答

1条回答 默认 最新

  • duanhua9398 2016-01-30 20:47
    关注

    I guess you have to do it manually:

    if($regform->isValid()){
        $regform->setData ($regform->getData ())->isValid ();
        $this->view->result = "ok";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!