How to add a custom Laravel error message when using Rule::in as validation

  laravel, php, validation

I am validating a select dropdown in Laravel with an array of agency names. I am using Rule::in() to do this. The validation works well and I am getting the standard error message back ‘The selected agency-name is invalid’. I want to edit this into a custom error message but am having difficulty doing this the same way as I have before. Here is my code.

$agencies = Session::get('');
    $agency_names = array();
    for ($x = 0; $x < count($agencies['Agencies']); $x++) {
        $name = $agencies['Agencies'][$x]["AgencyName"];
        array_push($agency_names, $name);
        array_push($agency_names, '');

        'referral'    => 'required',
        'agency-name' => ['required_if:referral,no', Rule::in($agency_names)],
        'password'    => 'required|min:6|regex:/[A-Z]/|regex:/[a-z]/|regex:/[0-9]/|confirmed'], [
        // New custom agency name message
        agency-name.Rule::in(agency_names) => 'NEW MESSAGE (DOESN'T WORK)',
        // Custom password messages.
        'password.confirmed' => 'Confirmation password did not match, please try again.',
        'password.regex'     => 'Password does not meet criteria, Please try again.',
        'password.min'       => 'Password does not meet criteria, please try again.',

Source: Laravel

Leave a Reply