pravaler/validator

Validation using the symfony components, but in the laravel style

1.1.2 2018-07-20 16:13 UTC

This package is not auto-updated.

Last update: 2024-05-12 03:27:58 UTC


README

Create a component using Synfony validations in the style of Laravel

Supported Symfony Versions

4.x Symfony

Installation

Download the package

$ composer require pravaler/validator

Using

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',
]);

Email

$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