0
votes

I've made a request validation which utilizes laravel 5.2's ability to easily validate multidimensional arrays in forms. In my custom validation request i've overridden the default rules and response methods like so:

The rules method loops through the form input, a single rule in the loop is validated something like this (slightly simplified):

$rules['from' . '.' . $field->name . '.*'] = required;

As you can see it is an array with two sublayers, an example of such a form value is:

"from" => array:4 [▼
    "collection1" => array:4 [▼
      0 => "some_value"
      1 => "some_other_value"

The validation itself works like a charm, but returning the input to the form does not. In the response method the optional withinput() parameter gives an array to string conversion: "htmlentities() expects parameter 1 to be string, array given"

I'm not sure what withinput exactly does, and how it tries to put back the data in the correct fields. What should my form look like to comply with the withinput method? I can understand it has a hard time with a form which has form field with names like this:

from[collection1][]
from[collection1][]
to[collection1][]
to[collection1][]

Any suggestions?

2

2 Answers

1
votes

There are a problem with laravel form helper witch named as array, please try to change the inputs to regular form inputs that will help

example : change this

    {!!form::text('name[]',null,['class'=>'form-controll']!!}

into this

<input type='text' name='name[]' class='form-controll' />
0
votes

Yeah! Found out how to fix it. Laravel needs to be able to identify the fields to put the input back in. With multiple fields named from[collection1][] it is not able to.

The solution itself makes sense, though my way might not be the most elegant because it requires php in the blade file. Anyhow, the outcome is that input fields need to be named like this:

from[collection1][0]
from[collection1][1]

My solution kinda looks like this:

<?php
  $i = 0;
?>

@foreach
  {!!form::text('from[collection1][' . $i . ']',null,['class'=>'form-controll']!!}

  <?php
    $i ++;
  ?>
@endforeach

note the $i in the otherwise empty []