1. Go to this page and download the library: Download omnicode/lara-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/ */
omnicode / lara-validation example snippets
namespace App\Validators;
use LaraValidation\LaraValidator;
class PostValidator extends LaraValidator
{
/**
* @return \LaraValidation\CoreValidator
*/
public function validationDefault()
{
$this->validator
->
// .. - your namespace
use App\Validators\PostValidator;
// ... controller, service or other class
protected $postValidator;
public function __construct(PostValidator $postValidator)
{
$this->postValidator = $postValidator;
}
public function someMethod()
{
// $data -> the data, that should be validated - as an array
// can be taken from request by $request->all()
// or can be a custom-created as below
$data = [
'title' => 'some title',
'content' => 'Some content for post'
];
if ($this->postValidator->isValid($data)) {
// validated
} else {
// not validated
}
}
public function validationDefault()
{
$this->validator->
// the rule will be applied only if the callable method returns true
$this->validator->s_company']) ? true : false;
});
$this->validator->add('date_of_birth', 'date')
$this->validator->add('some_field', [
'rule' => function ($attribute, $value, $parameters, $validator){
// logic goes here
// return true to apply the validation or false otherwise
}
], __('Some optional validation message'));
public function minLength($name, $length, $message = '', $when = null)
public function maxLength($name, $length, $message = '', $when = null)
public function numeric($name, $message = '', $when = null)
public function unique($name, $params = [], $message = '', $when = null)
$this->validator->unique('email', 'users', __('Email already exists. Please restore your password'));
namespace App\Http\Requests;
use App\Validators\PostValidator;
class PostRequest
{
/**
* @var PostValidator
*/
protected $postValidator;
/**
* @param PostValidator $postValidator
*/
public function __construct(PostValidator $postValidator)
{
$this->rules = $postValidator->validationDefault()->rules();
$this->messages = $postValidator->validationDefault()->messages();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.