1

I try to validate in array by using 'id_s.*' => 'required', not sure why t didnt work

UPDATED I add my jquery append , in my controller I use loop which I want to require id_s

In my html

   <select id="custom-select" class="form-control custom-select" name="id_s[]" >
    <option disabled="true" selected="true" name="id_s[]" value="choose">choose</option>
    @foreach($subject as $s)
  <option value="{{$s->ID}}" name=id_s[]>{{$s->NAME}} / {{$s->CREDIT}} </option>
  @endforeach
</select>



$(document).ready(function() {
    $('#add-form').click(function() {
        i++;
        id_i++;                     
        $('#add-me').append(
            '<tr>'+
            '<td>'+
            '<select id="custom-select" name="id_s[]" class="form-control custom-select"><option disabled="true" selected="true" name="id_s[]" value="choose">choose</option>@foreach($subject as $s)<option value="{{$s->ID}}" name=id_s[]>{{$s->NAME}} / {{$s->CREDIT}}</option>@endforeach</select>'
            +'</td>'
            +'<td>'
            +'<button id="'+i+'" type="button" class="btn btn-danger delegated-btn">Delete</button>'
            +'</td>'
            +'</tr>'
            );

        $('button.btn.btn-danger').click(function() {
            var whichtr = $(this).closest("tr");
            whichtr.remove(); 
        });

    });
});
6
  • Can't you just do 'id_s' => 'required' ? Commented Oct 25, 2017 at 15:42
  • Can you elaborate on the issue you are having? Commented Oct 25, 2017 at 15:45
  • id_s working only 1st item Commented Oct 25, 2017 at 15:46
  • I want to validate id_s[] in an array but after I use 'id_s.*' => 'required' it skip my validate Commented Oct 25, 2017 at 15:47
  • What do you mean 1st item? It's an array, id_s is only one item, it just contains other data. Can you clarify the issue? Commented Oct 25, 2017 at 15:48

1 Answer 1

2

For the select you will not need to do array in the name and you don't need to add name to the options as well :

<select id="custom-select" class="form-control custom-select" name="id_s" >
    <option disabled="true" selected="true" value="choose">choose</option>
    @foreach($subject as $s)
        <option value="{{$s->ID}}" >{{$s->NAME}} / {{$s->CREDIT}}</option>
    @endforeach
</select>

and the validation like this :

'id_s'  => 'required|not_in:choose'
Sign up to request clarification or add additional context in comments.

4 Comments

you mean $subject ?? if yes ==> then yes it will
The best choice is to name evry select with a different name then add a rule for the second one :)
I try to add value ="choose" but I use name="id_s[]" It work only 1 st data If I add 1 st data and my 2data is empty It skip my validate

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.