1. Go to this page and download the library: Download hundredminds/php-validation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hundredminds / php-validation example snippets
class ExampleController extends Zend_Controller_Action {
/**
* Your controller action that handles validation errors, as you would
* want these errors passed on to the view.
*
* @access public
* @return void
*/
public function indexAction()
{
try {
// validate the data
$validData = $this->_validate($_POST);
// validation passed because no exception was thrown
// ... to something with the $validData ...
} catch (Validator_Exception $e) {
// retrieve the overall error message to display
$message = $e->getMessage();
// retrieve all of the errors
$errors = $e->getErrors();
// the below code is specific to ZF
$this->_helper->FlashMessenger(array('error' => $message));
$this->_helper->layout->getView()->errors = $errors;
}
}
/**
* Your user-defined validation handling. The exception section is
* very important and should always be used.
*
* @access private
* @param array $post
* @return mixed
*/
private function _validate(array $post = array())
{
$validator = new Validator($post);
$validator
->
// load the validator
$validator = new Validator($_POST);
// ensure $_POST['field']['nested'] exists
$validator
->d('This field is
// standard php filter for valid user ids.
$validator
->filter('intval')
->min(1)
->validate('user_id');
// custom filter
$validator
->filter(function($val) {
// bogus formatting of the field
$val = rtrim($val, '/');
$val .= '_custom_formatted';
return $val;
})
->validate('field_to_be_formatted');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.