我们如何验证作为数组的表单字段?看看下面的代码
UserPhone型号:
public static $rules= array(
'phonenumber'=>'required|numeric',
'isPrimary'=>'in:0,1'
)
...........
Run Code Online (Sandbox Code Playgroud)
UserController的:
$validation = UserPhone::validate(Input::only('phonenumber')));
if($validation->passes())
{
$allInputs = Input::only('phonenumber','tid');
$loopSize = sizeOf($allInputs);
for($i=0;$i<$loopSize;$i++)
{
$phone = UserPhone::find($allInputs['tid'][$i]);
$phone->phonenumber = $allInputs['phonenumber'][$i];
$phone->save();
}
return Redirect::to('myprofile')->with('message','Update OK');
}
else
{
return Redirect::to('editPhone')->withErrors($validation);
}
}
Run Code Online (Sandbox Code Playgroud)
对$validation来自延伸洋洋洒洒一BaseModel.
在我看来:
<?php $counter=1; ?>
@foreach($phones as $thephone)
<section class="col col-12">
<label class="label">Phone Number {{$counter++}}</label>
<label class="input">
<i class="icon-append icon-phone"></i>
{{Form::text('phonenumber[]',$thephone->phonenumber)}}
{{Form::hidden('tid[]',$thephone->id)}}
</label>
</section>
@endforeach
Run Code Online (Sandbox Code Playgroud)
一切正常,我在更新表单中获得了我想要的所有电话号码,但我无法更新模型,因为验证失败并显示消息"Phonenumber必须是一个数字".
我知道没有一个简单的解决方案来验证数组表单字段,我试图扩展验证器类但没有成功.
我该如何验证这种字段?