Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zend form validation

I wonder how Zend_Form validates inputs, I mean how does it know which input fields to validate. I looked to php globals($_POST, $_GET) and I didn't see anything set as an identifier(for example ) in order to know how validate. Can anyone suggest me any guide for this stuff?

like image 611
aykut yaman Avatar asked Dec 22 '22 16:12

aykut yaman


2 Answers

Well, the best option to find out is to look at the code of Zend_Form:

/**
 * Validate the form
 *
 * @param  array $data
 * @return boolean
 */
public function isValid($data)
{
    if (!is_array($data)) {
        require_once 'Zend/Form/Exception.php';
        throw new Zend_Form_Exception(__METHOD__ . ' expects an array');
    }
    $translator = $this->getTranslator();
    $valid      = true;
    $eBelongTo  = null;

    if ($this->isArray()) {
        $eBelongTo = $this->getElementsBelongTo();
        $data = $this->_dissolveArrayValue($data, $eBelongTo);
    }
    $context = $data;
    foreach ($this->getElements() as $key => $element) {
        if (null !== $translator && $this->hasTranslator()
                && !$element->hasTranslator()) {
            $element->setTranslator($translator);
        }
        $check = $data;
        if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
            $check = $this->_dissolveArrayValue($data, $belongsTo);
        }
        if (!isset($check[$key])) {
            $valid = $element->isValid(null, $context) && $valid;
        } else {
            $valid = $element->isValid($check[$key], $context) && $valid;
            $data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key);
        }
    }
    foreach ($this->getSubForms() as $key => $form) {
        if (null !== $translator && !$form->hasTranslator()) {
            $form->setTranslator($translator);
        }
        if (isset($data[$key]) && !$form->isArray()) {
            $valid = $form->isValid($data[$key]) && $valid;
        } else {
            $valid = $form->isValid($data) && $valid;
        }
    }

    $this->_errorsExist = !$valid;

    // If manually flagged as an error, return invalid status
    if ($this->_errorsForced) {
        return false;
    }

    return $valid;
}

which means in a nutshell, Zend_Form will iterate over all the configured elements in the form and compare them against the values in the array you passed to it. If there is a match, it will validate that individual value against the configured validators.

like image 188
Gordon Avatar answered Jan 06 '23 03:01

Gordon


So, you create form in action and then check is there post|get data. You can check is_valid form right here. You need pass $_POST or $_GET data to isValid() function. Example:

if ($request->isPost() && $form->isValid($request->getPost())) {

isValid() is function Zend_Form class. Form runs all validations for each element (just if you dont set to stop in first validation fail) and then for subforms too.

like image 43
Vasily Avatar answered Jan 06 '23 03:01

Vasily