Html Ruby on Rails 表单中的 HTML5“必需”验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13541983/
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
HTML5 'required' validation in Ruby on Rails forms
提问by LpLrich
I can't see this question anywhere else, it's hopefully a quick and easy one.
我在其他任何地方都看不到这个问题,希望这是一个快速简单的问题。
How can I use HTML5 validators, such as 'required', in my forms (ruby on rails)?
如何在我的表单(ruby on rails)中使用 HTML5 验证器,例如“必需”?
Eg, How would this basic form look if I used HTML5 validation in it?
例如,如果我在其中使用 HTML5 验证,这个基本表单会是什么样子?
<%=form_for @testimonial do |t|%>
<dl>
<dt><label for="testimonial_rating">Rating</label></dt>
<dd><%=t.select :rating, Testimonial.ratings%></dd>
<dt><label for="testimonial_content">Comments</label></dt>
<dd><%=t.text_area :content, :rows => 3%></dd>
<dd><button class="button success">Submit Review</button></dd>
</dl>
<%end%>
It goes without saying that server side validation is still required.
不用说,仍然需要服务器端验证。
回答by LpLrich
Ah, it was easy :required => true
啊,这很容易 :required => true
eg: <%=t.text_area :content, :rows => 3, :required => true%>
例如: <%=t.text_area :content, :rows => 3, :required => true%>
回答by prashantsahni
Just to add on, if you have an email field, you can also use 'pattern' attribute to validate the format of email
补充一下,如果您有电子邮件字段,您还可以使用 'pattern' 属性来验证电子邮件的格式
<%=form.text_field :email, :required => true, :pattern => '[^@]+@[^@]+\.[a-zA-Z]{2,6}' %>
:)
:)
回答by Dmitry Davydov
Addition to @prashantsahni answer. You can also use type = 'email'instead of regex pattern, then your erb-template will look like this:
除了@prashantsahni 答案。您还可以使用type = 'email'而不是正则表达式模式,那么您的 erb-template 将如下所示:
<%= f.email_field :email, id: 'user_email', type:'email', required: true, placeholder: "Email" %>
回答by Ezequiel García
This is a little example with the common attributes and for required you only add required:true, but dont forget apply this validations in your backend.
这是一个带有通用属性的小示例,对于 required,您只需添加 required:true,但不要忘记在后端应用此验证。
<%= f.text_field
id: "yourID",
class: "yourCLass",
placeholder: "Your message",
maxlength: 14,
required: true
%>
回答by Trung Lê
This could be easily done by adding :required => true
parameter into your input fields:
这可以通过:required => true
在输入字段中添加参数来轻松完成:
For example
例如
f.text_field :first_name, :required => true
text_field_tag :first_name, nil, :required => true
Pushing the boundary abit further, you could add in pattern matcher for your input, such as email:
进一步推动边界,您可以为您的输入添加模式匹配器,例如电子邮件:
f.email_field :email, 'Email', :required => true, :pattern => '[^@]+@[^@]+\.[a-zA-Z]{2,6}'
回答by Alex Casta?o
For completing other answers, there is an awesome gem html5_validationswhich makes the most of the HTML5 validations reading from ActiveRecord Validations from the model. No extra code needed, just installing it.
为了完成其他答案,有一个很棒的gem html5_validations,它充分利用了从模型的 ActiveRecord Validations 读取的 HTML5 验证。不需要额外的代码,只需安装它。
回答by gsumk
New Syntax
<%= f.text_field :email, class: "form-control", required: true %>
新语法
<%= f.text_field :email, class: "form-control", required: true %>
回答by Bilal A.Awan
Use this if nothing works
如果没有任何效果,请使用它
include_blank: false, required: true