使用$ validator-> errors-> add()从自定义验证器函数内部的Laravel自定义错误消息

问题描述 投票:0回答:2

我有自定义验证规则appointment_status。我正在对它执行各种测试用例,并确定哪些错误消息是最好的和回归。每个case都会有所不同。我希望$validator->errors()->add('status', __('Invalid status for an appointment in past')设置错误消息并添加。但它没有返回控制器。我无法在任何地方访问此消息。它只显示在status.appointment_status函数中设置的messages()

自定义请求类:

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

自定义验证器功能:

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();
    }
laravel validation custom-errors
2个回答
1
投票

仅在字段中添加错误而不指定我认为不会起作用的规则,这就是验证请求中的消息优先的原因。

所以改成它:

$validator->errors()->add('status.appointment_status', __('Invalid status for an appointment in past'))

在你的情况下,你可能有一个错字:pendigpending


0
投票

您必须创建自定义验证器规则,并在需要的地方在规则中添加以下代码,请参阅下面的示例:

$validator->after(function ($validator) {
    if ($this->somethingElseIsInvalid()) {
        $validator->errors()->add('field', 'Something is wrong with this field!');
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.