简体   繁体   中英

How to all array inputs in laravel and displaying the validation error below of the fields

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

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM