1

At my registration form, i have a bool input as newCompany with values

0=register to existed company with company code 1=create new company

Using required_if validation at different fields. And if it fails throws a validation message like "The Company Code field is required when Company is 0."

I wanna set that 0 value as Existing for 0 and New for 1. So i'm inserted them to my language file and changed :value attribute for default error message at lang/en/validation.php file.

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

Here's my app/Requests/AuthRegisterRequest.php file:

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

And lang/en/fieldname.php file

<?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'
        ]
    ]
];

It's working but i wanna combine message for required_if validation to shorten the code according to newCompany field. But couldn't get input values at messages function.

Tried:

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

But it throws 500 with error below

{
    "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": []
}

I have a lot of forms like this. Some of them have much much more fields changing according a bool field. Is there a way to get input values at messages function or capture it from somewhere else? if not, is it worth to defining a new rule, or should i just define it for each field separately?

Thanks.

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85

1 Answers1

0

I think you can use sometimes method without message() method

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'
    ];
}
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85