The Validation class helps you validate user input.
To start validation you need to create an object.
$validate = new Validation();
Now run the Validation with this simple line:
$response = $validate->run('invalid@mail', 'my_email', 'email');
The first parameter is your input, the second the name (field name), the third is the validation rule.
You can also add your own error message with the optional fourth parameter.
You'll retrieve now a boolean (true or false). If you retrieve a 'false', you can also get the error message object:
Debug::dump($validate->get_errors());
Read a directory and return containing filenames as array.
Static | no | |||
Parameters | Parameter | Type | Default | Description |
$input | string | Value that should be validate. | ||
$field | string | Name of the field that contains given value. | ||
$rules | mixed | The set of validation rules with (optional) rule parameters. Rules are separated with a pipe symbol (|), or as a array of rules. | ||
$pattern | string | null | Optional value. Will be used by validation rule regex. | |
$optional_value | mixed | null | Optional value. Will be used by several validation rules like min_length. | |
$error_message | mixed | null | Custom error message. Messages are separated with a pipe symbol (|), or as a array of Messages. If not set, default message will be used. |
Rule | Description |
---|---|
alpha | Allow alphabetical character |
alpha_numeric | Allow alphanumeric characters |
numeric | Allow numerical characters |
integer | Allow integer characters |
float | Allow float characters |
Allow valide e-mail address. | |
required | Given input is required (not empty) |
min_length | Given input has min length (optional value required!) |
max_length | Given input has max length (optional value required!) |
exact_length | Given input has required (optional value required!) |
regex | Given input validate based on given regular expression |
You can also combine rules to validate your incoming data. Just separate the rules with a pipe symbol (|), or as a array of rules.
$response = $validate->run('invalid@mail', 'my_email', 'required|email'); // or $response = $validate->run( 'invalid@mail', 'my_email', array('required', 'email'));
If you want to customize your error messages, you can add this as fourth parameter.
You can also use different error messages for the different rules you use.
Separate your error messages with a pipe symbol (|), or as a array of error messages.
$response = $validate->run( 'invalid@mail', 'my_email', 'email', null, null, 'Invalid email address given!' ); // or $response = $validate->run( 'invalid@mail', 'my_email', 'required|email', null, null, 'E-Mail is required.|The given e-mail is not a valid e-mail address.' ); // or $response = $validate->run( 'invalid@mail', 'my_email', array( 'required', 'email' ), null, null, array( 'E-Mail is required.', 'The given e-mail is not a valid e-mail address.' ) );
Here is a complete example for you:
Just copy the code below, name the file 'example.php', paste the code and call it like it is written:
<?php
$validate = new Validation();
$response = $validate->run($_GET['email'], 'my_email', 'required|email');
if ($response === false)
{
Debug::dump($validate->get_errors());
}
Here is a complete example for you:
Just copy the code below, name the file 'example.php', paste the code and call it like it is written:
<?php
$value = '#Username';
$regex = '/^[ _a-z0-9öüäÖÜÄß9öüäß?!@.+*-]{1,50}$/i';
$validate = new Validation();
$response = $validate->run($value, 'username', 'required|regex',$regex);
if ( ! $response)
{
Debug::dump($validate->get_errors());
}