【发布时间】:2013-04-25 22:55:42
【问题描述】:
我开始学习 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 中的验证大致相同,是吗?
【问题讨论】: