Laravel 3-如何验证复选框数组,至少检查一次? [英] Laravel 3 - How to validate checkbox array, for at least 1 checked?

查看:44
本文介绍了Laravel 3-如何验证复选框数组,至少检查一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习Laravel,但仍处于学习曲线上.现在我开始使用Laravel 3,但是一旦我能正常工作,很可能会将我的项目切换到Laravel 4. 现在的问题是,如何验证复选框数组,我想验证组中至少有1个启用(已选中).我在Laravel论坛上的某个地方读到,我们只是使用必填项对它们进行验证,但是当我dd(input::all())时,我什么都没看到,但是输入字段和复选框不是它们的一部分...

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...

我的Blade的一部分为该复选框创建代码:

Part of my Blade Create code for the checkbox:

<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)代码是:

My controller (REST) code is:

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中大致相同,是吗?

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?

推荐答案

回到该项目并进行更多研究,我发现以下是解决此问题的最佳方法.

Going back on this project and making some more researches, I have found the best way for this problem is the following.

我的刀片视图:

<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:

  1. 使用Input::old拉出复选框数组(在我的情况下为'changeReasons []')
  2. 然后我们可以从该数组中搜索单个复选框,看看它们是否在其中,然后将其更改为checked状态.那是in_array()函数的工作,返回true/false将更改复选框的状态.
  1. Pull the checkbox array (in my case 'changeReasons[]') with Input::old
  2. 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.

我的控制器(REST)代码与开始时在我的问题中所写的完全相同.有关更多信息,定义$rules = array('changeReasons' => 'required');将确保至少一个复选框为checked.

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.

这篇关于Laravel 3-如何验证复选框数组,至少检查一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