I use the following class to validate incoming request:
class CreateInvoiceRequest extends FormRequest
{
public function authorize(Request $request)
{
return true;
}
public function messages()
{
return [
'name.required' => 'The name is required.',
'name.string' => 'The name should be a string',
'code.required' => 'Code',
'code.string' => 'Code',
'type.required' => 'Type'
];
}
public function rules()
{
return [
'name' => 'required|string',
'type' => 'string',
'code' => ['required', 'string', new InvoiceCode],
];
}
}
In fail case it returns data in {errors: "The given data was invalid."} object and http status 200.
How and where to change this status? More global question, how to handle errors and warnings in Laravel?
You can add an HTTP status to the ValidationException thrown in the failedValidation method of FormRequest:
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;
class CreateInvoiceRequest extends FormRequest
{
// ...
protected function failedValidation(Validator $validator)
{
throw (new ValidationException($validator))
->status(500)
->errorBag($this->errorBag)
->redirectTo($this->getRedirectUrl());
}
// ...
}
Related
I have these request file to register a new customer, I want to be the response json when the validation fails, how can I do that?
This is the request class
<?php
namespace App\Http\Requests\Validations;
use App\Http\Requests\Request;
class RegisterCustomerRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required|min:3|max:255',
'email' => 'required|email|max:255|unique:customers',
'password' => 'required|string|min:6|confirmed',
'agree' => 'required',
];
}
}
And the registration function:
public function register(RegisterCustomerRequest $request)
{
$customer = Customer::create([
'name' => $request->name,
'email' => $request->email,
'password' => $request->password,
'accepts_marketing' => $request->subscribe,
'verification_token' => Str::random(40),
'active' => 0,
]);
// Sent email address verification notich to customer
$customer->notify(new EmailVerificationNotification($customer));
$customer->generateToken();
event(new Registered($customer));
return new CustomerResource($customer);
}
in your RegisterCustomerRequest you can override default method failedValidation as below
protected function failedValidation(Validator $validator)
{
if ($this->ajax()){
throw new HttpResponseException($this->respondWithError(419,'VALIDATION_ERROR',$validator->errors()));
} else{
throw (new ValidationException($validator))
->errorBag($this->errorBag)
->redirectTo($this->getRedirectUrl());
}
}
also you can write your own response inside $this->ajax() like below
if ($this->ajax()){
return response()->json($validator->errors(),419);
}
I am having problems displaying custom error messages.
I received a training project that had the following code:
class StoreProject extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required|unique:projects,name|max:255',
'website' => 'url',
];
}
public function messages()
{
return [
'name' => 'Це імʼя вже використовується',
'website' => 'Будь-ласка введіть адресу вашого сайту вірно http://...'
];
}
}
I added the function message( ) myself.
This is the controller code:
public function store(StoreProject $request)
{
$project = new Project($request->except('project_image'));
$project->owner_id = Auth::user()->id;
$project->status_id = StatusProject::UNCONFIRMED;
//send email to moderator and accountant for the moderation
if( $project->save() ) {
$this->dispatch(new ConfirmNewProject($project));
}
// load image from cropie serves
if ($request->has('project_image')) {
$file = self::croppie($request->input("project_image"));
$project->uploadImage($file, 1);
}
return redirect()->route('projects.show', [$project->id]);
}
I tried various methods: withErrors([]) and this method:
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
'name' => 'Це імʼя вже використовується',
'website' => 'Будь-ласка введіть адресу вашого сайту вірно http://...'
],
]
but when checking, I get the key value, not the text of the error message
Errors:
validation.unique
validation.url
How to transfer the rule and message to the controller?
Try to modify the messages() function like that one:
public function messages()
{
return [
'name.required' => 'Name required message',
'name.unique' => 'Name unique message',
'name.max' => 'Name max message',
'website.url' => 'Будь-ласка введіть адресу вашого сайту вірно http://...'
];
}
I have custom validation rule appointment_status. I am performing various test cases on it and decide what error message is best and throwback. it will be different for every case. I want the $validator->errors()->add('status', __('Invalid status for an appointment in past') to set the error message and it's adding. but it's not returning back to the controller. I can't access this message anywhere. it shows only the status.appointment_status one which is set in messages() function.
Custom Request class:
namespace Modules\ShopManager\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class AppointmentsRequest extends FormRequest
{
public function __construct()
{
\Validator::extend('appointment_status', 'Modules\ShopManager\Validators\CustomValidator#appointmentStatus');
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
$rules = [
'services' => 'required',
'sdate' => 'required|date_format:m-d-Y|time_range:sTime,edate,eTime,timezone',
'edate' => 'required|date_format:m-d-Y|workinghours_range:sdate,sTime,edate,eTime,timezone',
'sTime' => 'required|date_format:h:i a',
'eTime' => 'required|date_format:h:i a',
'cname' => 'required',
'cphone' => 'required_without:cemail',
'cemail' => 'nullable|required_without:cphone|email',
'timezone' => 'required',
'status' => 'required|appointment_status:sdate,sTime,edate,eTime,timezone',
];
return $rules;
}
public function messages()
{
return [
'status.appointment_status' => 'Invalid status'
];
}
public function attributes()
{
return [
'services' => 'Services',
'date' => 'Date',
'sTime' => 'Start Time',
'eTime' => 'End Time',
'cname' => 'Customer name',
'cphone' => 'Customer phone',
'cemail' => 'Customer email',
'internal_note' => 'Internal note',
'status' => 'Status',
];
}
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
}
The custom validator function:
public function appointmentStatus($attribute, $value, $parameters, $validator)
{
$dateTimeOperations = new DateTimeOperations;
$sdate = array_get($validator->getData(), $parameters[0]);
$startTime = array_get($validator->getData(), $parameters[1]);
$edate = array_get($validator->getData(), $parameters[2]);
$endTime = array_get($validator->getData(), $parameters[3]);
$timezone = array_get($validator->getData(), $parameters[4]);
$now = $dateTimeOperations->getNow($timezone);
$start = $dateTimeOperations->getTimestamp($sdate, $startTime, $timezone);
$end = $dateTimeOperations->getTimestamp($edate, $endTime, $timezone);
switch ($value) {
case constants('appointments.status.pendig'):
$start->lessThan($now)
? $validator->errors()->add('status', __('Invalid status for an appointment in past'))
: '';
}
return $validator->errors()->any();
}
Adding an error just to the field without specifying the rule I don't think will work, that's why the message from the validation request takes precedence.
So change it to this:
$validator->errors()->add('status.appointment_status', __('Invalid status for an appointment in past'))
And also in your case do you maybe have a typo: pendig to be pending?
You have to create custom validator rules and add below code inside your rule wherever required, See example below:
$validator->after(function ($validator) {
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
});
How to adopt FormRequest to API JSON request? Now it looks as:
class CreateOrder extends FormRequest
{
// Validation rules
public function rules()
{
return [
'DeliveryDate' => 'required|date_format:d-m-Y H:m:s',
'PostCard' => 'boolean',
'PostCardText' => 'string|max:250',
'PhotoBefore' => 'boolean',
'Remark' => 'string|max:200',
'OrderDate' => 'required|date_format:d-m-Y',
'CallRecipientBefore' => 'boolean',
'AnonymousDelivery' => 'boolean',
'PhotoAfter' => 'boolean',
'Employers_Id' => 'required|integer',
'Employers_Id' => 'required|integer',
'Partners_Id' => 'required|integer',
];
}
public function authorize()
{
return true;
}
}
And using:
public function store(CreateOrder $request)
{
dd(Request::get('DeliveryDate'));
dd($request->all()); // Or
}
When I send data to this endpoint, Laravel instead showing incoming data $request, does redirect.
Sending data
Using Postman data is sent in raw format as:
{
"DeliveryDate": "2018-01-01"
}
As result I give Laravel screen saver.
So, I have did some tests, method is not called at all:
public function store(CreateOrder $request)
{
dd('ss');
}
Seems an error in class: CreateOrder
This is my form request code, i want to add new variable after validation success, so i can access that variable at my controller :
class CouponRequest extends Request
{
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'start_year' => 'required',
'start_month' => 'required',
'start_day' => 'required',
'start_time' => 'required',
'finish_year' => 'required',
'finish_month' => 'required',
'finish_day' => 'required',
'finish_time' => 'required',
];
}
public function afterValidation()
{
$this->start_date = Carbon::create( $this->start_year, $this->start_month, $this->start_day );
}
}
So after validation has no error, i can call this instance at my controller :
$request->start_date;
Could i do this?
All above methods work but in my opinion I would override the passedValidation method in the form request class. This method is called after the validation checks are passed and hence keep the data clean.
Ex.
public function passedValidation()
{
$this->merge([
'start_date' => Carbon::create( $this->start_year, $this->start_month, $this->start_day )
]);
}
If you dump the data now you should see your new start_date value as well.
You could do this
public function afterValidation()
{
$this->request->add([
'start_date' => Carbon::create($this->start_year, $this->start_month, $this->start_day)
]);
}
public function validate()
{
parent::validate();
$this->afterValidation();
}
And then access the attribute in your controller as
$request->get('start_date');
In your form request use function prepareForValidation()
protected function prepareForValidation(): void
{
$this->merge([
'start_date' => Carbon::now()
]);
}
Cheers!
I am using this method after success request for manipulations.
Source: 50116187/1101038
public function withValidator(Validator $validator)
{
if ( $validator->fails() ) {
\Log::info('Error! No Manipulation!');
}else{
$this->merge([
'custom' => 'Test Manipulation!'
]);
\Log::info('Success Manipulation!');
}
}
I made the validation in the Controller. The method has a "Request $request" parameter. I have a I do this:
$input = $request->all();
$input['my_new_field] = 'the_data';