Pravaler Validator Component
Create a component using Synfony validations in the style of Laravel
Supported Symfony Versions
4.x Symfony
Installation
Download the package
$ composer require pravaler/validatorUsing
Example:
<?php
namespace App\Controller;
use Pravaler\Component\Validator\Validator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class PostController extends Controller
{
public function savePost(Request $request)
{
$data = $request->request->all();
$validator = new Validator([
'name' => 'required',
'author' => 'required',
'email' => 'required|email',
'description' => 'required|size:1:500'
]);
$validator->validate($data);
if (!$validator->isValid()) {
return $this->render("post/index.html.twig", [
'errors' => $validator->getViolations()
]);
}
return $this->render("post/index.html.twig", [
'message' => 'The post is valid!'
]);
}
}
Possible Validations
Cpf
$validator = new Validator([
'field' => 'cpf',
]);$validator = new Validator([
'field' => 'email',
]);Max
$validator = new Validator([
'field' => 'max:10',
]);Min
$validator = new Validator([
'field' => 'min:5',
]);Numeric
$validator = new Validator([
'field' => 'numeric',
]);Password Confirm
// In order for the password confirm to take effect it is necessary to have in the data the password field
$validator = new Validator([
'field' => 'password_confirm',
]);Required
$validator = new Validator([
'field' => 'required',
]);Size
$validator = new Validator([
'field' => 'size:1:500',
]);regex
$validator = new Validator([
'field' => 'regex:/[1-9]{2}\9\d{8}/',
]);License
This project is licensed under the MIT license. For more information, see the license file included in this bundle.
Based
Created based on symfony/validator