0

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();
    }

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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!');
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.