0

The essence of the problem is that I started to learn Laravel and I can not find information on how to pass many fields through the form, followed by validation. Form example:

How to do it right?

`<form class="mt-5" method="post" enctype="multipart/form-data"
    @isset($word)
        action="{{ route('edit.word.update', $word) }}"
    @else
        action="{{ route('edit.word.store') }}"
    @endisset
@csrf
@isset($word)
    @method('PUT')
@endisset
<table class="table table-responsive-md">
    <tbody>
        <tr>
            <th>ID</th>
            <th>English</th>
            <th>Transcription</th>
            <th>Russian</th>
            <th>Ukrainian</th>
            <th>Module</th>
            <th>Action</th>
         </tr>
         @for($i = 1; $i < 3; $i++)
            <tr>
                <td>{{ $i }}</td>
                <td><input type="text" name="eng[{{ $i }}]" class="form-control"
                                           value="{{ old('eng') }}"></td>
                <td><input type="text" name="transaction[{{ $i }}]" class="form-control"
                                           value="{{ old('transaction') }}"></td>
                <td><input type="text" name="ru[{{ $i }}]" class="form-control"
                                           value="{{ old('ru') }}"></td>
                <td><input type="text" name="uk[{{ $i }}]" class="form-control"
                                           value="{{ old('uk') }}"></td>
                <td><input type="text" name="category_id[{{ $i }}]" class="form-control"
                                           value="{{ old('category_id') }}"></td>
                <td></td>
             </tr>
           @endfor
       </tbody>
    </table>
    <a class="btn btn-secondary mt-4" href="{{ route('edit.modules') }}">Back</a>
    <button class="btn btn-success  mt-4">Save</button>
</form>`

The output is like this, but I know for sure that this is not right.

^ array:6 [▼ "_token" => "Olp8kMQIFoDP9OOvV5YRihcV3FpKIHofxfYk8W7M" "eng" => array:2 [▶] "transaction" => array:2 [▶] "ru" => array:2 [▼ 1 => "www" 2 => "qqq" ] "uk" => array:2 [▶] "category_id" => array:2 [▶] ]

EduardRST
  • 115
  • 4
  • Why is that not correct? How should it be? Do you get any errors? Is it `laravel-5` or `laravel-8`? – brombeer Apr 05 '22 at 16:25
  • @brombeer I need to send an array of data to Request for validation. – EduardRST Apr 05 '22 at 16:46
  • That output looks like it has arrays. https://stackoverflow.com/questions/42258185/how-to-validate-array-in-laravel for validating arrays – brombeer Apr 05 '22 at 16:55
  • @brombeer This article helped a lot, thanks. But now I do not know how to check the error? @error('eng[$i]') – EduardRST Apr 06 '22 at 07:12
  • Not sure either, never used it. There's [Displaying The Validation Errors](https://laravel.com/docs/8.x/validation#quick-displaying-the-validation-errors) though, which states `$errors` will always be available, you could just `{{ dd($errors) }}` in your blade to check its values. [Retrieving All Error Messages For A Field](https://laravel.com/docs/8.x/validation#retrieving-all-error-messages-for-a-field) has hints on error messages for an array. Good luck – brombeer Apr 06 '22 at 07:19

1 Answers1

0

Your array is correct, when you have 2 (or more) inputs with the same name they will send as Array.

In your example you are using name="eng[{{ $i }}]". So you are making a array, if want to go separated use this: name="eng{{ $i }}".

Voxxii
  • 369
  • 2
  • 9
  • I passed an array like (words[0]['eng'] ) to function store(WordRequest $request). But I need to validate it to next – EduardRST Apr 05 '22 at 16:51