验证 Laravel 中的未知输入字段
Validating unknown input fields in Laravel
我无法思考如何验证我创建的表单。我的表格看起来像这样:
<input id="box-1-nickname" name="box-1-nickname" class="form-control" type="text" placeholder="Required">
<select id="box-1-destination" name="box-1-destination" class="form-control">
<option value="store">Storage Facility</option>
<option value="ship">Ship</option>
</select>
<input class="box-height form-control" id="box-1-height" name="box-1-height" type="number" placeholder="in Inches">
<input class="box-width form-control" id="box-1-width" name="box-1-width" type="number" placeholder="in Inches">
<input class="box-depth form-control" id="box-1-depth" name="box-1-depth" type="number" placeholder="in Inches">
<input class="box-weight form-control" id="box-1-weight" name="box-1-weight" type="number" placeholder="in Pounds">
<label class="radio-inline">
<input id="box-1-size-retail" name="box-1-size" type="radio" value="retail" checked>
Retail box (18" x 18" x 22")
</label>
<label class="radio-inline">
<input id="box-1-size-custom" name="box-1-size" type="radio" value="custom">
I'll use my own box
</label>
使它有点复杂的部分是用户可以 'add a box' 这将复制这些表单字段并将框 ID 增加 1。在 adding/removing 几个框并提交从 Input::all()
返回时可能会是这样的:
array (size=29)
'box-1-nickname' => string 'Something' (length=9)
'box-1-destination' => string 'store' (length=5)
'box-1-height' => string '1' (length=1)
'box-1-width' => string '2' (length=1)
'box-1-depth' => string '3' (length=1)
'box-1-weight' => string '4' (length=1)
'box-1-size' => string 'retail' (length=9)
'box-4-nickname' => string 'Another' (length=7)
'box-4-destination' => string 'ship' (length=4)
'box-4-height' => string '33' (length=2)
'box-4-width' => string '1' (length=1)
'box-4-depth' => string '22' (length=2)
'box-4-weight' => string '33' (length=2)
'box-4-size' => string 'custom' (length=6)
'box-6-nickname' => string 'Stuff' (length=5)
'box-6-destination' => string 'store' (length=5)
'box-6-height' => string '34' (length=2)
'box-6-width' => string '76' (length=2)
'box-6-depth' => string '44' (length=2)
'box-6-weight' => string '2' (length=1)
'box-6-size' => string 'retail' (length=9)
'box-8-nickname' => string 'Things and others' (length=17)
'box-8-destination' => string 'ship' (length=4)
'box-8-height' => string '5' (length=1)
'box-8-width' => string '66' (length=2)
'box-8-depth' => string '5' (length=1)
'box-8-weight' => string '33' (length=2)
'box-8-size' => string 'custom' (length=6)
'_token' => string 'BIXSdz16ccJLaOmTxh2ShW5C16W1g0xmpJ10xnwC' (length=40)
我正在努力寻找一种方法来验证这些框输入,因为我真的不知道要提交多少框字段或它们的输入名称是什么。非常欢迎任何建议。
更改输入变量的名称可能是解决此问题的方法。与其使用 box-1-...
和 box-2-...
等,为什么不用这样的名称:
name="box-nickname[]"
name="box-destination[]"
name="box-height[]"
name="box-width[]"
name="box-depth[]"
name="box-weight[]"
如果您向服务器发送了 2 个这样的框,调用 Input::all()
将类似于:
'box-nickname' => array(
"0" => "Value of Box Nickname 1",
"1" => "Value of Box Nickname 2",
...),
'box-destination' => array(
"0" => "Value of Box Destination 1",
...
你的想法对吗?验证这一点变得简单,规则只需要应用于每个值数组,而不是发送的每个数字框:
$rules = array(
"box-nickname" => "Required|Max:6",
"box-destination" => "Required|Numeric"
...
);
您可以根据需要更改规则。唯一困难的部分是在重定向回页面时显示验证器消息。因此,我还建议将此与客户端验证配对,在使用 Redirect::to()->withInput()->withErrors($validator)
.
时,它比 Laravel 更好地处理动态生成的输入
希望对您的问题有所帮助!
我无法思考如何验证我创建的表单。我的表格看起来像这样:
<input id="box-1-nickname" name="box-1-nickname" class="form-control" type="text" placeholder="Required">
<select id="box-1-destination" name="box-1-destination" class="form-control">
<option value="store">Storage Facility</option>
<option value="ship">Ship</option>
</select>
<input class="box-height form-control" id="box-1-height" name="box-1-height" type="number" placeholder="in Inches">
<input class="box-width form-control" id="box-1-width" name="box-1-width" type="number" placeholder="in Inches">
<input class="box-depth form-control" id="box-1-depth" name="box-1-depth" type="number" placeholder="in Inches">
<input class="box-weight form-control" id="box-1-weight" name="box-1-weight" type="number" placeholder="in Pounds">
<label class="radio-inline">
<input id="box-1-size-retail" name="box-1-size" type="radio" value="retail" checked>
Retail box (18" x 18" x 22")
</label>
<label class="radio-inline">
<input id="box-1-size-custom" name="box-1-size" type="radio" value="custom">
I'll use my own box
</label>
使它有点复杂的部分是用户可以 'add a box' 这将复制这些表单字段并将框 ID 增加 1。在 adding/removing 几个框并提交从 Input::all()
返回时可能会是这样的:
array (size=29)
'box-1-nickname' => string 'Something' (length=9)
'box-1-destination' => string 'store' (length=5)
'box-1-height' => string '1' (length=1)
'box-1-width' => string '2' (length=1)
'box-1-depth' => string '3' (length=1)
'box-1-weight' => string '4' (length=1)
'box-1-size' => string 'retail' (length=9)
'box-4-nickname' => string 'Another' (length=7)
'box-4-destination' => string 'ship' (length=4)
'box-4-height' => string '33' (length=2)
'box-4-width' => string '1' (length=1)
'box-4-depth' => string '22' (length=2)
'box-4-weight' => string '33' (length=2)
'box-4-size' => string 'custom' (length=6)
'box-6-nickname' => string 'Stuff' (length=5)
'box-6-destination' => string 'store' (length=5)
'box-6-height' => string '34' (length=2)
'box-6-width' => string '76' (length=2)
'box-6-depth' => string '44' (length=2)
'box-6-weight' => string '2' (length=1)
'box-6-size' => string 'retail' (length=9)
'box-8-nickname' => string 'Things and others' (length=17)
'box-8-destination' => string 'ship' (length=4)
'box-8-height' => string '5' (length=1)
'box-8-width' => string '66' (length=2)
'box-8-depth' => string '5' (length=1)
'box-8-weight' => string '33' (length=2)
'box-8-size' => string 'custom' (length=6)
'_token' => string 'BIXSdz16ccJLaOmTxh2ShW5C16W1g0xmpJ10xnwC' (length=40)
我正在努力寻找一种方法来验证这些框输入,因为我真的不知道要提交多少框字段或它们的输入名称是什么。非常欢迎任何建议。
更改输入变量的名称可能是解决此问题的方法。与其使用 box-1-...
和 box-2-...
等,为什么不用这样的名称:
name="box-nickname[]"
name="box-destination[]"
name="box-height[]"
name="box-width[]"
name="box-depth[]"
name="box-weight[]"
如果您向服务器发送了 2 个这样的框,调用 Input::all()
将类似于:
'box-nickname' => array(
"0" => "Value of Box Nickname 1",
"1" => "Value of Box Nickname 2",
...),
'box-destination' => array(
"0" => "Value of Box Destination 1",
...
你的想法对吗?验证这一点变得简单,规则只需要应用于每个值数组,而不是发送的每个数字框:
$rules = array(
"box-nickname" => "Required|Max:6",
"box-destination" => "Required|Numeric"
...
);
您可以根据需要更改规则。唯一困难的部分是在重定向回页面时显示验证器消息。因此,我还建议将此与客户端验证配对,在使用 Redirect::to()->withInput()->withErrors($validator)
.
希望对您的问题有所帮助!