Zend Form: Dependant field validation

笑着哭i 提交于 2019-12-12 01:03:36

问题


I have a form with two select boxes: country and city, city depending on the selected country. Note that the city field is populated dynamically when a country is selected using Ajax.

So far I extended Zend_Form_Element_Select overriding isValid() for the city select box, and I'll use the $context argument to get the selected country and check if the city is valid (for that country).

I want to skip city validation if the country validation fails. For example someone can inject a bad value into the country field (and the country validation will fail), and my city validation shouldn't take place and don't do that query on db, it simply shouldn't pass the validation.

How can I achieve this?


回答1:


Override the isValid() and setDefaults() methods in your Form class to populate the city element when the country element is set.

function isValid($data) {
    if (isset($data['country'])) {
        $this->populateCity($data['country']);
    }
    return parent::isValid($data);
}

function setDefault($name, $value) {
    if ('country' === $value) {
        $this->populateCity($value);
    }
    return parent::setDefault($name, $value);
}


来源:https://stackoverflow.com/questions/7377489/zend-form-dependant-field-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!