View Source

<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[{zone-template-instance:ZFDEV:Zend Proposal Zone Template}

{zone-data:component-name}
Zend_Validate_Array
{zone-data}

{zone-data:proposer-list}
[Boris Tomi?|mailto:boris@kodmasin.net]
[Darby Felton|mailto:darby@zend.com], Zend liaison
{zone-data}

{zone-data:revision}
1.1 - 22 March 2007: Created.
1.2 - 09 December 2007: Small changes
{zone-data}

{zone-data:overview}
Zend_Validate_Array is 'high level' validator created for easier filtering and validation of arrays ($_POST, $_GET). It is intended to give you possibility to create end-user error messages (and use Zend_Translate if you require).

Latest implementation is [Zend_Font - Karol Babioch^Array.php.3.zip].

{zone-data}

{zone-data:references}
* [kodform4|http://code.google.com/p/kodform4/]
{zone-data}

{zone-data:requirements}

{zone-data}

{zone-data:dependencies}
* Zend_Validate_Exception
* Zend_Validate_Interface
* Zend_Filter_Interface
{zone-data}
{zone-data:operation}
This is quite simple solution for validating array values (usually $\_POST, $\_GET, ...). It uses Zend_Filter_* and Zend_Validate_* classes for filtering and validation. You also can add custom messges for validators. That way you can have error messages ready for end user.

So first you have to create object:
{code}
$arrayValidator = Zend_Validate_Array(array("usename", "password", "passworda"));
{code}
Constructor needs to know what keys () will be used. They are passe as array.

Next we need to add some filters and validators:
{code}
$arrayValidator->addFilter("username", new Zend_Filter_StringTrim());
->addValidate("username", new Zend_Validate_Alnum(), "Your can enter just number and digits for username");
->addValidate("username", new Zend_Validate_StringLength(6, 8), "Your username should be betveen 6 and 8 chars long.");
->addValidate("password", new Zend_Validate_Digits(), "Password can contain only numbers.");
{code}

Then we do validation:
{code}
if($arrayValidator->isValid($_POST)){
//data is valid
$validValues = $arrayValidator->getValidValues();
}
else{
$view->errors = $arrayValidator->getErrors();
$view->enduserMessages = $arrayValidator->getUserMessages();
}
{code}

{zone-data}

{zone-data:milestones}
* Milestone 1: Initial design
* Milestone 2: Unit tests
* Milestone 3: Documentation
{zone-data}

{zone-data:class-list}
* Zend_Validate_Array
{zone-data}

{zone-data:use-cases}
||UC-01||
|{code}
$test = array("key1" => 10, "key2" => " 0999837576", "key3" => "FranzHendrix");

$valid = new Zend_Validate_Array(array("key1", "key2", "key3"));
$valid->addFilter("key2", new Zend_Filter_StringTrim())
->addValidate("key1", new Zend_Validate_Int(), "key1 is not integer!")
->addValidate("key2", new Zend_Validate_Digits(), "key2 contains not only digits. That is not allowed!")
->addValidate("key1", new Zend_Validate_Between(3, 14), "key1 is not in corrected range 3<x<14")
->addValidate("key3", new Zend_Validate_Alnum(), "key3 does not contain only alnum chars");

if($valid->isValid($test)){
//it is valid so do something with valid values
Zend_Debug::dump($valid->getValidValue());
Zend_Debug::dump($valid->getValidValue("key2"));
}
else{
//it is not valid display errors
Zend_Debug::dump($valid->getErrors());
}
{code}|

||UC-02 - realworld example||
|{code}
<?php
$input = new Zend_Validate_Array(array("username", "password", "passwordr"));
$input->addFilter("username", new Zend_Filter_StringTrim())
->addValidate("username", new Required(), $this->trans->_("You must enter username."))
->addValidate("username", new Zend_Validate_Alnum(), $this->trans->_("Username must include only alfanumeric charachters"))
->addValidate("username", new Zend_Validate_StringLength(4,8), $this->trans->_("Username must have at least 4 and at most 8 charachters."))
->addValidate("password", new Required(), $this->trans->_("You must enter password."))
->addValidate("password", new Zend_Validate_Alnum(), $this->trans->_("Passowrd must have just alfanumeric charachters."))
->addValidate("password", new Zend_Validate_StringLength(4,8), $this->trans->_("Password must have at least 4 and at most 8 charachters."))
->addValidate("passwordr", new Required(), $this->trans->_("You must confirm your password."))
->addValidate("password", new Equal($input, 'passwordr'), $this->trans->_("Password confirmation does not match password."));
$status = null;

if($input->isValid($_POST)){
//do something usefull - ex. add user to db
}
else{
$messages = $input->getUserMessages();
$status = array(
"type" => "error",
"message" => $this->trans->_("There are some errors while creating new user."),
"errors" => $messages
);
}
echo json_encode($status);
{code}|

{zone-data}

{zone-data:skeletons}
{code}
/**
* @category Zend
* @package Zend_Validate
* @copyright Copyright (c) 2007 Boris Tomic <boris@kodmasin.net>
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Validate_Array implements Zend_Validate_Interface
{

/**
* sets validator available keys which will be validated
*
* @param array $names
*/
public function __construct(array $names){

}
/**
* add filter for key in array to be validated
*
* @param string $name name of key for which filter will be added
* @param Zend_Filter_Interface $filter
* @return Zend_Validate_Array this
*/
public function &addFilter($name, Zend_Filter_Interface $filter)
{

}
/**
* add filter for key in array to be validated
*
* @param string $name name of key for which validator will be added
* @param Zend_Validate_Interface $validate
* @param string $error error message which can be displayed to end user
* @return Zend_Validate_Array this
*/
public function &addValidate($name, Zend_Validate_Interface $validate, $error = null)
{

}
/**
* sets array to be validated.
*
* @param array $values
* @return Zend_Validate_Array this
*/
protected function _setArray($values)
{

}
/**
* clears all values set by validate function. it does not clear already set filters
* and validators
*
*/
private function _reset()
{

}
/**
* check if key is set to be checked
*
* @param string $name name of key to be chacked
*/
private function _isValidName($name)
{

}
/**
* all 'clever' work of this validator is done here. First all filters are applied to
* apropriate values and then this filtered values are validated. If any validator fall
* this function returns false.
*
* @return boolean true if array is valid and false otherwise
*/
public function validate()
{

}
/**
* returns errors of all keys of for one key if its name supplied.
*
* @return mixed array of errors sorted by key names or if $name supplied just errors
* for key with name $name.
*/
public function getUserMessages($name = null)
{

}
/**
* check if some valu is valid
*
* @param string $name key name
* @return boolean true if valid false if not
*/
public function isKeyValid($name)
{

}
/**
* internal function which checks if wanted array is valid
*
* @return boolean true if valid false if not
*/
protected function _isValid()
{

}
/**
* validate array
*
* @param array $values
* @return boolean true if valid false if not
*/
public function isValid($value)
{

}
/**
* returns valid value of wanted key or null if is not valid
*
* @param string $key name of key for wich validated value will be returnned. If null all valid values will be returned.
* @return mixed valid value if valid or null if not valid
*/
public function getValidValue($key = null)
{

}
/**
* returns original error messages for invalid validators
*
* @return array messages
*/
public function getMessages()
{

}
/**
* Add user error for key with $name
*
* @param string $name name of key to wich we will add errror
* @param string $error error
*/
public function addUserErrorMessage($name, $error){

}
/**
* Return error codes
*
* @return array with error codes
*/
public function getErrors(){

}
}
{code}
{zone-data}

{zone-template-instance}]]></ac:plain-text-body></ac:macro>