php Laravel 5,如何测试控制器中是否选中了复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40411283/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:09:51  来源:igfitidea点击:

Laravel 5, how to test if a Checkbox is checked in a controller

phplaravel

提问by loic.lopez

i tried to get if checkbox is checked with :

我试图了解是否选中了复选框:

In my view :

在我看来 :

 <form data-toggle="validator" data-disable="false" role="form" action="/admin/role/add" method="post">
  <div class="checkbox checkbox-success">
   <input name="test" id="test" type="checkbox" value="test">
   <label for="test" style="padding-left: 15px!important;">test</label>
  </div>
 </form>
 <form data-toggle="validator" data-disable="false" role="form" action="/admin/role/add" method="post">
  {{ csrf_field() }}
  <div class="form-group pull-right">
     <button type="submit" class="btn btn-danger">Submit</button>
  </div>
 </form>

In my controller :

在我的控制器中:

public function createRole(Request $request)
{
    if( $request->has('test')) {
        new \App\Debug\Firephp('test', ['test' => true]);
    }
}

in web.php :

在 web.php 中:

 Route::post('/admin/role/add', 'AdminController@createRole')

but doesn't work.

但不起作用。

How i can do ?

我该怎么办?

Thanks for reply.

谢谢您的回复。

EDIT 1 :

编辑 1:

It was my form that was poorly build

是我的身体构造不佳

采纳答案by Ed Rands

I believe your real problem is that you have two separate forms. Your checkbox is in one form, your submit button is in a second form. I believe they both need to be in the same form. Otherwise your checkbox state is never returned, regardless of it's state.

我相信你真正的问题是你有两种不同的形式。你的复选框是一种形式,你的提交按钮是另一种形式。我相信它们都需要采用相同的形式。否则您的复选框状态永远不会返回,无论它的状态如何。

In your view, try replacing the form markup you provided with this:

在您看来,尝试替换您提供的表单标记:

<form data-toggle="validator" data-disable="false" role="form" action="/admin/role/add" method="post">
    <div class="checkbox checkbox-success">
        <input name="test" id="test" type="checkbox" value="test">
        <label for="test" style="padding-left: 15px!important;">test</label>
    </div>
    {{ csrf_field() }}
    <div class="form-group pull-right">
        <button type="submit" class="btn btn-danger">Submit</button>
    </div>
</form>

回答by Kurucu

You can test it with:

您可以使用以下方法对其进行测试:

if( Input::get('test', false) ) {
    // there is something for 'test'
} else {
    // there was no 'test' or it was false
}

Or

或者

if( Request::input('test') ) { //...

Or

或者

public function store(Request $request)
{
    if( $request->has('test') ){
        //...
    }
}

https://laravel.com/docs/5.0/requests

https://laravel.com/docs/5.0/requests

回答by Peon

Checkbox sends info onlywhen it's checked, so what you do is, you check, if the value is present. It's easier, if you use method with Request $requestthan using direct inputs:

复选框在选中时发送信息,因此您要做的是检查该值是否存在。如果您使用方法 withRequest $request比使用直接输入更容易:

if (isset($request->test)) {
    // checked
}

Laravel docs: https://laravel.com/docs/5.0/requests

Laravel 文档:https://laravel.com/docs/5.0/requests

回答by Zoran Stankovic

I made an class and put this method into it:

我做了一个类并将这个方法放入其中:

//This function is used to insert missing index key when checkboxes are used
public static function checkboxHelper($request_array, $key)
{
    if(!array_key_exists($key,$request_array)){
        $request_array[$key]=0;
    }
    else{
        $request_array[$key]=1;
    }

    return $request_array;
}

This method is taking array from $request object and inserting missing key (name of checkbox).

此方法从 $request 对象中获取数组并插入缺少的键(复选框名称)。