简体   繁体   中英

Validate arrray name[] input with values Laravel

Good afternoon/evening/night

I am trying to validate from my Laravel controller an empty name array.

In this case it is the following code in blade.php:

<input type="text" name='ambito[]' placeholder='Ambit name' class="form-control @error('ambito[]') is-invalid @enderror"/>

And my controler has the next code:

$validator = Validator::make($request->all(), [
    'ambito' => 'Required|array|min:2',
    'ambito.*' => 'Required|min:3'

]);

I tried this too:

$data = $request->validate([
     "ambito"  => 'required|array|min:1',
     'ambito.*' => 'required|string'
]);

But I don't get the error from the form. I have searched a lot and the truth is that the options I have seen have not worked for me.

Output to dd($request['ambito'])

array:3 [▼
  0 => "test1"
  1 => "test2"
  2 => "testasfd"
]

Thanks to all!

I've fixed it guys! The problem was not the Laravel validate but the HOW to return the message in the blade.php.

<input type="text" name='ambito[]' placeholder='Nom àmbit' class="form-control @error('ambito[]') is-invalid @enderror"/>
@error('ambito[]')
<span class="invalid-feedback d-block" role="alert">
    <strong>{{$message}}</strong>
</span>
@enderror

Code fixed:

<input type="text" name='ambito[]' placeholder='Nom àmbit' class="form-control @error('ambito') is-invalid @enderror"/>
@error('ambito')
<span class="invalid-feedback d-block" role="alert">
    <strong>{{$message}}</strong>
</span>
@enderror

The problem is @error('ambito[]<--') it doesnt work with []

Thanks to all guys!!!

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