Zend Framework 2 Repopulate form using validated values
<p>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?</p>

<p>An example:<br>
Form data submitted string "   asdf   ".</p>

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

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

<p>I appreciate any input. Code is below. Thank you!</p>

<p>Controller code:</p>

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

<p>RegForm.php</p>

<pre><code>&lt;?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-&gt;setAttribute('class', 'form form-inline');
        $this-&gt;setAttribute('role', 'form');
        $this-&gt;setAttribute('method', 'post');
        $this-&gt;setAttribute('action','/app/registration/index');

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

            'options' =&gt; array(
                'label' =&gt; 'First Name:',

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

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

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

        $this-&gt;add(array(
            'name' =&gt; 'submit',
            'attributes' =&gt; array(
                'type'  =&gt; 'submit',
                'value' =&gt; 'Submit',
                'class' =&gt; 'btn btn-default',
            ),
        ));
    }
}
</code></pre>

<p>RegFilter.php</p>

<pre><code>&lt;?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-&gt;inputFilter) {
            $this-&gt;inputFilter = new InputFilter();
            $this-&gt;factory     = new InputFactory();

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

<p>View Script:</p>

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

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

<pre><code>$valid = $regform-&gt;isValid();
$regform-&gt;setData($regform-&gt;getData());
if($valid){
    $this-&gt;view-&gt;result = "ok";
} else {
    // not ok, show form again
}
</code></pre>