Skip to content

gold688/validation

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rakit Validation - PHP Standalone Validation Library

Build Status License

PHP Standalone library for validating data. Inspired by Illuminate\Validation Laravel.

Requirements

  • PHP 5.5 or higher
  • Composer for installation

Quick Start

Installation

composer require "rakit/validation"

Usage

There are two ways to validating data with this library. Using make to make validation object, then validate it using validate. Or just use validate. Examples:

Using make:

<?php

require('vendor/autoload.php');

use Rakit\Validation\Validator;

$validator = new Validator;

// make it
$validation = $validator->make($_POST, [
	'name' => 'required',	
	'email' => 'required|email',
	'password' => 'required|min:6',
	'confirm_password' => 'required|same:password',
]);

// then validate
$validation->validate();

if ($validation->fails()) {
	// handling errors
	$errors = $validation->errors();
	echo "<pre>";
	print_r($errors->firstOfAll());
	echo "</pre>";
	exit;
} else {
	// validation passes
	echo "Success!";
}

or just validate it:

<?php

require('vendor/autoload.php');

use Rakit\Validation\Validator;

$validator = new Validator;

$validation = $validator->validate($_POST, [
	'name' => 'required',	
	'email' => 'required|email',
	'password' => 'required|min:6',
	'confirm_password' => 'required|same:password',
]);

if ($validation->fails()) {
	// handling errors
	$errors = $validation->errors();
	echo "<pre>";
	print_r($errors->firstOfAll());
	echo "</pre>";
	exit;
} else {
	// validation passes
	echo "Success!";
}

In this case, 2 example above will output the same results.

But with make you can setup something like custom invalid message, custom attribute alias, etc before validation running.

Attribute Alias

By default we will transform your attribute into more readable text. For example confirm_password will be displayed as Confirm password. But you can set it anything you want with setAlias or setAliases method.

Example:

$validator = new Validator;

// To set attribute alias, you should use `make` instead `validate`.
$validation->make([
	'province_id' => $_POST['province_id'],
	'district_id' => $_POST['district_id']
], [
	'province_id' => 'required|numeric',
	'district_id' => 'required|numeric'
]);

// now you can set aliases using this way:
$validation->setAlias('province_id', 'Province');
$validation->setAlias('district_id', 'District');

// or this way:
$validation->setAliases([
	'province_id' => 'Province',
	'district_id' => 'District'
]);

// then validate it
$validation->validate();

Now if province_id value is empty, error message would be 'Province is required'.

Custom Validation Message

Before register/set custom messages, here are some variables you can use in your custom messages:

  • :attribute: will replaced into attribute alias.
  • :value: will replaced into stringify value of attribute. For array and object will replaced to json.
  • :params[n]: will replaced into rule parameter, n is index array. For example :params[0] in min:6 will replaced into 6.

And here are some ways to register/set your custom message(s):

Custom Messages for Validator

With this way, anytime you make validation using make or validate it will set your custom messages for it. It is useful for localization.

To do this, you can set custom messages as first argument constructor like this:

$validator = new Validator([
	'required' => ':attribute harus diisi',
	'email' => ':email tidak valid',
	// etc
]);

// then validation belows will use those custom messages
$validation_a = $validator->validate($dataset_a, $rules_for_a);
$validation_b = $validator->validate($dataset_b, $rules_for_b);

Or using setMessages method like this:

$validator = new Validator;
$validator->setMessages([
	'required' => ':attribute harus diisi',
	'email' => ':email tidak valid',
	// etc
]);

// now validation belows will use those custom messages
$validation_a = $validator->validate($dataset_a, $rules_for_dataset_a);
$validation_b = $validator->validate($dataset_b, $rules_for_dataset_b);

Custom Messages for Validation

Sometimes you may want to set custom messages for specific validation. To do this you can set your custom messages as 3rd argument of $validator->make or $validator->validate like this:

$validator = new Validator;

$validation_a = $validator->validate($dataset_a, $rules_for_dataset_a, [
	'required' => ':attribute harus diisi',
	'email' => ':email tidak valid',
	// etc
]);

Or you can use $validation->setMessages like this:

$validator = new Validator;

$validation_a = $validator->make($dataset_a, $rules_for_dataset_a);
$validation_a->setMessages([
	'required' => ':attribute harus diisi',
	'email' => ':email tidak valid',
	// etc
]);

...

$validation_a->validate();

Custom Message for Specific Attribute Rule

Sometimes you may want to set custom message for specific rule attribute. To do this you can use . as message separator or using chaining method.

Examples:

$validator = new Validator;

$validation_a = $validator->make($dataset_a, [
	'age' => 'required|min:18'
]);

$validation_a->setMessages([
	'age.min' => '18+ only',
]);

$validation_a->validate();

Or using chaining methods:

$validator = new Validator;

$validation_a = $validator->make($dataset_a, [
	'photo' => [
		'required',
		$validator('uploaded_file')->fileTypes('jpeg|png')->message('Photo must be jpeg/png image')
	]
]);

$validation_a->validate();

Available Rules

Below is list of all available validation rules

required

The field under this validation must be present and not 'empty'.

Here are some examples:

Value Valid
'something' true
'0' true
0 true
[0] true
[null] true
null false
[] false
'' false

For uploaded file, $_FILES['key']['error'] must not UPLOAD_ERR_NO_FILE.

required_if:another_field,value_1,value_2,...

The field under this rule must be present and not empty if the anotherfield field is equal to any value.

For example required_if:something,1,yes,on will be required if something value is one of 1, '1', 'yes', or 'on'.

uploaded_file:min_size,max_size,file_type_a,file_type_b,...

This rule will validate $_FILES data, but not for multiple uploaded files. Field under this rule must be following rules below to be valid:

  • $_FILES['key']['error'] must be UPLOAD_ERR_OK or UPLOAD_ERR_NO_FILE. For UPLOAD_ERR_NO_FILE you can validate it with required rule.
  • If min size is given, uploaded file size MUST NOT be lower than min size.
  • If max size is given, uploaded file size MUST NOT be higher than max size.
  • If file types is given, mime type must be one of those given types.

Here are some example definitions and explanations:

Definition Explanation
uploaded_file Uploaded file is optional. When it is not empty, it must be ERR_UPLOAD_OK.
`required uploaded_file`
uploaded_file:0,1M uploaded file size must be between 0 - 1 MB, but uploaded file are optional
`required uploaded_file:0,1M,png,jpeg`

email

The field under this validation must be valid email address.

alpha

The field under this rule must be entirely alphabetic characters.

numeric

The field under this rule must be numeric.

alpha_num

The field under this rule must be entirely alpha-numeric characters.

alpha_dash

The field under this rule may have alpha-numeric characters, as well as dashes and underscores.

in

The field under this rule must be included in the given list of values.

not_in

The field under this rule must not be included in the given list of values.

min

soon ...

max

soon ...

between

soon ...

url

The field under this rule must be valid url format.

ip

The field under this rule must be valid ipv4 or ipv6.

ipv4

The field under this rule must be valid ipv4.

ipv6

The field under this rule must be valid ipv6.

array

The field under this rule must be array.

same:another_field

The field value under this rule must be same with another_field value.

regex:/your-regex/

The field under this rule must be match with given regex.

date:format

The field under this rule must be valid date format. Parameter format is optional, default format is Y-m-d.

accepted

The field under this rule must be one of 'on', 'yes', '1', 'true', or true.

present

The field under this rule must be exists, whatever the value is.

different:another_field

Opposite of same. The field value under this rule must be different with another_field value.

Register/Modify Rule

Soon ...

About

PHP Standalone Validation Library

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%