Package Data | |
---|---|
Maintainer Username: | andersonandrade |
Maintainer Contact: | contato@andersonandra.de (Anderson Andrade) |
Package Create Date: | 2015-01-25 |
Package Last Update: | 2024-05-05 |
Home Page: | http://andersao.github.io/laravel-validator |
Language: | PHP |
License: | Unknown |
Last Refreshed: | 2024-12-19 03:17:05 |
Package Statistics | |
---|---|
Total Downloads: | 8,548,488 |
Monthly Downloads: | 134,358 |
Daily Downloads: | 5,759 |
Total Stars: | 412 |
Total Watchers: | 10 |
Total Forks: | 82 |
Total Open Issues: | 25 |
Add "prettus/laravel-repository": "1.1.*" to composer.json
"prettus/laravel-validation": "1.1.*"
The Validator contains rules for adding, editing.
Prettus\Validator\Contracts\ValidatorInterface::RULE_CREATE
Prettus\Validator\Contracts\ValidatorInterface::RULE_UPDATE
In the example below, we define some rules for both creation and edition
use \Prettus\Validator\LaravelValidator;
class PostValidator extends LaravelValidator {
protected $rules = [
'title' => 'required',
'text' => 'min:3',
'author'=> 'required'
];
}
To define specific rules, proceed as shown below:
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'
]
];
}
You may use custom error messages for validation instead of the defaults
protected $messages = [
'required' => 'The :attribute field is required.',
];
Or, you may wish to specify a custom error messages only for a specific field.
protected $messages = [
'email.required' => 'We need to know your e-mail address!',
];
You too may use custom name attributes
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 - contato@andersonandra.de
http://culttt.com/2014/01/13/advanced-validation-service-laravel-4/