Why custom validation in Laravel 5.6 isn't working? - php

I created a custom rule with php artisan make:rule, set it in controller, but it not working. What the problem can be?
class CheckDeliveryDate implements Rule
{
public $client_id;
private $is_after_midday;
private $error_messge;
public function __construct(int $client_id)
{
$this->client_id = $client_id;
$this->is_after_midday = Carbon::now()->greaterThan(Carbon::now()->midDay());
$this->error_messge = 'Error';
}
public function passes($attribute, $value)
{
$delivery_date = Carbon::parse($value);
if ($delivery_date->isToday()) {
$this->error_messge = 'Error';
return false;
}
if ($delivery_date->endOfDay()->isPast()) {
$this->error_messge = 'Error';
return false;
}
return true;
}
public function message()
{
return $this->error_messge;
}
}
In controller i set method rules:
public function rules($client_id)
{
return [
'orders.*.positions.*.delivery_time' => [
'required',
'date',
new CheckDeliveryDate($client_id)
],
];
}
and when i store my order, validator->fails() return me "false".
$validator = Validator::make(
$request->all(),
$this->rules($client_id)
);
I tried set dd or dump in Rule, not working. Where is my mistake?

As stated on laravel documentation (https://laravel.com/docs/5.8/validation#custom-validation-rules) you are not supposed to pass the parameter to your custom rule instance. Laravel does it for you.
Thus:
new CheckDeliveryDate($client_id)
Becomes :
new CheckDeliveryDate
Have a nice day !

Wrong JSON format date. This is amazing mistake...

Related

Laravel withValidator() not working as expected

I have this formrequest that contains rules and a withValidator as a second layer of validation.
Note: I am aware that having it unique on the rules would supress the need for this example, but I'll need to do further validations here.
public function rules(Request $request) {
return [
"name" => "required|max:191",
"begin_date" => "required|after_or_equal:today|date_format:d-m-Y",
"end_date" => "required|after:begin_date|date_format:d-m-Y",
];
}
public function withValidator($factory) {
$result = User::where('name', $this->name)->get();
if (!$result->isEmpty()) {
$factory->errors()->add('User', 'Something wrong with this guy');
}
return $factory;
}
I am positive that it enters the if as I've placed a dd previously it to check if it's going inside. However, it proceeds to this method on the Controller and I don't want it to.
public function justATest(UserRequest $request) {
dd("HI");
}
I'm an idiot and didn't read the full doc.
It needs to specify with an after function,like this:
public function withValidator($factory) {
$result = User::where('name', $this->name)->get();
$factory->after(function ($factory) use ($result) {
if (!$result->isEmpty()) {
$factory->errors()->add('User', 'Something wrong with this guy');
}
});
return $factory;
}
I was facing this problem too.
I changed my withValidator to this:
public function withValidator($validator)
{
if (!$validator->fails()) {
$validator->after(function ($validator) {
if (Cache::has($this->mobile)) {
if (Cache::get($this->mobile) != $this->code) {
$validator->errors()->add('code', 'code is incorrect!');
} else {
$this->user = User::where('mobile', $this->mobile)->first();
}
} else {
$validator->errors()->add('code', 'code not found!');
}
});
}

Laravel 5.1 - Return First Error From FormRequest Validation

I have the following form request:
class FileRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
$rules = [];
if($this->file_type == 'image')
$rules['file'] = 'required|image|mimes:jpg,jpeg,gif|max:244|image_size:>=360,>=180';
else
$rules['file'] = 'required|mimes:doc,pdf,docx,txt,rtf|max:1000';
return $rules;
}
public function messages()
{
$messages = [
'file.image_size' => 'The image size is incorrect.'
];
return $messages;
}
public function response(array $errors)
{
$content = "<textarea data-type=\"application/json\">{\"ok\": false, \"message\": \"" . $errors[0] . "\" }</textarea>";
return response($content);
}
}
How do i return the first error from the errors array.
When I manually create a validator I'm able to do the following: $validator->errors()->first() but this doesn't work when using a FormRequest class. Doing errors[0] simply gives me an offset exception error.
I'm using an iframe submit which is why I need to return the response as above.
Any help appreciated.
You seem to need use the validator system from laravel.
After that you could use : $validator->messages() ; for have all the errors.
If you use $validator->messages()[0]; you gonna have the first message.
I suggest you something like that
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'file' =>'required|image|mimes:jpg,jpeg,gif|max:244|image_size:>=360,>=180';
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
// Store the file
}
I've had to do the following:
$errors['file'][0]

