我正在尝试使用angular进行设计寄存器表单的客户端验证。但我无法使其发挥作用。我成功地将“无效”类添加到字段中按预期方式。但是,当我做ng-show时,无论班级如何,它现在都显示出来。除此之外没有角度控制器或文件。我所做的只是客户端验证。我现在添加的是ng-required
字段中的first_name
。有人能告诉我这里有什么问题吗?
这是我的代码。
<div class="contaner-fluid" ng-app>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: {name: "myform", "novalidate" => true}) do |f| %>
<%= devise_error_messages! %>
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><h2>Sign up</h2></div>
<div class="panel-body">
<div class="col-md-12">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<%= f.label :first_name %><br />
<%= f.text_field :first_name, autofocus: true, "ng-model" => "myform.firstname", "ng-required": true, class: "form-control input-lg" %>
</div>
<p ng-show="myform.firstname.$invalid">
You must fill a valid name
</p>
</div>
<div class="col-md-6">
<div class="form-group">
<%= f.label :last_name %><br />
<%= f.text_field :last_name, autofocus: true, class: "form-control input-lg"%>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, class: "form-control input-lg" %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, autocomplete: "off" , class: "form-control input-lg" %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off", class: "form-control input-lg" %>
</div>
<div class="actions">
<%= f.submit "Sign up", class: "btn btn-success btn-block" %>
</div>
<% end %>
</div>
</div>
</div>
<%= render "devise/shared/links" %>
</div>
这是我的凉亭文件
"dependencies": {
"normalize-css": "normalize.css#~3.0.3",
"bootstrap-sass": "~3.3.6",
"angular": "~1.4.8",
"bootstrap": "~3.3.6"
}
更新
我可以做数据绑定。因此角度在表单中工作正常。如果我尝试这个,它将有效{{myform.firstname}}
。但是如果我{{myform.firstname.$valid}}
它就不会显示任何内容。我可以看到类被添加到输入字段但不能像这样访问它。我没有创建controller
或module
,因为这是一个非常基本的实现。谢谢你的帮助。
以下是表单
的完整生成的html<div class="contaner-fluid ng-scope" ng-app="">
<form name="myform" novalidate="novalidate" class="new_user ng-dirty ng-valid-parse ng-invalid ng-invalid-required" id="new_user" action="/users" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="omiBCucx+U4r9P+T0d4rQEeKhni3H4YlSPSU9FIiP4yFnMe+orTnE64czY5NSk4Yd/8ObjPFz8mlURYgOfxXhw==">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><h2>Sign up</h2></div>
<div class="panel-body">
<div class="col-md-12">
<div class="row">
<div class="col-md-6 ng-binding">
<div class="form-group">
<label for="user_first_name">First name</label><br>
<input autofocus="autofocus" ng-model="myform.first_name" ng-required="true" class="form-control input-lg ng-dirty ng-valid-parse ng-invalid ng-invalid-required ng-touched" type="text" name="user[first_name]" id="user_first_name" required="required">
</div>
<p ng-show="myform.first_name.$invalid" class="ng-hide">
You must fill a valid name
</p>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="user_last_name">Last name</label><br>
<input autofocus="autofocus" class="form-control input-lg" type="text" name="user[last_name]" id="user_last_name">
</div>
</div>
</div>
</div>
<div class="form-group">
<label for="user_email">Email</label><br>
<input autofocus="autofocus" class="form-control input-lg" type="email" value="" name="user[email]" id="user_email">
</div>
<div class="form-group">
<label for="user_password">Password</label>
<input autocomplete="off" class="form-control input-lg" type="password" name="user[password]" id="user_password">
</div>
<div class="form-group">
<label for="user_password_confirmation">Password confirmation</label><br>
<input autocomplete="off" class="form-control input-lg" type="password" name="user[password_confirmation]" id="user_password_confirmation">
</div>
<div class="actions">
<input type="submit" name="commit" value="Sign up" class="btn btn-success btn-block">
</div>
</div>
</div>
</div></form>
<a href="/users/sign_in">Log in</a><br>
</div>
答案 0 :(得分:0)
更改输入的ng-model,如下所示:
<%= f.text_field :first_name, autofocus: true, "ng-model" => "firstname", "ng-required": true, class: "form-control input-lg" %>
然后更改您的ng-show
:
<p ng-show="myform['user[first_name]'].$invalid">
它应该有用。
P.S。您不需要使用ng-required
,因为您没有评估该字段的要求。您只需使用html required
。