我开始学习 Laravel 并且仍在学习曲线上。现在我从 Laravel 3 开始,但一旦我开始工作,很可能会将我的项目切换到 Laravel 4。现在的问题是,如何验证复选框数组,我想验证组内至少有 1 个启用(选中)。我在 Laravel 论坛上的某个地方读到我们只是使用必需的来验证它们,但是当我dd(input::all())
没有看到任何其他内容时,除了输入字段和复选框不是它们的一部分......
我的 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>
我的控制器(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');
}
我还想知道验证失败后获取复选框状态的正确代码,我以为我看到了Input::had(checkBoxName)
某个地方,但这似乎不起作用,我可能没有正确使用它,我有点困惑因为我看到的所有示例都是用于输入,仅此而已。我假设 L4 中的验证大致相同,是吗?