Laravel/Blade 表单添加“必需”以选择元素

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

Laravel/Blade forms add 'required' to select element

phphtmlformslaravelblade

提问by Arthur

How can I make my select element a required select element?

如何使我的选择元素成为必需的选择元素?

What I want in HTML :

我在 HTML 中想要什么:

What I have at the moment in blade:

我目前在刀片中拥有什么:

{!! Form::select('someid', [null => 'Please Select'] + $somelist, ['required']) !!}

Thanks

谢谢

回答by lagbox

You want the 4th argument to select - 'options'

您希望选择第四个参数 - 'options'

public function select($name, $list = [], $selected = null, $options = [])

{!! Form::select('someid', [null => 'Please Select'] + $somelist, null, ['required']) !!}

回答by Vahid Hallaji

According to Laravel API, you have missed the 4th parameter:

根据Laravel API,您错过了第四个参数:

string select( 
    string $name, 
    array $list = array(), 
    string $selected = null, 
    array $options = array()
)

/*
Parameters:
string  $name   
array   $list   
string  $selected   
array   $options    

Return Value:
string
*/

{!! Form::select('someid', [null => 'Please Select'] + $somelist, null, ['required']) !!}

回答by Franco

In Laravel you don't need to require a field value. For this purpose you will use a request where you can set your rules:

在 Laravel 中,您不需要字段值。为此,您将使用一个可以设置规则的请求:

public function rules()
    {

        return [
            'name'  => 'required',

        ];
    }

If the validation fails than you can not post your form.

如果验证失败,则您无法发布表单。

It is bad practice to rely on client-side validation.

依赖客户端验证是不好的做法。