validation - Laravel 5 dynamic form validatioin -
i have form has fields can added dynamicly. works fine, when add fields , submit , make mistake on purpose , validation fails, dynamicly created fields disappear.
let's have 3 static fields , 1 dinamicly added. looks that
static fields: keyword1, keyword2, keyword3
dynamic fields: keyword4
the validation error message this:
the keyword0 field required. keyword1 field required. keyword2 field required. keyword3 field required.
fields 0,1,2 in the form values filled them, field 3 not that.
how can access field can print in 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 $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 request class model (form)
/** * validation rules apply request. * * @return array */ public function rules() { $rules = [ 'date' => 'required|date|unique:keywords_plans,date', 'approved' => 'boolean' ]; ($i = 0; $i < count($this->keyword); $i++) { $rules['keyword'.$i] = 'required'; } return $rules; }
my controller
<?php namespace app\http\controllers; use illuminate\http\request; use app\http\requests; use app\keyword; use app\company; use app\http\controllers\controller; use app\keywordsplan; use app\http\requests\createkeywordsplan; use app\http\requests\updatekeywordsplan; class keywordsplancontroller extends controller { public function __construct() { $this->middleware('auth'); } /** * display listing of resource. * * @return response */ public function index() { $keywordsplans = keywordsplan::all(); return view('keywordsplans/index', compact('keywordsplans')); } /** * show form creating new resource. * * @return response */ public function create($company) { $data['keywordsplan'] = new keywordsplan; $data['company'] = company::find($company); $data['keywords'] = $data['company']->keywords; return view("keywordsplans/create", $data); } }
there few mistakes in ur code @ moment.
1) rules.
it needs be
$rules['keyword.'.$i] = 'required';
ur checking 'keyword0'. u need checking 'keyword.0'. u need '.'.
laravel's validatior uses array.index when needs check array['index']'s value.
u can add 'keyword.*' = 'required'
rules array instead of loop there. (available in 5.2. not sure previous versions.)
2) when ur repopulating form after errors u need repopulating using values session. ur using $keywords regardless of wether fresh form or repopulated one.
u need this
@if (input::old('keywords')) @foreach (input::old('keywords') $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 @else @foreach ($keywords $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 @endif
and non-dynamic form values u need use old helper way.
{!! form::date('date', input::old('date'), $date, ['class' => 'form-control']) !!}
check these links know more stuff. https://laravelcollective.com/docs/5.2/html#form-model-binding. note priority order
Comments
Post a Comment