Validator class to load with composer.
PHP
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
README.md
composer.json

README.md

rafamds/validator

Latest Stable Version Total Downloads Latest Unstable Version License

Overview

This PHP Validator script is a set of validation rules which lets you add server-side validation to your forms quickly and with as little effort as possible. This lets you create validations using prebuilt rules and custom error messages with ease.

Instalation

To install it, you have to use composer as package manager and run on the command line, the following code:

composer require rafamds/validator

Or you can add this "rafamds/validator": "dev-master" in your composer.json file and composer update on the command line.

How to Validate data

use Validator\Validation;

$rules = [
  "required,name,name",
	"required,email,email",
	"valid_email,email,email",
	"required,password,password",
	"same_as,password,conpassword,conpassword"
];

$validator = new Validation;
$errors = $validator->validateFields($request->input(), $rules);

Rules List

// standard form fields
$rules[] = "required,user_name,This field is required.";
$rules[] = "required,email,Please enter your email address.";
$rules[] = "valid_email,email,Please enter a valid email address.";
    
// date fields
$rules[] = "valid_date,any_date_month,any_date_day,any_date_year,any_date,Please enter a valid date.";
$rules[] = "valid_date,later_date_month,later_date_day,later_date_year,later_date,Please enter an date later than today.";
    
// Numbers / alphanumeric fields
$rules[] = "required,any_integer,Please enter an integer.";
$rules[] = "digits_only,any_integer,This field may only contain digits.";
$rules[] = "digits_only,number_range,This field may only contain digits.";
$rules[] = "range=1-100,number_range,Please enter a number between 1 and 100.";
$rules[] = "range>100,number_range_greater_than,Please enter a number greater than 100.";
$rules[] = "range>=100,number_range_greater_than_or_equal,Please enter a number greater than or equal to 100.";
$rules[] = "range<100,number_range_less_than,Please enter a number less than 100.";
$rules[] = "range<=100,number_range_less_than_or_equal,Please enter a number less than or equal to 100.";
$rules[] = "letters_only,letter_field,Please only enter letters (a-Z) in this field.";
$rules[] = "required,alpha_field,Please enter an alphanumeric (0-9 a-Z) string.";
$rules[] = "is_alpha,alpha_field,Please only enter alphanumeric characters (0-9 a-Z) in this field.";
$rules[] = "custom_alpha,custom_alpha_field1,LLL-VVV,Please enter a string of form LLL-VVV - where L is an uppercase "
   . "letter and V is an uppercase vowel.";
$rules[] = "custom_alpha,custom_alpha_field2,DDxxx,Please enter a string of form DDxxx.";
$rules[] = "custom_alpha,custom_alpha_field3,EEXX,Please enter a string of form EEXX.";
$rules[] = "custom_alpha,custom_alpha_field4,VVvvllFF,Please enter a string of form VVvvllFF.";
$rules[] = "custom_alpha,custom_alpha_field5,#XccccCCCC,Please enter a string of form #XccccCCCC.";
$rules[] = "reg_exp,reg_exp_field1,^\s*(red|orange|yellow|green|blue|indigo|violet|pink|white)\s*$,Please enter your "
   . "favourite colour in lowercase (e.g. \"red\" or \"blue\")";
$rules[] = "required,reg_exp_field2,Please enter your favourite colour (e.g. \"red\" or \"blue\")";
$rules[] = "reg_exp,reg_exp_field2,^\s*(red|orange|yellow|green|blue|indigo|violet|pink|white)\s*$,i,Please enter your "
   . "favourite colour (e.g. \"red\" or \"blue\")";
 
// Length of field input
$rules[] = "length=2,char_length,Please enter a value that is exactly two characters long.";
$rules[] = "length=3-5,char_length_range,Please enter a value that is between 3 and 5 characters in length.";
$rules[] = "length>5,char_length_greater_than,Please enter a value that is over 5 characters long.";
$rules[] = "length>=5,char_length_greater_than_or_equal,Please enter a value that is at least 5 characters long.";
$rules[] = "length<5,char_length_less_than,Please enter a value that is less than 5 characters long.";
$rules[] = "length<=5,char_length_less_than_or_equal,Please enter a value that is less than or equal to 5 characters.";
 
// password fields
$rules[] = "required,password,Please enter a password.";
$rules[] = "same_as,password,password_2,Please ensure the passwords you enter are the same.";
 
// conditional (if-else) fields
$rules[] = "required,gender,Please enter your gender.";
$rules[] = "if:gender=male,required,male_question,Please enter the name of your favourite Care Bear.";
$rules[] = "if:gender=female,required,female_question,Please indicate what max weight you can bench.";  

Credits

Rafael Santos (Class, Package & Improvements)

Benjamin Keen (Validation Script)

License

The MIT License (MIT)

Copyright (c) 2015 Rafael Santos(rafaelmsantos.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.