I have a form which has fields that can be added dynamicly. Everything works fine, but when I add some fields and submit and I make some mistake on purpose and validation fails, the dynamicly created fields disappear.
Let's say that I have 3 static fields and one dinamicly added. so it looks like that
static fields: keyword1, keyword2, keyword3
dynamic fields: keyword4
The validation error message that I get is like this:
The keyword0 field is required.
The keyword1 field is required.
The keyword2 field is required.
The keyword3 field is required.
fields 0,1,2 are in the the form with the values that I filled for them, but field 3 is not that.
How can I access this field so I can print it in my form?
my form
{!! Form::model($keywordsPlan, ['route' => ['keywordsplans.store', 'company' => $company], 'method' => 'POST', 'class' => 'form-horizontal keywords-plan-form']) !!}
<div class="row">
<div class="col-md-4 col-md-offset-4" style="margin-bottom: 20px;">
{!! Form::label('Date') !!}
{!! Form::date('date', null, ['class' => 'form-control']) !!}
</div>
<table class="table table-stripped">
<thead>
<tr>
<th>Add Keyword</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr>
<td scope="row">{!! Form::text(null, null, ['class' => 'form-control keyword-input']) !!}</td>
<td scope="row"><a href="#" class="btn btn-primary add-keyword">Add keyword</a></td>
</tr>
</tbody>
</table>
<table class="table table-striped keywords-table">
<thead>
<tr>
<th>Keyword</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach ($keywords as $i => $keyword)
<tr class="keyword-row">
<td scope="row">{!! Form::text('keyword['.$i.']', $keyword->text, ['class' => 'form-control']) !!}</td>
<td class="action"><input type="button" class="btn btn-danger remove-keyword" value="Remove"></td>
</tr>
@endforeach
<tr class="keyword-copy-row hidden">
<td>{!! Form::text(null, null, ['class' => 'form-control']) !!}</td>
<td class="action"><input type="button" class="btn btn-danger remove-keyword" value="Remove"></td>
</tr>
</tbody>
</table>
<div class="col-md-4 col-md-offset-4 text-center">
{!! Form::submit('Create Plan', ['class' => 'btn btn-success btn-lg save-plan']); !!}
</div>
</div>
{!! Form::close() !!}
and this is my Request class for this model (form)
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
'date' => 'required|date|unique:keywords_plans,date',
'approved' => 'boolean'
];
for ($i = 0; $i < count($this->keyword); $i++) {
$rules['keyword'.$i] = 'required';
}
return $rules;
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire