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 Answers1

0

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');
Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24
  • I'm very sorry but none of these works for me, The validation plugin im using is https://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. – darth-dev-vader Apr 23 '21 at 06:02
  • echo $errors->first('ref_name.0'); try this i hope this will help you @darth-dev-vader – Rakesh kumar Oad Apr 23 '21 at 07:42