我正在使用巫术进行身份验证以及twitter bootstrap.
我想通过改变<div class="field_with_errors">添加到DOM 的默认轨道,以twitter的bootstrap风格在我的注册表单上设置我的错误消息.
做这样的事情的轨道惯例是什么?
我想你可以添加一些操作DOM来重命名的javascript <div class="field_with_errors">,但这看起来像是一个黑客.似乎应该有一种方法可以在rails中覆盖它,但我无法弄清楚在哪里这样做.
这是bootstrap要求您标记错误以使用其内置表单错误样式的方式:
<div class="control-group error">
<label class="control-label" for="inputError">Input with error</label>
<div class="controls">
<input type="text" id="inputError">
<span class="help-inline">Please correct the error</span>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 如何在未通过验证时停止Rails更改我的代码.
每次铁路包裹我的田地
<div class='field_with_error'>...</div>
Run Code Online (Sandbox Code Playgroud)
我可以编辑fields_with_error课程
.fields_with_error{ display: inline }
Run Code Online (Sandbox Code Playgroud)
这是有效的,但它是hacky
保存表单和字段验证失败时,我不希望完全修改标签.我不介意用div和一个"field_with_errors"类包装输入,但是如何阻止它包装标签呢?
为了清楚起见,我知道如何在config/locales/devise.en.yml中编辑错误消息,但我指的是设置这些类型的错误消息:
2 errors prohibited this user from being saved:
Run Code Online (Sandbox Code Playgroud)
-
我只看到<%= devise_error_messages!注册页面上有%>,但我不知道如何实际编辑错误消息本身.
在我的情况下,消息显示在左侧,我的注册是居中的(看起来很奇怪),我也不喜欢消息的红色,并且更喜欢不同的颜色.
所以我的问题是,我如何设置错误信息的样式?将它居中,并改变颜色.
不知道要包含哪些其他控制器或内容,所以一旦你问,我会在需要时用它们更新OP.
所以我有一个像这样的输入元素.你知道,包装元素是一种视觉效果.
<div class="input-wrap">
<input class="blah-blah" />
</div>
Run Code Online (Sandbox Code Playgroud)
当<input>包含错误时,它将是这样的:
<div class="input-wrap">
<div class="field-with-errors">
<input class="blah-blah" />
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但我想做的是:
<div class="input-wrap field-with-errors">
<input class="blah-blah" />
</div>
Run Code Online (Sandbox Code Playgroud)
我找到了这个页面,它非常接近我的问题
Rails 3:"field-with-errors"包装器改变了页面外观.怎么避免这个?
现在我知道我可以扔掉
config.action_view.field_error_proc = Proc.new { |html_tag, instance|
"#{html_tag}".html_safe
}
Run Code Online (Sandbox Code Playgroud)
避免在<input>有错误的标记周围创建包装标记.但我真正想做的是再次在<input>标签的直接父级上添加"field-with-errors"类.我能这样做吗?ActionView是否包含DOM节点的树结构?
我使用bootstrap 3的网格系统来布局一个简单的注册表单.但是,如果Rails中的验证失败,添加了类"field_with_errors"的div会弄乱我的布局.
以下是我的表单中的一行通常如下所示:
<div class="form-group">
<label class="col-xs-2 control-label" for="user_secondary_email">Secondary email</label>
<div class="col-xs-4">
<input class="form-control" id="user_secondary_email" name="user[secondary_email]" type="text" />
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
验证失败时,当前应用的样式如下所示:
<div class="form-group">
<div class="field_with_errors"><label class="col-xs-2 control-label" for="user_primary_email">Primary email</label></div>
<div class="col-xs-4">
<div class="field_with_errors"><input class="form-control" id="user_primary_email" name="user[primary_email]" type="text" value="" /></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我做了一些研究,据我所知,这是代码应该是这样的:
<div class="form-group has-error">
<label class="col-xs-2 control-label" for="user_secondary_number">Secondary number</label>
<div class="col-xs-4">
<input class="form-control" id="user_secondary_number" name="user[secondary_number]" type="text" />
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我不明白的是如何让应用程序将其他类添加到正确的html标记中?
任何帮助表示赞赏!
在我的项目中,我有一个注册表.
我的HTML看起来像
<form method="post" action="/users">
<div style="margin: 0pt; padding: 0pt;">
<input type="hidden" value="lDHrKRC2ENhRKcaoBR4XGfzri/MY09PjqVDvHRtC0D4=" name="authenticity_token"/>
</div>
<p><label for="login">Login</label>
<input type="text" size="30" name="user[login]" id="user_login"/></p>
<p><label for="email">Email</label>
<input type="text" size="30" name="user[email]" id="user_email"/></p>
<p><label for="password">Password</label>
<input type="password" size="30" name="user[password]" id="user_password"/></p>
<p><label for="password_confirmation">Confirm Password</label>
<input type="password" size="30" name="user[password_confirmation]" id="user_password_confirmation"/></p>
<p><input type="submit" value="Sign up" name="commit"/></p>
</form>
Run Code Online (Sandbox Code Playgroud)
现在通过提交空表单生成错误我的代码如下:
<form method="post" action="/users">
<div style="margin: 0pt; padding: 0pt;"><input type="hidden" value="lDHrKRC2ENhRKcaoBR4XGfzri/MY09PjqVDvHRtC0D4=" name="authenticity_token"/>
</div>
<p><label for="login">Login</label>
</p><div class="fieldWithErrors"><input type="text" value="hg" size="30" name="user[login]" id="user_login"/></div>
<p><label for="email">Email</label>
</p><div class="fieldWithErrors"><input type="text" value="gh" size="30" name="user[email]" …Run Code Online (Sandbox Code Playgroud) 目前,当存在验证错误时,将在我的表单中呈现divwith class="field_with_errors":
<form method="post" action="/users" accept-charset="UTF-8">
<div class="field_with_errors">
<input id="user_email" type="text" value="tom@test.com" name="user[email]">
</div>
<input id="user_password" type="password" value="mypass" size="30" name="user[password]">
<input id="user_submit" type="submit" value="Submit" name="commit">
</form>
Run Code Online (Sandbox Code Playgroud)
如果不使用额外的gem并在Rails 3环境中,有没有办法修改验证错误的呈现方式?例如,我希望在带有错误div的input标记之后呈现验证错误:
<input id="user_email" type="text" value="tom@test.com" name="user[email]"><div class="field_with_errors"></div>
Run Code Online (Sandbox Code Playgroud)
另外,有没有办法呈现特定的错误?如果是电子邮件错误,则div在输入文本框之后呈现并生成"重新输入您的电子邮件"消息.
(到目前为止,我所做的大多数搜索都使用gems作为解决方案.)