Download the PHP package bootpress/validator without Composer
On this page you can find all versions of the php package bootpress/validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bootpress/validator
More information about bootpress/validator
Files in bootpress/validator
Package validator
Short Description Form validation that is designed to simplify and work seamlessly with Jörn's jQuery Validation Plugin.
License MIT
Informations about the package validator
use BootPress\Validator\Component as Validator;
Form validation that is designed to simplify and work seamlessly with Jörn's jQuery Validation Plugin. All of the validation routines are designed to match (as closely as possible) those provided by Jörn, so that the browser and server validation routines are in sync with one another.
Installation
Add the following to your composer.json
file.
Example Usage
The first thing you need to do is give us an array of values to validate against. In this case you have given us the $_POST
vars. Now you can set the rules and filters for each field.
Field names can be an array by adding brackets to the end ie. 'name[]'. They can also be multi-dimensional arrays such as 'name[first]', or 'name[players][]', or 'name[parent][child]', etc. The important thing to remember is that you must always use the exact name given here when referencing them in other methods.
Rules and filters are either '|' (single pipe) delimited, or you can make them an array. Custom messages can be specified by making it an array($rule => $message, ...)
. Parameters are comma-delimited, and placed within '[]' two brackets. The available options are:
- 'remote[rule]' - Set
$validator->rules['rule'] = function($value){}
to determine the validity of a submitted value. The function should return a boolean true or false. - 'default' - A default value if the field is empty, or not even set.
- 'required' - This field must have a value, and cannot be empty.
- 'equalTo[field]' - Must match the same value as contained in the other form field.
- 'notEqualTo[field]' - Must NOT match the same value as contained in the other form field.
- Numbers:
- 'number' - Must be a valid decimal number, positive or negative, integer or float, commas okay. Defaults to 0.
- 'integer' - Must be a postive or negative integer number, no commas. Defaults to 0.
- 'digits' - Must be a positive integer number, no commas. Defaults to 0.
- 'min[number]' - Must be greater than or equal to [number].
- 'max[number]' - Must be less than or equal to [number].
- 'range[min, max]' - Must be greater than or equal to [min], and less than or equal to [max].
- Strings:
- 'alphaNumeric' - Alpha (a-z), numeric (0-9), and underscore (_) characters only.
- 'minLength[integer]' - String length must be greater than or equal to [integer].
- 'maxLength[integer]' - String length must be less than or equal to [integer].
- 'rangeLength[minLength, maxLength]' - String length must be greater than or equal to [minLength], and less than or equal to [maxLength].
- 'minWords[integer]' - Number of words must be greater than or equal to [integer].
- 'maxWords[integer]' - Number of words must be less than or equal to [integer].
- 'rangeWords[minWords, maxWords]' - Number of words must be greater than or equal to [minWords], and less than or equal to [maxWords].
- 'pattern[regex]' - Must match the supplied ECMA Javascript compatible [regex].
- 'date' - Must be a valid looking date. No particular format is enforced.
- 'email' - Must be a valid looking email.
- 'url' - Must be a valid looking url.
- 'ipv4' - Must be a valid looking ipv4 address.
- 'ipv6' - Must be a valid looking ipv6 address.
- 'inList[1,2,3]' - Must be one of a comma-separated list of acceptable values.
- 'noWhiteSpace' - Must contain no white space.
- Filters:
- 'singleSpace' - Removes any doubled-up whitespace so that you only have single spaces between words.
- 'trueFalse' - Returns a 1 (true) or 0 (false) integer.
- 'yesNo' - Returns a 'Y' or 'N' value.
To see if the $_POST
array you gave us meets all of your requirements:
The $vars
returned are all trim()
ed and filtered, ready for you to process as you see fit. From here, the best thing to do is use our BootPress Form Component, but if you have any better ideas then you can determine whether or not the $validator->required('field')
, get the submitted $validator->value('field')
, check if there was a $validator->error('field')
, get the data-rule-... $validator->rules('field')
attributes, and the data-msg-... $validator->messages('field')
attributes, find the $validator->id('field')
we assigned, and set the $validator->jquery('#form')
javascript when creating your form and fields.
All of the above is just assuming you are using this component to validate submitted form data, but it is equally well suited to validate anything on the side as well. The static methods we provide (and use ourselves) are:
The value you want to validate always comes first, and any parameters come second - lest you be confused.
License
The MIT License (MIT). Please see License File for more information.