Laravel 4 - redirect from a repository when not returning the called method

I am using a repository pattern in my Laravel 4 project but come across something which I think I am doing incorrectly.
I am doing user validation, before saving a new user.
I have one method in my controller for this:
public function addNewUser() {
$validation = $this->userCreator->validateUser($input);
if ( $validation['success'] === false )
{
return Redirect::back()
->withErrors($validation['errors'])
->withInput($input);
}
return $this->userCreator->saveUser($input);
}
Then the validateUser method is:
public function validate($input) {
$rules = array(
'first_name' => 'required',
'last_name' => 'required',
'email_address' => 'unique:users'
);
$messages = [
];
$validation = Validator::make($input, $rules, $messages);
if ($validation->fails())
{
$failed = $validation->messages();
$response = ['success' => false, 'errors' => $failed];
return $response;
}
$response = ['success' => true];
return $response;
}
This may be okay, but I dont like doing the if statement in my controller? I would rather be able to handle that in my validation class.
But to be able to redirect from the validation class, I need to return the method in the controller.
What if I then want to have 5 methods called, I cant return them all?
I would like to be able to simply call the methods in order, then in their respective class handle what I need to and if there is any errors redirect or deal with them. But if everything is okay, simply ignore it and move to the next function.
So example:
public function addNewUser()
{
$this->userCreator->validateUser($input);
$this->userCreator->formatInput($input);
$this->userCreator->sendEmails($input);
return $this->userCreator->saveUser($input);
}
If doing the if statement in the controller isn't as bad as I think then I can continue, but this seems incorrect?
For repository pattern, you can use this :-
setup your basemodel like this
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model{
protected static $rules=null;
protected $errors=null;
public function validateForCreation($data)
{
$validation=\Validator::make($data,static::$rules);
if($validation->fails())
{
$this->errors=$validation->messages();
return false;
}
return true;
}
/**
* #return errors
*/
public function getErrors() { return $this->errors; }
}
now in your repository, add these methods
protected $model;
protected $errors=null;
public function model(){ return $this->model; }
public function getErrors(){ return $this->errors; }
public function create($inputs)
{
if(!$this->model->validateForCreation($inputs))
{
$this->errors=$this->model->getErrors();
return false;
}
$new=$this->model->create($inputs);
return $new;
}
and the controller will look like this..
public function postCreate(Request $request)
{
$inputs=$request->all();
if($new=$this->repo->create($inputs))
{
return redirect()->back()
->with('flash_message','Created Successfully');
}
return redirect()->back()->withInput()->withErrors($this->repo->getErrors())
->with('flash_message','Whoops! there is some problem with your input.');
}

Laravel 4 Unique Validation Rules Not Working

