2

I am validating a form using Laravel 5.2

using validation check required_if for input youtube-embed. validation takes place successfully but the custom error message does not work. Here is the code maybe some one can take a look.

$messages = [
        'youtube-embed.required_if' => 'Please paste in your youtube embed code',
    ];

$this->validate($request, [
        'youtube-embed'      => 'required_if:youtube,on',
    ]);

This is the error message that laravel is returning instead of my custom error:

The youtube-embed field is required when youtube is on.

3 Answers 3

4

Try this:

$this->validate($request, 
    ['youtube-embed'      => 'required_if:youtube,on',], 
    ['required_if' => 'Please paste in your youtube embed code',]
);

Basically you can pass your custom messages as third parameter in validate function.

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

1 Comment

Worked! Thank you very much! Was trying to fix this for an hour before I posted this question. Cheers!
3
/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    $rules = [
        'payout_method' => 'required:in,bankTransfer,paypal',
        'paypal_email'  => 'required_if:payment_type,paypal|email',
        'receiver'      => 'required_if:payment_type,bankTransfer|max:45',
        'iban'          => 'required_if:payment_type,bankTransfer|max:45|iban',
        'bic'           => 'required_if:payment_type,bankTransfer|max:15|bic',
    ];

    return $rules;
}

/**
 * Get the error messages for the defined validation rules.
 *
 * @return array
 */
public function messages()
{
    return [
        'payout_method.required'   => trans('validation.attributes.payout_type_required'),
        'paypal_email.required_if' => trans('validation.attributes.paypal_email_required'),
        'paypal_email.email'       => trans('validation.attributes.paypal_email_invalid'),
        'iban.required_if'         => trans('validation.attributes.bank_account_required'),
        'bic.required_if'          => trans('validation.attributes.bank_swift_required'),
        'iban.iban'                => trans('validation.attributes.bank_account_invalid'),
        'bic.bic'                  => trans('validation.attributes.bank_swift_invalid'),
    ];
}

what i can do in this case? Laravel return 'The paypal email field is required.' for other fields this work, only not work with required_if

Comments

0

If your'e using the Request class:

public function messages()
{
    return [
        'youtube-embed.required' => 'Your custom message here.',
    ];
}

Works on Laravel 7

1 Comment

Looks like as of Laravel 8, using required_if works as expected, and alternatively will not fall back on required as mentioned previously:

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.