1

My view blade is like this :

@foreach($reviews as $key => $review)
    ...
    <div class="form-group">
        {!! Form::open(['route' => 'message.review.update', 'method' => 'post', 'id' => 'reviewform']) !!}
            <label for="review" class="sr-only">Review</label>
            {!! Form::textarea('review', null, ['class' => 'form-control', 'id' => 'review', 'rows'=>3,'required'=>'required']) !!}
            @if ($errors->has('review'))
                <span class="help-block">
                    <strong>{{ $errors->first('review') }}</strong>
                </span>
            @endif
            {!! Form::hidden('id', $review->_id) !!}
            {!! Form::hidden('store', $review->store_id) !!}
        {!! Form::close() !!}
    </div>
    ...
@endforeach

My routes is like this :

Route::group(['prefix' => 'message','as'=>'message.'],function(){
    Route::post('review/update', ['as'=>'review.update','uses'=>'ReviewController@update']);
});

My controller to update is like this :

public function update(CreateReviewRequest $request)
{       
    $param = $request->only('id', 'review', 'store');
    ...
}

Before update, it will call CreateReviewRequest to validation

My CreateReviewRequest is like this :

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CreateReviewRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }
    public function rules()
    {
        return [
            'review'=>'required|max:300'
        ];
    }
}

When, only one data, I input comment and submit, it works

But, when more than one data, it does not works

There exist error like this :

An invalid form control with name='review' is not focusable.

How can I solve it?

samuel toh
  • 6,836
  • 21
  • 71
  • 108

2 Answers2

1

VIEW

 {!! Form::textarea('review[]', null, ['class' => 'form-control', 'id' => 'review', 'rows'=>3,'required'=>'required']) !!}

or

<textarea name="review[]" class="form-control" id="review" rows="3" required></textarea>

You must insert name field like array empty, when you will submit the form you can get all values in your controller calling:

CONTROLLER

public function update(Request $request)
{       
    // Validate the form
    $this->validate($request, [
        'review' => 'required',
    ]);
    // get inputs
    $review_input = $request->get('review');
    dd($review_input); // see if it work
}
Diego Cespedes
  • 1,353
  • 4
  • 26
  • 47
  • If I have 3 data. The first data works. But The second and third data do not work. There exist error like this : `An invalid form control with name='review[]' is not focusable.` – samuel toh Mar 23 '17 at 11:40
  • Try to delete validation, like this: public function update(Request $request){ . . . then do a test. – Diego Cespedes Mar 23 '17 at 12:51
  • I updated my answer, maybe the error is "max:300" in the validation, the field is array and not a single data. – Diego Cespedes Mar 23 '17 at 12:56
1

You made some mistakes here... In HTML IDs MUST be unique. You can't put "review" as id for all your textareas... My comment was about to use an array as name like this name=review[], and in the ID add dynamically the $key at the end of the id to make them unique.

This will give you something like this :

<textarea name="review[]" class="form-control" id="review0" rows="3" required></textarea>
<textarea name="review[]" class="form-control" id="review1" rows="3" required></textarea>
<textarea name="review[]" class="form-control" id="review2" rows="3" required></textarea>

EDIT

I've just founded this stackoverflow topic. Could you try to add novalidate attribute in the <form>?

<form name="myform" novalidate>

Something like this in your case :

{!! Form::open(['route' => 'message.review.update', 'method' => 'post', 'id' => 'reviewform', 'novalidate']) !!}
Community
  • 1
  • 1
JoDev
  • 6,633
  • 1
  • 22
  • 37