# Validations
TIP
Zest provides different methods to easily validate data.
# Validation simple usage
Let's consider a simple example. We shall attempt to accept input from the user. The first thing we need to do is define the necessary routes.
# Define Route
$router->add('user/create',['controller'=>"User",'action'=>'userCreate']);
# Create a Controller with Validation Logic
<?php
namespace App\Controllers;
use Zest\View\View;
use Zest\Validation\Validation;
class User extends \Zest\Controller\Controller
{
public function usercreate()
{
if (input('submit')) {
$rules = [
'username' => ['required' => true, ],
'email' => ['required' => true],
'password' => ['required' => true, ],
];
$validation = new Validation(input_all(),$rules,'input');
if($validation->fail()){
$errors = $validation->error()->get();
foreach ($errors as $error) {
foreach ($error as $value) {
echo $value."<br>";
}
}
} else {
//TO-Do create the user
//We will provide auth clas soon
}
} else {
View::view('Home/form');
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# User form file
<!DOCTYPE html>
<html>
<head>
<title>Signup</title>
</head>
<body>
<form actio='' method='post'>
<label>Name: </label>
<input type="text" name="username" ><br>
<label>Email: </label>
<input type="text" name="email" ><br>
<label>Password: </label>
<input type="text" name="password" ><br>
<input type="submit" name="submit">
</form>
</body>
</html>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Getting validation errors for specific fields
/**
* Check if a given key exists in error message
*
* @param $key
* @return bool
*/
$exists = $validator->error()->has('username');
/**
* Get all the validation errors for a specific fields
*
* @param $key
* @return array
*/
$passwordErrors = $validator->error()->get('password');
/**
* Get the first validation error for a specific fields
*
* @param $key
* @return mixed
*/
$firstError = $validator->error()->first('email');
//OR
$lastError = $validator->error()->lase('email');
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Available Validation Rules
The following validation rule are available in Zest Framework
# Required rule
The required rule is used to specify that a specific field cannot be empty:
$rules = [
'username' => ['required' => true,],
];
2
3
# Int rule
The int rule is used to specify that a specific field much be int:
$rules = [
'favNum' => ['int' => true,],
];
2
3
4
# Float rule
The float rule is used to specify that a specific field much be float:
$rules = [
'payment' => ['float' => true,],
];
2
3
4
# String rule
The string rule is used to specify that a specific field much be string:
$rules = [
'name' => ['string' => true,],
];
2
3
4
# Email rule
The required rule is used to specify that a specific field must be a valid email address:
$rules = [
'email' => ['email' => true,],
];
2
3
4
# IP rule
The IP rule is used to specify that a specific field must be valid IP address:
$rules = [
'email' => ['email' => true,],
];
2
3
4
# JSON rule
The JSON rule validates the json value
$validation = new Validation('jsonValue','validate','json');
# Unique rule
The unique rule allows you to check if a given value exists in a specific database table:
$validation = new Validation(['field'=> 'fieldLike_username','value'=>'valueToBeSearch'],'tabelName');