php 如何在laravel 4中设置表单输入所需的属性

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

How to set form input required attribute in laravel 4

phphtmlformslaravel-4requiredfieldvalidator

提问by GothamCityRises

I'm using the laravel framework for a project, and I'm implementing a basic form page, where I require certain values to be required, something that can be done very easily in HTML5.

我正在为一个项目使用 laravel 框架,并且我正在实现一个基本的表单页面,其中我需要某些值required,这在 HTML5 中可以很容易地完成。

<input type="text" name="abc" required>

In laravel, without the requiredattribute, the same would be :

在 laravel 中,如果没有该required属性,则为:

{{ Form::text('abc') }}

How do I incorporate a required attribute in the above statement?

如何在上述语句中加入必需的属性?

回答by GothamCityRises

Since simply writing ['required']did not work, I searched online a bit more and found the answer, so I thought I'd share it here.

由于单纯的写['required']是行不通的,我在网上多搜了一下,找到了答案,所以想在这里分享一下。

The third parameter is an array of optional attributes, which, in convention, must be written as:

第三个参数是一个可选属性数组,按照惯例,必须写成:

{{ Form::text('abc','',array('required' => 'required')) }}

Similarly, for a radio button with default selected/checked we have:

同样,对于默认选中/选中的单选按钮,我们有:

{{ Form::radio('abc', 'yes', array('checked' => 'checked')) }}

回答by thpl

Check out the API-Docs. The method signature shows that you can provide 3 parameters.

查看API 文档。方法签名显示您可以提供 3 个参数。

The first one is the nameattribute, the second one is the valueattribute. Third one is your array with any additional attributes.

第一个是名称属性,第二个是属性。第三个是具有任何其他属性的数组。

So just call your method with:

所以只需调用你的方法:

{{ Form::text('key', 'value', ['required']) }}

And a requiredattribute will be attached to your input field.

并且required属性将附加到您的输入字段。

回答by Max

I believe the correct answer is similar to the other post where the third parameter is

我相信正确的答案类似于第三个参数是的另一篇文章

array('required' => 'required')

however to get the attribute without any value you can do the following:

但是要获取没有任何值的属性,您可以执行以下操作:

array('required' => '')

The input field (for text example), will then look what was necessary in the question.

输入字段(例如文本)将查看问题中的必要内容。

Laravel Example:

Laravel 示例:

{{ Form::text('title', '', array('tabindex' => '1', 'required' => '')) }}

HTML output:

HTML 输出:

<input tabindex="1" required name="title" type="text" value="" id="title">

I believe this actually shorthand for required='', just wanted to add this note

我相信这实际上是 required='' 的简写,只是想添加此注释

回答by Kamlesh

Radio required featured works with laravel 5.7 version

电台需要的特色作品与 laravel 5.7 版本

@foreach($status_list as $status_key => $status)
  {!! Form::radio('status', $status_key, false, array('id'=>'status_'.$status_key, 'required'=>'required' )); !!}
  {!! Form::label('status_'.$status_key, $status ) !!}
@endforeach

I hope, this will help you also. :)

我希望,这也能帮助你。:)