0

Im using dynamic form input inserting array data but validation for array is not works for me. without input its saving in database as blank values.

In a table column i have inert my form input like this, adding dynamic form field im using vueJs:

<tr v-for="row in rows">
 <td>{!! Form::text('description[]',null,['class' => 'input-field input-sm','v-model'=>'row.description']) !!}
@if ($errors->has('description'))
       <span class="error"><i class="glyphicon glyphicon-warning-sign" data-toggle="tooltip" data-placement="top" title="{{ $errors->first('description') }}"></i></span>@endIf</td>
<td>{!! Form::text('log_time[]',null,['class' => 'input-field input-sm','v-model'=>'row.log_time']) !!}
      @if ($errors->has('log_time'))<span class="error"><i class="glyphicon glyphicon-warning-sign" data-toggle="tooltip" data-placement="top" title="{{ $errors->first('log_time') }}"></i></span>@endIf </td>
<td> <a @click="removeRow(row)"><button class="btn btn-danger" type="button" id="dim">
            <span class="glyphicon glyphicon-minus"></span></button> </a>
             <a @click="addRow"><button class="btn btn-success" type="button" id="dim">
              <span class="glyphicon glyphicon-plus"></span></button></a>
</td>
</tr>

My Controller store function :

protected $rules = [
        'row.description' => ['required|array'],
        'row.log_time' => ['required|array'],
    ];
public function store(PslCall $call,Request $request)
    {
        $this->validate($request, $this->rules);
        $data = array();
        foreach($request->description as $key=>$value){
            $data[]=[
                'description'=> $value,
                'log_time'=> $request->log_time[$key],
                'call_id'=>$call->id,
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now(),
            ];
        }

        PortLog::insert($data);
        return Redirect::route('calls.logs.index',$call->id)->with('message','You have successfully submitted');
    }

here you can check my dd() without input i can insert :( for this i have to give validation ;

array:2 [▼
  0 => array:5 [▼
    "description" => ""
    "log_time" => ""
    "call_id" => 2
    "created_at" => Carbon {#351 ▶}
    "updated_at" => Carbon {#352 ▶}
  ]
  1 => array:5 [▼
    "description" => ""
    "log_time" => ""
    "call_id" => 2
    "created_at" => Carbon {#353 ▶}
    "updated_at" => Carbon {#354 ▶}
  ]
]
4
  • can you assure, $request contain all of the values you want to validate? Commented Aug 30, 2016 at 6:31
  • @SafoorSafdar check question now Commented Aug 30, 2016 at 6:37
  • look like, you need remove row. from your validation rules Commented Aug 30, 2016 at 6:39
  • ya i removed but its not working Commented Aug 30, 2016 at 6:55

1 Answer 1

3

There is issue with your $rules Try that

protected $rules = [
        'description.*' => 'required|min:1',
        'log_time.*' => 'required|min:1',
    ];
Sign up to request clarification or add additional context in comments.

1 Comment

If the description field is an array like, 'description' : [ { 'value' : 'testing' } , { 'value' : 'description content'} ] It can be validated as description.* but that's not the case here I think..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.