msharaf's avatar

array validation - make sure last element of array required or not empty

Say i have two selects with the same array name

<select name="location[]">
    <option value="" selected="selected">please select</option>
    <option value="us">US</option>
    <option value="uk">UK</option>
</select>

<select name="location[]">
    <option value="" selected="selected">please select</option>
    <option value="california">california</option>
    <option value="alaska">alaska</option>
</select>

i need to make sure last location is selected does laravel validation array has some thing like that

0 likes
3 replies
edoc's avatar

@msharaf try

$this->validate($request, [
        'location.1' => 'required',
    ]);

if u want every location to be filled

$this->validate($request, [
        'location.*' => 'required',
    ]);
1 like
msharaf's avatar

@edoc thx for answer but i need last level

''' 'location.1' => 'required', ''' yes i know will validate second element of array not last one

what if i dont know the last element number say us->alaska->region1->subregion1 now it should be 'location.3' => 'required', i need to validate on the last level its selected

edoc's avatar

@msharaf

I dont know if Laravel supports such a thing

You may need to do it manually like using count()

Please or to participate in this conversation.