1. Go to this page and download the library: Download rakit/validation library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$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' => 'ses([
'province_id' => 'Province',
'district_id' => 'District'
]);
// then validate it
$validation->validate();
$validator = new Validator([
'd',
// 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);
$validator = new Validator;
$validator->setMessages([
'alidation 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);
$validator = new Validator;
$validation_a = $validator->validate($dataset_a, $rules_for_dataset_a, [
'
$validator = new Validator;
$validation_a = $validator->make($dataset_a, $rules_for_dataset_a);
$validation_a->setMessages([
'
$validation = $validator->validate($inputs, [
'random_url' => 'url', // value can be `any_scheme://...`
'https_url' => 'url:http', // value must be started with `https://`
'http_url' => 'url:http,https', // value must be started with `http://` or `https://`
'ftp_url' => 'url:ftp', // value must be started with `ftp://`
'custom_url' => 'url:custom', // value must be started with `custom://`
'mailto_url' => 'url:mailto', // value must conatin valid mailto URL scheme like `mailto:[email protected],[email protected]`
'jdbc_url' => 'url:jdbc', // value must contain valid jdbc URL scheme like `jdbc:mysql://localhost/dbname`
]);
$validation = $validator->validate($_POST, [
'even_number' => [
'n (is_numeric($value) AND $value % 2 === 0);
}
]
]);
$validation = $validator->validate($_POST, [
'even_number' => [
' return ":attribute must be numeric.";
}
if ($value % 2 !== 0) {
return ":attribute is not even number.";
}
// you can return true or don't return anything if value is valid
}
]
]);
use Rakit\Validation\Rule;
class UniqueRule extends Rule
{
protected $message = ":attribute :value has been used";
protected $fillableParams = ['table', 'column', 'except'];
protected $pdo;
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
public function check($value): bool
{
// make sure select count(*) as count from `{$table}` where `{$column}` = :value");
$stmt->bindParam(':value', $value);
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);
// true for valid, false for invalid
return intval($data['count']) === 0;
}
}
use Rakit\Validation\Validator;
$validator = new Validator;
$validator->addValidator('unique', new UniqueRule($pdo));
use Rakit\Validation\Rule;
class UniqueRule extends Rule
{
...
public function table($table)
{
$this->params['table'] = $table;
return $this;
}
public function column($column)
{
$this->params['column'] = $column;
return $this;
}
public function except($value)
{
$this->params['except'] = $value;
return $this;
}
...
}
use Rakit\Validation\Rule;
class YourCustomRule extends Rule
{
protected $implicit = true;
}
use Rakit\Validation\Rule;
use Rakit\Validation\Rules\Interfaces\ModifyValue;
class YourCustomRule extends Rule implements ModifyValue
{
...
public function modifyValue($value)
{
// Do something with $value
return $value;
}
...
}
use Rakit\Validation\Rule;
use Rakit\Validation\Rules\Interfaces\BeforeValidate;
class YourCustomRule extends Rule implements BeforeValidate
{
...
public function beforeValidate()
{
$attribute = $this->getAttribute(); // Rakit\Validation\Attribute instance
$validation = $this->validation; // Rakit\Validation\Validation instance
// Do something with $attribute and $validation
// For example change attribute value
$validation->setValue($attribute->getKey(), "your custom value");
}
...
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.