Issues

ZF-8494: Add method to retrieve only valid values of a Form

Issue Type: New Feature Created: 2009-12-08T02:07:53.000+0000 Last Updated: 2009-12-22T05:11:46.000+0000 Status: Resolved Fix version(s): - 1.10.0 (27/Jan/10)

Reporter: Benjamin Eberlei (beberlei) Assignee: Benjamin Eberlei (beberlei) Tags: - Zend_Form

Related issues: Attachments:

Description

Although you often want to access filtered form data only when the complete form is valid, there are lots of use cases in business entities that have state, where the validity of data is only important at the point where a transform in state is requested.

Zend Form however doesn't give me access to only those values that validated during the current request. This feature would however allow to easily save those values already and just wait for the rest to be entered at a later point. The following simple method does this:

<pre class="highlight">
class Zend_Form //...
{
//...
    public function getValidValues($data)
    {
        $values = array();
        foreach ($this->getElements() as $key => $element) {
            if (isset($data[$key])) {
                if($element->isValid($data[$key], $data)) {
                    $values[$key] = $element->getValue();
                }
            }
        }
        foreach ($this->getSubForms() as $key => $form) {
            if (isset($data[$key])) {
                $values[$key] = $form->getValidValues($data[$key]);
            }
        }

        return $values;
    }

It allows a pattern of the following kind:

<pre class="highlight">
if($this->getRequest()->isPost()) {
    $validValues = $form->getValidValues($this->getRequest()->getPost());
    if(count($validValues)) {
        $service->update($this->getRequest()->getParam('id'), $validValues);
    }
}

Comments

Posted by Benjamin Eberlei (beberlei) on 2009-12-22T05:11:46.000+0000

Was implemented and will be in 1.10 release.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts