Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation of array form fields in laravel 4 error
    primarykey
    data
    text
    <p>How can we validate form fields that are arrays? Take a look at the following code</p> <p>UserPhone Model:</p> <pre class="lang-php prettyprint-override"><code> public static $rules= array( 'phonenumber'=&gt;'required|numeric', 'isPrimary'=&gt;'in:0,1' ) ........... </code></pre> <p>UserController:</p> <pre class="lang-php prettyprint-override"><code>$validation = UserPhone::validate(Input::only('phonenumber'))); if($validation-&gt;passes()) { $allInputs = Input::only('phonenumber','tid'); $loopSize = sizeOf($allInputs); for($i=0;$i&lt;$loopSize;$i++) { $phone = UserPhone::find($allInputs['tid'][$i]); $phone-&gt;phonenumber = $allInputs['phonenumber'][$i]; $phone-&gt;save(); } return Redirect::to('myprofile')-&gt;with('message','Update OK'); } else { return Redirect::to('editPhone')-&gt;withErrors($validation); } } </code></pre> <p>the <code>$validation</code> comes from a BaseModel which extends Eloquent.</p> <p>In my view:</p> <pre class="lang-php prettyprint-override"><code> &lt;?php $counter=1; ?&gt; @foreach($phones as $thephone) &lt;section class="col col-12"&gt; &lt;label class="label"&gt;Phone Number {{$counter++}}&lt;/label&gt; &lt;label class="input"&gt; &lt;i class="icon-append icon-phone"&gt;&lt;/i&gt; {{Form::text('phonenumber[]',$thephone-&gt;phonenumber)}} {{Form::hidden('tid[]',$thephone-&gt;id)}} &lt;/label&gt; &lt;/section&gt; @endforeach </code></pre> <p>Everything is working fine and I get all the phone numbers I want in the Update Form, but I cannot update the model because the validation fails with the message "Phonenumber must be a number".</p> <p>I know that there is not a simple solution for validating array form fields and I tried to extend the validator class but with no success.</p> <p>How can I validate this kind of fields?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload