有没有办法在laravel的请求文件中获取messages函数的输入值?

w80xi6nr  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(133)

在我的注册表单中,我有一个bool输入newCompany,其值为
0=使用公司代码注册到现有公司1=创建新公司
在不同的字段使用required_if验证。如果失败,将抛出验证消息,如“当公司为0时,公司代码字段是必填的”。
我想将0的值设置为Existing for 0和New for 1,所以我将它们插入到我的语言文件中,并在lang/en/validation.php文件中更改默认错误消息的:value属性。

'required_if' => 'The :attribute field is required when :other is :value.'

下面是我的应用程序/请求/AuthRegisterRequest. php文件:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;
use Illuminate\Validation\Rules\File;
use Illuminate\Http\Request;

class AuthRegisterRequest extends FormRequest {
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize() {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules(Request $request) {
        return [
            'name'                => 'required|max:50|alpha',
            'surname'             => 'required|max:50|alpha',
            'email'               => 'required|email|unique:users,email',
            'password'            => [
                'required',
                Password::min(8)->numbers()->letters()->uncompromised(5)
            ],
            'c_password'          => 'required|same:password',
            'newCompany'          => 'required|boolean',
            'companyCode'         => 'nullable|required_if:newCompany,0|max:20|exists:user_companies,code',
            'companyName'         => 'nullable|required_if:newCompany,1|max:255',
            'companyPhone'        => 'nullable|required_if:newCompany,1|max:50|unique:user_companies,phone',
            'activityTaxDocument' => [
                'nullable',
                'required_if:newCompany,1',
                File::types(['jpg, jpeg, gif, png, doc, docx, pdf'])->max(5*1024) //5 MB
            ],
            'privacyPolicies' => 'required|boolean'
        ];
    }

    public function attributes() {
        return __('fieldname.AuthRegister');
    }

    public function messages() {
        return [
            'companyCode.required_if'=>__('validation.required_if', [
                'value'=>__('fieldname.AuthRegister.newCompanyValues')['0']
            ]),
            'companyName.required_if'=>__('validation.required_if', [
                'value'=>__('fieldname.AuthRegister.newCompanyValues')['1']
            ]),
            'companyPhone.required_if'=>__('validation.required_if', [
                'value'=>__('fieldname.AuthRegister.newCompanyValues')['1']
            ]),
            'activityTaxDocument.required_if'=>__('validation.required_if', [
                'value'=>__('fieldname.AuthRegister.newCompanyValues')['1']
            ])
        ];
    }
}

以及lang/en/fieldname.php文件

<?php

return [
    'AuthRegister' => [
        'name' => 'Name',
        'surname' => 'Surname',
        'email' => 'Email',
        'password' => 'Password',
        'c_password' => 'Confirm Password',
        'newCompany' => 'Company',
        'companyCode' => 'Company Code',
        'companyName' => 'Company Name',
        'companyPhone' => 'Company Phone',
        'activityTaxDocument' => 'Activity/Tax Document',
        'privacyPolicies' => 'Privacy Policy & Terms',
        'newCompanyValues' => [
            '1' => 'New',
            '0' => 'Existing'
        ]
    ]
];

它的工作,但我想合并消息的required_if验证,以缩短代码根据newCompany字段。但无法获得输入值的消息函数。
尝试:

public function messages(Request $request) {
        return [
            'required_if'=>__('validation.required_if', [
                'value'=>__('fieldname.AuthRegister.newCompanyValues')[$request->newCompany]
            ])
        ];
    }

但它抛出500以下的错误

{
    "message": "Declaration of App\\Http\\Requests\\AuthRegisterRequest::messages(Illuminate\\Http\\Request $request) must be compatible with Illuminate\\Foundation\\Http\\FormRequest::messages()",
    "exception": "Symfony\\Component\\ErrorHandler\\Error\\FatalError",
    "file": "/var/www/html/app/Http/Requests/AuthRegisterRequest.php",
    "line": 67,
    "trace": []
}

我有很多这样的表单。其中一些表单有更多的字段根据布尔字段进行更改。有没有办法在消息函数中获取输入值或从其他地方捕获它?如果没有,是否值得定义一个新规则,或者我应该为每个字段单独定义它?
谢谢。

u5rb5r59

u5rb5r591#

我认为可以使用sometimes方法而不使用message()方法

public function rules(Request $request) {
    return [
        'name'                => 'required|max:50|alpha',
        'surname'             => 'required|max:50|alpha',
        'email'               => 'required|email|unique:users,email',
        'password'            => [
            'required',
            Password::min(8)->numbers()->letters()->uncompromised(5)
        ],
        'c_password'          => 'required|same:password',
        'newCompany'          => 'required|boolean',
        'companyCode'         => 'nullable|sometimes|required_if:newCompany,0|max:20|exists:user_companies,code',
        'companyName'         => 'nullable|sometimes|required_if:newCompany,1|max:255',
        'companyPhone'        => 'nullable|sometimes|required_if:newCompany,1|max:50|unique:user_companies,phone',
        'activityTaxDocument' => [
            'nullable',
            'sometimes',
            'required_if:newCompany,1',
            File::types(['jpg, jpeg, gif, png, doc, docx, pdf'])->max(5*1024) //5 MB
        ],
        'privacyPolicies' => 'required|boolean'
    ];
}

相关问题