php - Validating dynamically added input fields in Laravel 5 -


problem: need validate dynamically added input fields.

here screenshot of ui:

enter image description here

scenario 1: adding new schedule, validation works perfectly.

the form validated using form request file:

public function rules() {     $rules = [         'name' => 'required|max:255',         'due_at' => 'required|date_format:y-m-d',         'users' => 'required',         'task_name' => 'required|max:255'     ];     if($this->request->get('task_name')){         foreach($this->request->get('task_name') $key => $val)         {             $rules['task_name.'.$key] = 'required|max:255';             $rules['task_description.'.$key] = 'required|max:255';         }     }      return $rules; }  public function messages() {     $messages = [];     foreach($this->request->get('task_name') $key => $val) {         $messages['task_name.'.$key.'.max'] = 'the field labeled task name must less :max characters.';         $messages['task_name.'.$key.'.required'] = 'the field task name required.';         $messages['task_description.'.$key.'.max'] = 'the field task description must less :max characters.';         $messages['task_description.'.$key.'.required'] = 'the field task description required.';     }     return $messages; } 

scenario 2: updating existing schedule. fails , receive following error message:

errorexception in helpers.php line 454: htmlentities() expects parameter 1 string, array given (view: /users/petestewart/documents/git repos/.../resources/views/schedules/partials/_form.blade.php) (view: /users/.../documents/git repos/.../resources/views/schedules/partials/_form.blade.php)

enter image description here

here schedule controller update function:

