mikelovelyuk
mikelovelyuk

Reputation: 4152

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.

Upvotes: 5

Views: 2149

Answers (3)

Jon Slabaugh
Jon Slabaugh

Reputation: 76

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.

Upvotes: 6

Pasindu Jay
Pasindu Jay

Reputation: 4510

if(!valiate){
  return redirect()->back()->withError('Some Fields are Required');
}

Upvotes: 0

Emeka Mbah
Emeka Mbah

Reputation: 17553

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

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

Upvotes: -1

Related Questions