5

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.

6
  • Check the out documentation laravel.com/docs/5.0/validation#custom-error-messages. It is possible to set a per field validation message. Commented Apr 23, 2015 at 15:36
  • Will it not just print out 5 custom errors messages though? I only want one to capture all five... Commented Apr 23, 2015 at 15:38
  • why are you using foreach ? Its better you post the whole code Commented Apr 23, 2015 at 15:46
  • I don't get it, if you just want one error message why do you have 5 rules? why don't you create one validation rule that tests your five inputs? Instead of validating each one manually? Commented Apr 23, 2015 at 16:00
  • Because I have to compare each field against the other 4. I thought the best way to do that is a required_without_all. You cannot do that in just one rule - can you? You have to do that 5 times. Commented Apr 23, 2015 at 16:14

3 Answers 3

6

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.

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

Comments

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

2 Comments

what is wrong with this answer, please add a comment if you thumb down.
spelling mistake I think :p
-1
$messages = [
    'required_without_all' => 'The :attribute field is required.',
];

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

1 Comment

As I've already said - I will update my question though - this will still output FIVE error messages and I only want one. Thanks though

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.