2017 © Pedro Peláez
 

library laravel-validation-trait

Provide a powerful trait for saving model easily

image

yangmls/laravel-validation-trait

Provide a powerful trait for saving model easily

  • Monday, June 15, 2015
  • by yangmls
  • Repository
  • 1 Watchers
  • 9 Stars
  • 4 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Laravel Validation Trait

Provide a powerful trait for laravel 4 model, (*1)

Use it in Eloquent

create a model and use it, (*2)

use Yangmls\ValidationTrait;

class User extends Eloquent
{
    use ValidationTrait;

    public function rules()
    {
        return [
            'email' => ['required', 'email'],
        ];
    }

    public function ruleMessages()
    {
        return [
            'email.required' => ':attribute is required',
        ];
    }

    public function customAttributes()
    {
        return [
            'email' => 'E-Mail',
        ];
    }
}

create a new model and save, (*3)

$model = new User();
$result = $model->saveFromRequest(Input::all()); // true or false
return Response::json($model->getErrors());

create a new model without errors, (*4)

$model = User::createFromRequest(Input::all()); // User instance or null

save a existing model, (*5)

$model = User::find(1);
$result = $model->saveFromRequest(Input::all()); // true or false
return Response::json($model->getErrors());

Use it in other models

sometimes you may process a form without Eloquent, you can do like this, (*6)

use Yangmls\ValidationTrait;

class Login
{
    use ValidationTrait;

    public $attributes;

    public function __construct($input = [])
    {
        $this->attributes = $input;
    }

    public function validate($options = [])
    {
        return $this->validateRequest($this->attributes, $options);
    }

    public function rules()
    {
        return [
            'email' => ['required', 'email'],
        ];
    }

    public function ruleMessages()
    {
        return [
            'email.required' => ':attribute is required',
        ];
    }

    public function customAttributes()
    {
        return [
            'email' => 'E-Mail',
        ];
    }
}

then call it in controller, (*7)

$model = new Login(Input::all());
$result = $model->validate(); // true or false
return Response::json($model->getErrors());

Inline Validators

validator can be defined in the class and will be called automatically, (*8)

use Yangmls\ValidationTrait;

class User extends Eloquent
{
    use ValidationTrait;

    protected function validatorEmail($value, $input, $options)
    {
        // $value is attribute value
        // $input is whole input
        // $options is the config you pass to saveFromRequest

        // Note: 
        // 1. you must use addError to stop saving
        // 2. you must return true if you think the validator is passed
    }
}

Built-in hooks

below methods are called automatically when you do validating or saving, (*9)

beforeSave, afterSave, beforeValidate, afterValidate, (*10)

however you can also use laravel built-in events for saving/creating/updating, (*11)

License

under the MIT license, (*12)

The Versions

15/06 2015

dev-master

9999999-dev

Provide a powerful trait for saving model easily

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Clive Young