Zend_Form manually sets and validates a field value.

I have a Zend_Form with a drop down field. When the user sets a value in the URL, the default value should be selected from this drop-down list.

So what I am doing at the moment:

$parlang = $this->getRequest()->getParam('lang'); if($parlang){ $this->view->filterForm->getElement('ddLanguage')->setValue($parlang); } if ($this->getRequest()->isPost()) { if($this->view->filterForm->isValid($_POST)){ ... ... ... 

No, I want to check if the value of the variable is valid for the drop-down list? How can I test this in collaboration with form validation. Yes, I can check the variable for an array or so, but it looks like a “wireframe fight."

So what is the Zend way to do this?

Edit: My final decision for anyone interested is:

 $parlang = $this->getRequest()->getParam('lang'); if($parlang){ $ddLanguage = $this->view->filterForm->ddLanguage; if($ddLanguage->isValid($parlang)){ $ddLanguage->setValue($parlang); $language = $parlang; } } 
+4
source share
2 answers

I checked a quick test, and it looks like one of the methods you can use is Zend_Form_Element_Select::getMultiOption() to check if the language exists in the selection values.

 <?php $parlang = $this->getRequest()->getParam('lang'); if ($parlang) { $el = $this->view->filterForm->getElement('ddLanguage'); // attempt to get the option // Returns null if no such option exists, otherwise returns a // string with the display value for the option if ($el->getMultiOption($parlang) !== null) { $el->setValue($parlang); } } 
+1
source

If your Multiselect element contains a list of countries, I would just populate the default value in your element value according to the address in the URL.

To do this, you can create your own Zend_Form_Element as follows:

 class My_Form_Element_SelectCountry extends Zend_Form_Element_Select { protected $_translatorDisabled = true; public function init() { $locale = Zend_Registry::get('Zend_Locale'); if (!$locale) { throw new Exception('No locale set in registry'); } $countries = Zend_Locale::getTranslationList('territory', $locale, 2); unset($countries['ZZ']); // fetch lang parameter and set US if there is no param $request = Zend_Controller_Front::getInstance()->getRequest(); $lang = $request->getParam('lang', 'US'); // sort your country list $oldLocale = setlocale(LC_COLLATE, '0'); setlocale(LC_COLLATE, 'en_US'); asort($countries, SORT_LOCALE_STRING); setlocale(LC_COLLATE, $oldLocale); // check weither the lang parameter is valid or not and add it to the list if (isset($countries[$lang])) { $paramLang = array($lang => $countries[$lang]); $countries = array_merge($paramLang, $countries); } $this->setMultiOptions($countries); } 

}

You get the idea from this user form. If what you are trying to do is not a Multiselect field filled with a list of countries, but a list of languages ​​instead, then the logic will be the same, you just need to change the call to the static method Zend_Locale::getTranslationList() and capture all the information you need.

One more thing, if you only need one element in your Multiselect element, go to Zend_Form_Element_Hidden .

This is a lot of ifs, but I can’t understand how your Multiselect element looks exactly from your question.

Now let's take a look at the validation side, when you use the Multiselect element, Zend_Framework automatically adds an InArray validator, which means you have nothing to do to check whether the data is sent correctly or not. isValid is going to do it for you.

The user can specify the default parameter, and everything will be fine, or he will change / delete this parameter, and the default parameter (en_US in this case, see the code above) will be set as the default value for the Multiselect field.

To answer your last question, no, not against the framework, to check the variable set by the user and compare it with an array (for example, from getTranslationList() ). I would say that this is even the recommended way to do something.

+1
source

Source: https://habr.com/ru/post/1397126/


All Articles