定制 yangmls/laravel-validation-trait 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yangmls/laravel-validation-trait

Composer 安装命令:

composer require yangmls/laravel-validation-trait

包简介

Provide a powerful trait for saving model easily

README 文档

README

Provide a powerful trait for laravel 4 model

Use it in Eloquent

create a model and use it

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

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

create a new model without errors

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

save a existing model

$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

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

$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

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

beforeSave, afterSave, beforeValidate, afterValidate

however you can also use laravel built-in events for saving/creating/updating

License

under the MIT license

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-04