Custom validation message in Laravel

I need an error message that says: "You need to check at least one field in at least one multi-page window"

My five names with a few drop-down names; Country, County, Gor, Locauth, Parlc.

My controller so far:

$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 see no way to return only one rule. It returns 5 very similar formulated 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 only one user message?

--- EDIT ---

I have to add ... The code Array()above is not the actual code that was the result of the print_r of the actual code that creates these rules. I just thought it would make reading and understanding my problem easier. Actual code, if you're interested,

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

--- ---

:

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

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

, .

+4
3

, . - . - .

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

.

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

, ( ) , . , .

+4
if(!valiate){
  return redirect()->back()->withError('Some Fields are Required');
}
0
$messages = [
    'required_without_all' => 'The :attribute field is required.',
];

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

Source: https://habr.com/ru/post/1584312/


All Articles