/**  * update specified resource in storage.  *  * @param  \app\schedule $schedule  * @return response  */ public function update(schedule $schedule, schedulerequest $request) {     $schedule->name = $request->name;     $schedule->apiary_id = $request->apiary_id;     $schedule->due_at = $request->due_at;     $schedule->update();      // attach users new schedule     $schedule->users()->sync($request->get('users'));      // save tasks     if($request->get('task_name')){         foreach($request->get('task_name') $key => $task_name)         {                //if(empty($task_name)) continue; // if task empty skip next 1                 // check if task exists , update task             if(!empty($request->task[$key])){                 $task = task::find($request->task[$key]);                 $task->name = $task_name;                 $task->description = $request->task_description[$key];                 $task->update();             }else{             // add new task                 $task = new task();                 $task->name = $task_name;                 $task->description = $request->task_description[$key];                 $task->schedule_id = $schedule->id;                 $task->save();             }         };     };     return redirect::to('schedules')->with('success', trans('messages.update', ['name' => 'schedule'])); } 

and here html form:

@if(isset($tasks))           @foreach($tasks $task)             <div class="task-item row form-group">                 {!! form::hidden('task[]', $task->id) !!}                 <a href="{{ route('schedules.task.delete', $task) }}" class="btn btn-danger btn-sm task-delete-btn" data-method="delete" data-confirm="are sure want delete task?"><span class="fa fa-trash"></span></a>                 <div class="col-sm-4 task-name">                     {!! form::label('task_name', 'task name', array('class'=>'control-label')) !!}                     {!! form::text('task_name[]', $task->name, array('class'=>'form-control')) !!}                 </div>                 <div class="col-sm-8">                     {!! form::label('task_description', 'task description', array('class'=>'control-label')) !!}                     {!! form::text('task_description[]', $task->description, array('class'=>'form-control')) !!}                 </div>             </div>         @endforeach             @else         @if(form::old('task_name'))             @foreach(old('task_name') $key => $val)                 <div class="task-item row form-group">                     <a href="#" class="btn btn-default btn-sm remove-task"><span class="fa fa-minus"></span></a>                     <div class="col-sm-4 task-name {!! $errors->first('task_name.'.$key, 'has-error') !!}">                         {!! form::label('task_name', 'task name', array('class'=>'control-label')) !!}                         {!! form::text('task_name['.$key.']', old('task_name.'.$key), array('class'=>'form-control')) !!}                         {!! $errors->first('task_name.'.$key, '<p>:message</p>') !!}                     </div>                     <div class="col-sm-8 {!! $errors->first('task_description.'.$key, 'has-error') !!}">                         {!! form::label('task_description', 'task description', array('class'=>'control-label')) !!}                         {!! form::text('task_description['.$key.']', old('task_description.'.$key), array('class'=>'form-control')) !!}                         {!! $errors->first('task_description.'.$key, '<p>:message</p>') !!}                     </div>                 </div>             @endforeach         @else             <div class="task-item row form-group">                 <a href="#" class="btn btn-default btn-sm remove-task"><span class="fa fa-minus"></span></a>                 <div class="col-sm-4 task-name">                     {!! form::label('task_name', 'task name', array('class'=>'control-label')) !!}                     {!! form::text('task_name[]', null, array('class'=>'form-control')) !!}                     {!! $errors->first('task_name', '<p>:message</p>') !!}                 </div>                 <div class="col-sm-8">                     {!! form::label('task_description', 'task description', array('class'=>'control-label')) !!}                     {!! form::text('task_description[]', null, array('class'=>'form-control')) !!}                 </div>             </div>         @endif     @endif     <a href="#" class="btn btn-default btn-sm" id="add-task"><span class="fa fa-plus"></span></a> 

and jquery responsible adding , removing tasks:

var tasklist = {      addtaskbtn: $('#add-task'),     completetaskbtn: $('.task-complete-btn'),     deletetaskbtn: $('.task-delete-btn'),     tasktemplate: '<div class="task-item row form-group"> \                         <a href="#" class="btn btn-default btn-sm remove-task"><span class="fa fa-minus"></span></a> \                         <div class="col-sm-4 task-name"> \                             <label for="task_name" class="control-label">task name</label> \                             <input class="form-control" name="task_name[]" type="text"> \                         </div> \                         <div class="col-sm-8"> \                             <label for="task_description" class="control-label">task description</label> \                             <input class="form-control" name="task_description[]" type="text"> \                         </div> \                     </div>',      init: function() {          this.binduiactions();     },     binduiactions: function() {         // add new task item         this.addtaskbtn.click(function () {             $('.task-item:last').after(tasklist.tasktemplate);             var newtask = $('.task-item:last');             newtask.find('input[type=text]:first').focus();                          return false;         });          $(document).on('click', 'a.remove-task', function (e) {             if($(this).parent().is(':first-child')){                 $(this).parent().find('input[type=text]').val('');                 return;             };             $(this).parent().remove();             return false;         });          // set task complete via ajax         this.completetaskbtn.change(function () {             var h3 = $(this).parent().parent().parent().parent().find('h3');             var csrf_token = $('meta[name="csrf-token"]').attr('content');             var url = '/schedules/update-task';             $.ajax({                 type: 'post',                 url: url,                 data: {                        id: $(this).attr('id'),                     complete: $(this).is(':checked')? 1 : 0,                     '_token': csrf_token                 },                 datatype: 'json',                 success: function (data) {                     h3.find('span').remove();                     h3.append(data.status_label);                 }             });             return false;         });         // delete task via ajax         this.deletetaskbtn.click(function () {             var taskwrapper = $(this).parent();             var csrf_token = $('meta[name="csrf-token"]').attr('content');             var url = $(this).attr('href');             if (confirm('are sure want delete task?')) {                 $.ajax({                     type: 'delete',                     url: url,                     data: {                            '_token': csrf_token                     },                     datatype: 'json',                     success: function (data) {                         taskwrapper.remove();                     }                 });             }             return false;         });     }, }; 

any validating when updating schedule appreciated. lot!

solved following way, not sure if best option works now. on update remove current tasks , add them based on form data...

changed form to:

@if(form::old('task_name'))         @foreach(old('task_name') $key => $val)             <div class="task-item row form-group">                 <a href="#" class="btn btn-default btn-sm remove-task"><span class="fa fa-minus"></span></a>                 <div class="col-sm-4 task-name {!! $errors->first('task_name.'.$key, 'has-error') !!}">                     {!! form::label('task_name', 'task name', array('class'=>'control-label')) !!}                     {!! form::text('task_name['.$key.']', old('task_name.'.$key), array('class'=>'form-control')) !!}                     {!! $errors->first('task_name.'.$key, '<p>:message</p>') !!}                 </div>                 <div class="col-sm-8 {!! $errors->first('task_description.'.$key, 'has-error') !!}">                     {!! form::label('task_description', 'task description', array('class'=>'control-label')) !!}                     {!! form::text('task_description['.$key.']', old('task_description.'.$key), array('class'=>'form-control')) !!}                     {!! $errors->first('task_description.'.$key, '<p>:message</p>') !!}                 </div>             </div>         @endforeach     @elseif(isset($tasks))           @foreach($tasks $task)             <div class="task-item row form-group">                 {!! form::hidden('task[]', $task->id) !!}                 <a href="{{ route('schedules.task.delete', $task) }}" class="btn btn-danger btn-sm task-delete-btn" data-method="delete" data-confirm="are sure want delete task?"><span class="fa fa-trash"></span></a>                 <div class="col-sm-4 task-name">                     {!! form::label('task_name', 'task name', array('class'=>'control-label')) !!}                     {!! form::text('task_name[]', $task->name, array('class'=>'form-control')) !!}                 </div>                 <div class="col-sm-8">                     {!! form::label('task_description', 'task description', array('class'=>'control-label')) !!}                     {!! form::text('task_description[]', $task->description, array('class'=>'form-control')) !!}                 </div>             </div>         @endforeach           @else         <div class="task-item row form-group">             <a href="#" class="btn btn-default btn-sm remove-task"><span class="fa fa-minus"></span></a>             <div class="col-sm-4 task-name">                 {!! form::label('task_name', 'task name', array('class'=>'control-label')) !!}                 {!! form::text('task_name[]', null, array('class'=>'form-control')) !!}                 {!! $errors->first('task_name', '<p>:message</p>') !!}             </div>             <div class="col-sm-8">                 {!! form::label('task_description', 'task description', array('class'=>'control-label')) !!}                 {!! form::text('task_description[]', null, array('class'=>'form-control')) !!}             </div>         </div>     @endif 

and update function in controller:

public function update(schedule $schedule, schedulerequest $request) {     $schedule->name = $request->name;     $schedule->apiary_id = $request->apiary_id;     $schedule->due_at = $request->due_at;     $schedule->update();      // sync users schedule     $schedule->users()->sync($request->get('users'));      // remove tasks , add new     $schedule->tasks()->delete();     // save tasks     if($request->get('task_name')){         foreach($request->get('task_name') $key => $task_name)         {                //if(empty($task_name)) continue; // if task empty skip next 1                 // // check if task exists , update task             // if(isset($request->task[$key])){             //  $task = task::find($request->task[$key]);             //  $task->name = $task_name;             //  $task->description = $request->task_description[$key];             //  $task->update();             // }else{             // add new task                 $task = new task();                 $task->name = $task_name;                 $task->description = $request->task_description[$key];                 $task->schedule_id = $schedule->id;                 $task->save();             //}         };     };     return redirect::to('schedules')->with('success', trans('messages.update', ['name' => 'schedule'])); } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -