2

How can I validate an array value in Laravel 5.1? Each values of the array should be validated. Number of array values may increase or decrease. But the name will be same. My sample input will be like,

emails[] = '[email protected]',
emails[] = '[email protected]',

I gave the validation rule like,

'emails' => 'required|email'

But when I give more than one values, it returns email invalid error because the email input is array. How can I validate this? Should I write custom rule?

Based on this answer I have tried something like,

$validator->each('emails', 'required|email')

But not working.

3
  • This questions is already answered here: stackoverflow.com/questions/33368759/… Commented Jan 11, 2016 at 11:36
  • My input array format is different. I do not have any sub arrays. So I have tried like, $validator->each('emails', 'required|email'). Still not working. Commented Jan 11, 2016 at 11:54
  • you have missed the point for declaring 'emails' => 'array' in your rules.. I will show you an example in an answer Commented Jan 11, 2016 at 12:09

3 Answers 3

2

If you are trying to validate an array value.
You can use in the validation place like below,

    'email.*' => 'required|email'        

If it is an object like,

    {
      'email' : [
                  {
                    'email' : '[email protected]'
                  },
                  {
                    'email' : '[email protected]'
                  }
                 ]
    }

Then you need to use like below,

   'email.*.email' => 'required|email'

Thse are the best way of validate an array elements.

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

Comments

1

Here is a basic example of what your are looking for ( hopefully )

This is a simple blade file that just shows a form with two email fieds and a short error display:

@if($errors->any())
    @foreach($errors->all() as $error)
        <p>{{ $error }}</p>
    @endforeach
@endif

<form action="{{ route('emails.store') }}" method="POST">
    {{ csrf_field() }}
    <input name='emails[]'>
    <input name="emails[]">
    <input type="submit" name="submit">
</form>

Inside the store method of my controller I put:

public function store(Request $request)
{
    $validator = \Validator::make($request->all(), [
        'emails' => 'array',
        // your other rules here
    ]);

    $validator->each('emails', 'required|email');

    if($validator->fails()) {
        return back()->withErrors($validator->errors());
    }

    dd('success');
}

This is working as expected and validates all emails[] fields

Comments

0

You might want to try this from the documentation https://laravel.com/docs/5.2/validation#validating-arrays

1 Comment

I have tried that already by giving '*.emails' => 'required|email'. But no validation. But I think it is for laravel 5.2. I have Laravel 5.1

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.