Skip to content

Use Laravel validation standalone with support translate

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

salkhwlani/laravel-validation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Laravel Validation.

Software License Build Status Quality Score

A tiny package to use laravel validation outside laravel with support translate error message. this package is extend for rakit/validation so read it's document for more information.

Features

  • Init validation easy with trait.
  • Support Multi lang for error messages.
  • More come soon.

Requirements & Installation

Requires PHP 7.0+

Via Composer

$ composer require yemenifree/laravel-validation

Getting Started

To init validation on class add HasValidator trait.

use Yemenifree\Validation\Traits\HasValidator;

class SomeController
{
    use HasValidator;
    //
}

Then to valid some data you can pass array for data & rules and others options.

    $this->valid(array $data, array $rules, array $messages = [], array $aliases = [])

For example

/**
 * Register User
 *
 * @return array
 */
public function register()
{
    $isValid = $this->valid(
    [
        'username' => 'salah',
            'password' => 'test'
    ]
    , [
        'username' => 'required',
        'password' => 'required',
    ]);
    
    if(!$isValid){
        // data not valid.
    }

    // every thing right.
}

If you have same response for all form in controller you can handler validation error once by create InValidCallback in class.

use Yemenifree\Validation\Traits\HasValidator;

class SomeController
{
    use HasValidator;
    
    /**
     * Register User
     *
     * @return array
     */
    public function register()
    {
        $isValid = $this->valid(
        [
            'username' => 'salah',
            'password' => 'test'
        ]
        , [
            'username' => 'required',
            'password' => 'required',
        ]);
        
        // if data not valid will excute `InValidCallback` in this class.
        if(!$isValid)
        {
            return;
        }
        
        // every thing right.
    }
    
    /**
     * In valid function.
     *
     * @param array $errors
     *
     * @throws InvalidArgumentException
     */
    public function InValidCallback(array $errors)
    {
        // do whatevet you want with errors.
        
        return false;
    }
}

You can use custom translate file for validation errors.

use Yemenifree\Validation\Traits\HasValidator;
    
class SomeController
{
    use HasValidator;
    
    public function __construct()
    {
        $this->setValidatorLocal(
// file name wihout .php
        'ar',
        // path of translate
        'translate/path'
        );
    }
        
    /**
     * Register User
     *
     * @return array
     */
    public function register()
    {
        $isValid = $this->valid(
        [
            'username' => 'salah',
            'password' => 'test'
        ]
        , [
            'username' => 'required',
            'password' => 'required',
        ]);
        
         if(!$isValid)
        {
            // message errors.
            $errors = $this->getValidErrors();
        }
        
        // every thing right.
    }
}

translate files must return array of messages. see src/lang/ar.php for example.

To access to all method of Validator use getValidator() method.

use Yemenifree\Validation\Traits\HasValidator;

class SomeController
{
    use HasValidator;

    public function __construct()
    {
        // add custom rule.
        $this->getValidator()->addValidator('simple', new SimpleRule());
    }
}

For more information about rules check rakit/validation

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

About

Use Laravel validation standalone with support translate

Topics

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Packages

No packages published

Languages