Checking the zend form

I wonder how Zend_Form checks the input, I mean, how does it know which input fields should be checked. I looked at php globals ($ _ POST, $ _GET) and I did not see anything specific as an identifier (for example) to know how to check. Can someone offer me a guide for this?

+4
source share
4 answers

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

 /** * 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 Zend_Form over all configured elements in the form and compare them with the values ​​in the array that you passed to it. If there is a match, it will check this individual value for configured validators.

+8
source

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

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

isValid () is a function of the Zend_Form class. The form starts all the checks for each element (just if you don’t stop at the first check failure), and then also for the subforms.

+2
source

Take a look at Zend_Form quickstart , this is a very vivid example of how to get started with forms in Zend.

Checking text input is as follows:

 $username = new Zend_Form_Element_Text('username'); // Passing a Zend_Validate_* object: $username->addValidator(new Zend_Validate_Alnum()); // Passing a validator name: $username->addValidator('alnum'); 
+1
source

Or you can use:

  $username_stringlength_validate = new Zend_Validate_StringLength(6, 20); $username = new Zend_Form_Element_Text('username'); $username->setLabel('Username: ') ->addFilters(array('StringTrim', 'HtmlEntities')) ->setAttrib('minlength', '6') ->setAttrib('class', 'required') ->removeDecorator('label') ->removeDecorator('HtmlTag') ->removeDecorator('DtDdWrapper') ->setDecorators(array(array('ViewHelper'), array('Errors'))) ->addValidator($username_stringlength_validate); 
0
source

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


All Articles