prettus/laravel-validation

此包的最新版本(1.6.0)没有提供许可证信息。

Laravel 验证服务

1.6.0 2024-05-05 23:33 UTC

This package is auto-updated.

Last update: 2024-09-06 00:37:27 UTC


README

Total Downloads Latest Stable Version Latest Unstable Version License

安装

将 "prettus/laravel-repository": "1.1.*" 添加到 composer.json 中

"prettus/laravel-validation": "1.1.*"

创建验证器

验证器包含添加、编辑的规则。

Prettus\Validator\Contracts\ValidatorInterface::RULE_CREATE
Prettus\Validator\Contracts\ValidatorInterface::RULE_UPDATE

在下面的示例中,我们为创建和编辑定义了一些规则

use \Prettus\Validator\LaravelValidator;

class PostValidator extends LaravelValidator {

    protected $rules = [
        'title' => 'required',
        'text'  => 'min:3',
        'author'=> 'required'
    ];

}

要定义特定的规则,请按照以下步骤操作

use \Prettus\Validator\LaravelValidator;

class PostValidator extends LaravelValidator {

    protected $rules = [
        ValidatorInterface::RULE_CREATE => [
            'title' => 'required',
            'text'  => 'min:3',
            'author'=> 'required'
        ],
        ValidatorInterface::RULE_UPDATE => [
            'title' => 'required'
        ]
   ];

}

自定义错误信息

您可以使用自定义错误信息来验证,而不是默认值。

protected $messages = [
    'required' => 'The :attribute field is required.',
];

或者,您可能只想为特定字段指定自定义错误信息。

protected $messages = [
    'email.required' => 'We need to know your e-mail address!',
];

自定义属性

您也可以使用自定义名称属性

protected $attributes = [
    'email' => 'E-mail',
    'obs' => 'Observation',
];

使用验证器

use \Prettus\Validator\Exceptions\ValidatorException;

class PostsController extends BaseController {

    /**
     * @var PostRepository
     */
    protected $repository;
    
    /**
     * @var PostValidator
     */
    protected $validator;

    public function __construct(PostRepository $repository, PostValidator $validator){
        $this->repository = $repository;
        $this->validator  = $validator;
    }
   
    public function store()
    {

        try {

            $this->validator->with( Input::all() )->passesOrFail();
            
            // OR $this->validator->with( Input::all() )->passesOrFail( ValidatorInterface::RULE_CREATE );

            $post = $this->repository->create( Input::all() );

            return Response::json([
                'message'=>'Post created',
                'data'   =>$post->toArray()
            ]);

        } catch (ValidatorException $e) {

            return Response::json([
                'error'   =>true,
                'message' =>$e->getMessage()
            ]);

        }
    }

    public function update($id)
    {

        try{
            
            $this->validator->with( Input::all() )->passesOrFail( ValidatorInterface::RULE_UPDATE );
            
            $post = $this->repository->update( Input::all(), $id );

            return Response::json([
                'message'=>'Post created',
                'data'   =>$post->toArray()
            ]);

        }catch (ValidatorException $e){

            return Response::json([
                'error'   =>true,
                'message' =>$e->getMessage()
            ]);

        }

    }
}

作者

Anderson Andrade - [email protected]

鸣谢

http://culttt.com/2014/01/13/advanced-validation-service-laravel-4/