Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Validation Message in Laravel

I need an error message that essentially says "You need to have checked at least one box in at least one multi-dropdown"

My five multi-dropdown names are; Country, County, Gor, Locauth, Parlc.

My controller so far is;

$rules = Array
(
  [country] => required_without_all:county,gor,locauth,parlc
  [county] => required_without_all:country,gor,locauth,parlc
  [gor] => required_without_all:country,county,locauth,parlc
  [locauth] => required_without_all:country,county,gor,parlc
  [parlc] => required_without_all:country,county,gor,locauth
)

$validator = \Validator::make($input, $rules);

My problem is that I cannot see a way of returning just the one rule. It returns 5 very similar worded rules;

The country field is required when none of county / gor / locauth / parlc are present.
The county field is required when none of country / gor / locauth / parlc are present.
The gor field is required when none of country / county / locauth / parlc are present.
The locauth field is required when none of country / county / gor / parlc are present.
The parlc field is required when none of country / county / gor / locauth are present.

Not brilliant! Is there a way to return just one custom message?

--- EDIT ---

I should add... The Array() code above is not the actual code, that was a print_r result of the actual code which creates those rules. I just thought it would make it easier to read and understand my problem. The actual code, if you're interested is this;

$fields = ['country','county','gor','locauth','parlc'];
    $rules = [];
    foreach ($fields as $i => $field) {
        $rules[$field] = 'required_without_all:' . implode(',', array_except($fields, $i));
    }

--- ANOTHER EDIT ---

I already know about custom error messages like;

$messages = [
'required' => 'The :attribute field is required.',
];

$validator = Validator::make($input, $rules, $messages);

But this will just give me five error messages instead of only one.

like image 388
mikelovelyuk Avatar asked Apr 23 '15 15:04

mikelovelyuk


People also ask

What is the method used for specifying custom messages for validator errors in form request?

After checking if the request failed to pass validation, you may use the withErrors method to flash the error messages to the session. When using this method, the $errors variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user.

How do you validate exact words in laravel?

To get the exact words to validate you can make use of Rule::in method available with laravel. Using Rule::in method whatever the values provided by this rule has to be matched otherwise it will fail.

How many types of validation are there in laravel?

Each form request generated by Laravel has two methods: authorize and rules .


1 Answers

You only need the rule on one field for it to work correctly. The easiest way is to create an empty field and apply the rule to it. The field doesn't have to exist in the database schema or anywhere else in your application.

public static $rules = [
    location => required_without_all:country,county,gor,locauth,parlc
];

Then customize the message in your language file.

'custom' => array(
    'location' => array(
        'required_without_all' => 'At least one location field is required',
    ),
),

Now when all of the fields (or check boxes in your case) are empty, you will receive the error message. If one or more are filled in, there won't be any error.

like image 87
Jon Slabaugh Avatar answered Sep 23 '22 14:09

Jon Slabaugh