Laravel 3 - 如何验证复选框数组,至少选中 1 个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16326693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 3 - How to validate checkbox array, for at least 1 checked?
提问by ghiscoding
I'm starting to learn Laravel and still on the learning curve. Now I'm starting with Laravel 3 but will most probably switch my project into Laravel 4 once I get something working.
Now the question being, how to validate an array of checkbox, I want to validate that at least 1 inside the group is enable(checked). I read somewhere on Laravel forum that we just validate them using a required, but when I dd(input::all())
I don't see anything else but the inputs field and checkbox are not part of them...
我开始学习 Laravel 并且仍在学习曲线上。现在我从 Laravel 3 开始,但一旦我开始工作,我很可能会将我的项目切换到 Laravel 4。现在的问题是,如何验证一组复选框,我想验证组内至少有 1 个已启用(选中)。我在 Laravel 论坛上的某处读到我们只是使用必需的来验证它们,但是当我dd(input::all())
看不到任何其他东西时,除了输入字段和复选框不是它们的一部分......
Part of my Blade Create code for the checkbox:
复选框的部分 Blade Create 代码:
<label class="checkbox">{{ Form::checkbox('changeReasons[]', 'ckbCRCertification', Input::had('ckbCRCertification'), array('id' => 'ckbCRCertification')) }} Certification</label>
<label class="checkbox">{{ Form::checkbox('changeReasons[]', 'ckbCRDesignCorrection', Input::had('ckbCRDesignCorrection'), array('id' => 'ckbCRDesignCorrection')) }} Design Correction</label>
My controller (REST) code is:
我的控制器(REST)代码是:
public function post_create()
{
print "Inside the post_create()";
// validate input
$rules = array(
'ecoNo' => 'min:4',
'productAffected' => 'required',
'changeReasons' => 'required'
);
$validation = Validator::make(Input::all(), $rules);
if($validation->fails())
{
return Redirect::back()->with_input()->with_errors($validation);
}
$eco = new Eco;
$eco->ecoNo = Input::get('ecoNo');
$eco->productAffected = Input::get('productAffected');
$eco->save();
return Redirect::to('ecos');
}
I also want to know the correct code for getting the checkboxes state after a validation fails, I thought I saw the Input::had(checkBoxName)
somewhere but that doesn't seem to work, I'm probably not using it correctly and I'm getting a little confuse on that since all example I see are for inputs and nothing else. I assume the validation is roughly the same in L4, would it be?
我还想知道在验证失败后获取复选框状态的正确代码,我以为我看到了Input::had(checkBoxName)
某个地方,但这似乎不起作用,我可能没有正确使用它,而且我有点困惑因为我看到的所有示例都是用于输入而不是其他任何东西。我假设 L4 中的验证大致相同,是吗?
采纳答案by ghiscoding
Going back on this project and making some more researches, I have found the best way for this problem is the following.
回到这个项目并进行更多研究,我发现解决这个问题的最佳方法如下。
My blade view:
我的刀片视图:
<div class="control-group row-fluid">
<?php $arrChangeReasons = Input::old('changeReasons', array()); // array of enable checkboxes in previous request ?>
<label class="checkbox">{{ Form::checkbox('changeReasons[]', 'certification', in_array('certification', $arrChangeReasons)) }} Certification</label>
<label class="checkbox">{{ Form::checkbox('changeReasons[]', 'designCorrection', in_array('designCorrection', $arrChangeReasons)) }} Design Correction</label>
</div>
The explanation of the blade view is a 2 steps process, after a validation occur, is the following:
刀片视图的解释是一个 2 个步骤的过程,验证发生后,如下:
- Pull the checkbox array (in my case 'changeReasons[]') with
Input::old
- From that array we can then search for individual checkbox and see if they are in there, if they are then change the checkbox as a
checked
state. That is the job of the in_array() function, returning a true/false will change the state of the checkbox.
- 拉复选框数组(在我的情况下'changeReasons[]')与
Input::old
- 然后我们可以从该数组中搜索单个复选框并查看它们是否在那里,如果它们在,则将复选框更改为
checked
状态。这就是 in_array() 函数的工作,返回真/假将改变复选框的状态。
My controller (REST) code is exactly as it was written in my question at the beginning. For more information, defining $rules = array('changeReasons' => 'required');
will make sure that at least 1 of the checkboxes is checked
.
我的控制器 (REST) 代码与我一开始的问题中所写的完全一样。有关更多信息,定义$rules = array('changeReasons' => 'required');
将确保至少有 1 个复选框是checked
。
回答by Strernd
Please remember that Checkboxes need a value like . It the Checkbox is checked Input::get('foo') will return 1, but if it is unchecked it will return nothing, because it is not in the post-array.
请记住,复选框需要一个像 . 如果 Checkbox 被选中 Input::get('foo') 将返回 1,但如果未选中它则不会返回任何内容,因为它不在 post-array 中。
I'm using this code:
我正在使用此代码:
if(Input::get('foo')){
$bar->is_foo = 1;
}
else{
$bar->is_foo = 0;
}