need help updating a unique rule in my validation rules. I have a abstract validator that will validate a rules before storing into my database and in the rules array I set the email to be unique when creating or registering a user but when updating the user the enique email should not validate if the email is owned by the user.
abstract class Validator
abstract class Validator {
protected $errors;
protected $attributes;
public function __construct($attributes = null)
{
$this->attributes = $attributes ?: \Input::all();
}
public function passes()
{
$validation = \Validator::make($this->attributes, $this->rules());
if ($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
public function getErrors()
{
return $this->errors;
}
}
Validation Rules(UserRule.php)
use MyCustomValidatorNamespaceHere....
class UserRules extends Validator
public function rules()
{
return [
'email' => 'required|email|unique:users,email,id',
...
];
}
and in my UserController I injected the UserRule in the constractor. (UserRule $userRule). Here is the code in the update method.
public function update($id)
{
$if ($this->userRule->passes())
{
$this->user->find($id)->update(Input::all());
return .........
}
}
But the validation always fail and displaying the error that the email is already taken. Please help guys.
The problem is your rule. When you update, you need to use unique that doesn't check record you update. So you should have:
unique:users,email,id
but for example:
unique:users,email,10
if you edit record with id 10.
What you could do is to define this rule:
'email' => 'required|email|unique:users,email,{id}',
and your passes method:
public function passes($id = null)
{
$rules = $this->rules();
$rules['email'] = str_replace('{id}', $id, $rules['email']);
$validation = \Validator::make($this->attributes, $rules);
if ($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
and now in update rule use:
if ($this->userRule->passes($id))
By the way you have error in $if ($this->userRule->passes()) - it should be if and not $if
You can use the route method inside your request class to except an id from the validation
public function rules()
{
return [
'email' => 'required|email|unique:users,email,'.$this->route('user'),
...
];
}
I had a problem like that before and it was difficult to find an answer. Here is what I did.
class UserRules extends Validator {
public function __construct($input = NULL) {
$this->input = $input ?: \Input::all();
if(isset($this->input['id'])):
self::$rules['username'] = 'required|unique:users,username,'.$this->input['id'];
self::$rules['email'] = 'required|email|unique:users,email,'.$this->input['id'];
else:
self::$rules['username'] = 'required|unique:users';
self::$rules['email'] = 'required|email|unique:users';
endif;
}
public static $rules = array(
'company_id' => 'required',
'role' => 'required',
'password' => 'sometimes|required|confirmed'
);
}
You need to use self:: because $rules is static.
I have moved this function:
public function passes($id)
{
$rules = static::$rules;
$rules['username'] = str_replace('{id}', $id, $rules['username']);
$rules['email'] = str_replace('{id}', $id, $rules['email']);
$validation = \Validator::make($this->attributes, $rules);
if($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
into UserRule.php and commented the same function in abstract class Validator
Now updating is working.
I solved this problem here on stackoverflow in a generic way. It will automatically adapt your rules if you do an update and add exceptions to your :unique if necessary.

return validation errors as an array and change to json

I am trying to return my validation errors to angular but I can't figure out how to return them in the format array('field under validation' => 'error message'). This exact array is held in errors->messages() but it is a protected property.
here is my code
validator.php
<?php namespace TrainerCompare\Services\Validation;
use Validator as V;
/**
*
*/
abstract class Validator
{
protected $errors;
public function validate($data)
{
$validator = V::make($data, static::$rules);
if ($validator->fails()) {
$this->errors = $validator->messages();
return false;
}
return true;
}
public function errors()
{
return $this->errors;
}
}
controller
<?php
use TrainerCompare\Services\Validation\ProgramValidator;
class ProgramsController extends BaseController
{
protected $program;
protected $validator;
public function __construct(Program $program, ProgramValidator $validator)
{
$this->program = $program;
$this->validator = $validator;
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
$input = Input::all();
$v = $this->validator->validate($input);
if ($v == true) {
//$this->program->create($input);
return Response::json(
array('success' => true)
);
} else {
$errors = $this->validator->errors();
return Response::json(
array('errors' => $errors)
);
}
}
}
this returns the json
{"errors":{}}
if I change the controller to
$errors = $this->calidator->errors()->all();
this is returned
{"errors":["The title field is required.","The focus field is required.","The desc field is required."]}
what I really want returned is
{"errors":[title: "The title field is required.",focus: "The focus field is required.",desc: "The desc field is required."]}
The Validator errors in Laravel return a MessageBag object, which has many useful methods you may want to look over.
It sounds like what you want is the toArray method, and you can use it like so in your controller.
Replace the following code that you have in your controller;
$errors = $this->validator->errors();
return Response::json(
array('errors' => $errors)
);
With;
$errors = $this->validator->errors()->toArray();
return Response::json(
array('errors' => $errors)
);
Or, depending how how you're using it with Angular, you can return the object directly, using the toJson method.
return $this->validator->errors()->toJson();

Categories