Home > Backend Development > PHP Tutorial > Example of using the validator of the zf framework (custom validator and validator chain)_PHP tutorial

Example of using the validator of the zf framework (custom validator and validator chain)_PHP tutorial

WBOY
Release: 2016-07-13 10:35:58
Original
815 people have browsed it

Copy code The code is as follows:

require_once("Zend/Loader.php");
//Introducing the validator class and the functional class (Int) of the validator, and the custom interface class;
Zend_Loader::loadClass('Zend_Validate');
Zend_Loader::loadClass('Zend_Validate_Int' ; Declare the error message reporting attribute in the interface
protected $_messages = array();
//Declare the verification method in the interface
public function isValid($num)
{
if ( !($num%3==0) && !($num%5==0))
{
//If the verification fails, the error message return value is given to the error message reporting attribute
$this -> _messages[] = "The value you entered is not a common multiple of 3 and 5!";
// Terminate the program
return false;
}
//Return true
return true;
}
//Define the error reporting method of the interface
public function getMessages()
{
return $this -> _messages;
}
// Define extraction error information (optional)
public function getErrors()
{

}
}
//External definition of common multiple detection method
function check_num($num){

//Instantiate the validator class
$Validate = new Zend_Validate();
//Add validator function class, add custom validator function class, form a validator chain
$Validate -> addValidator(new Zend_Validate_Int())
- > addValidator(new GongBeiNum());
//Validation parameters
if (!$Validate -> isValid($num))
{
//If there is an error, loop the error message and Output
foreach ($Validate -> getMessages() as $value)
{
echo $value . "
";
return false;
}
}
}

//Specify the judgment value
$num1 = '15';
//Run the verification method
check_num($num1);

?>






http://www.bkjia.com/PHPjc/741266.htmlwww.bkjia.com

truehttp: //www.bkjia.com/PHPjc/741266.htmlTechArticleCopy the code as follows: ?php require_once("Zend/Loader.php"); //Introduce the validator Function classes (Int) of classes and validators, and custom interface classes; Zend_Loader::loadClass('Zend_Validate'); Z...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template