0

My goal here is when I hit the submit button, all fields will validate and will automatically display a validation error below of each input fields, for now when I try submitting the form without putting any data to the fields, only the first input field has an error message displaying below of the input field and the other fields does not have only after I submit the form and then I click the input fields the message will show.

I tried to look the solution here but unfortunately it doesn't work for me.

I have a form that looks like this

<div class="form-group">
  <input type="text" name="ref_name[]"> 
</div>
<div class="form-group">
  <input type="text" name="ref_name[]"> 
</div
<div class="form-group">
  <input type="text" name="ref_name[]"> 
</div
<div class="form-group">
  <input type="text" name="ref_name[]"> 
</div
<div class="form-group">
  <input type="text" name="ref_name[]"> 
</div>

This is the rule/code on my request class

 'ref_name' => 'array',
 'ref_name.*' => 'required|distinct'

I'm using this https://packagist.org/packages/proengsoft/laravel-jsvalidation as my validation plugin on my laravel project.

The laravel version project im working on is 5.2

1
  • The first thing is that you can't use the same "name" attribute for the input fields. Commented Apr 23, 2021 at 5:49

1 Answer 1

1

To get the first validation error for an input array:

{{ $errors->first('ref_name.*') }}

To check if there is an error within an input array:

@if($errors->has('ref_name.*'))
<h1>There is an error in your input array</h1>
<ul>
   @foreach($errors->get('ref_name.*') as $errors)
       @foreach($errors as $error)
           <li>{{ $error }}</li>
       @endforeach
   @endforeach
</ul>
@endif

Other examples:

@error('ref_name.*')
<div class="alert alert-danger">{{ $message }}</div>
@enderror

also you can get error :

echo $errors->first('ref_name.0');
Sign up to request clarification or add additional context in comments.

2 Comments

I'm very sorry but none of these works for me, The validation plugin im using is packagist.org/packages/proengsoft/laravel-jsvalidation the page is not needed to refresh when the form is validating. If i submit the form it will automatically validate the form and display all errors below of the input fields without refreshing the page.
echo $errors->first('ref_name.0'); try this i hope this will help you @darth-dev-vader

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.