louis_coetzee
louis_coetzee

Reputation: 393

Array field name validation issue

I have fields with array field names and I am having issues validating the fields. An example is: user[first_name]

$this->validate(request(), [
    'user[first_name]' => 'required|min:15',
]);

if ($errors->any()) {
  print_r($errors->all());exit;
}

It seems that Laravel does not detect the field this way, I get user[first_name] is required.

Help will be appreciated.

Upvotes: 1

Views: 36

Answers (2)

kunwar97
kunwar97

Reputation: 825

replace user[first_name] with user.first_name

$this->validate(request(), [
    'user.first_name' => 'required|min:15',
]);

Upvotes: 2

dparoli
dparoli

Reputation: 9161

You should try to retrieve the user object from the request, e.g.:

$this->validate(request('user'), [
    'first_name' => 'required|min:15',
]);

Or even better using dot notation:

$this->validate(request(), [
    'user.first_name' => 'required|min:15',
]);

Upvotes: 1

Related Questions