| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
This class follows Zend Framework naming conventions for easy drop-in as a substitute to Zend_Validation. If you opt out of using the bulky Zend_Form on your projects, you might choose to use this for quick and painless form validation.
The example below shows how to throw validation exceptions with the custom exception. You can then retrieve the error messages from the calling method. It is not good practice to validate your data in your controller, this should be handled in your Model. This is just a quick example.
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
->required('You must supply a name.')
->validate('name', 'Name');
$validator
->required('You must supply an email address.')
->email('You must supply a valid email address')
->validate('email', 'Email');
// check for errors
if ($validator->hasErrors()) {
throw new Validator_Exception(
'There were errors in your form.',
$validator->getAllErrors()
);
}
return $validator->getValidData();
}
}
Callback functions can be passed as strings or closures.
// numeric example
$validadator
->callback('is_numeric', 'Field is not numeric.')
->validate('number_field');
// closure example
$validator
->callback(function($val) {
return $val < -1 || $val > 1;
}, 'Number must be less than -1 or greater than 1.')
->validate('number_field_2');
This validation class has been extended to allow for validation of arrays as well as nested indices of a multi-dimensional array.
To validate specific indices of an array, use dot notation, i.e.
// load the validator
$validator = new Validator($_POST);
// ensure $_POST['field']['nested'] exists
$validator
->required('The nested field is required.')
->validate('field.nested');
// ensure we have the first two numeric indices of $_POST['links'][]
$validator
->required('This field is required')
->validate('links.0');
$validator
->required('This field is required')
->validate('links.1');
You can apply pre-validation filters to your data (i.e. trim, strip_tags, htmlentities). These filters can also be custom defined so long as they pass an is_callable() check.
// 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');
| Back | FazBrowse Home | New Git URL |