角形式验证但没有<form>?

时间:2016-11-29 00:25:38

标签: javascript jquery angularjs sharepoint-2010 angular-material

我正在尝试使用角度材料框架获得一个非常简单的角度验证表单。我正在Sharepoint 2010内容编辑器webpart中构建此表单,该表单允许使用。我正在努力重建表单而不是在网页上,但没有涉及其他更复杂的问题。

但我深入研究其他问题,我只是想知道角度验证表单是否可以在没有元素的情况下工作?或者如果其他指令可以替代它?

这是一个关于我的目标的一个小例子,但没有表格元素。

http://jsfiddle.net/jhadesdev/yfLqfzLw/2/?utm_source=website&utm_medium=embed&utm_campaign=yfLqfzLw

<div class="container" id="demoApp">
  <form class="pure-form pure-form-aligned" name="frm" method="post"
    novalidate autocomplete="off">
      <fieldset>

          <div class="pure-control-group">
              <label>Username</label>
              <input name="username" ng-model="user.username" type="text" placeholder="Username" required>
              <div class="field-message" ng-messages="frm.username.$error" ng-if='frm.username.$dirty' ng-cloak>
                <div ng-message="required">Username is required</div>
              </div>
          </div>

          <div class="pure-control-group">
              <label>Password</label>
              <input name="password"
              ng-model="user.password"
              type="password"
              placeholder="Password"
              required
              ng-minlength="6"
              ng-maxlength="10">
              <div class="field-message" ng-messages="frm.password.$error" ng-if='frm.password.$dirty' ng-cloak>
                <div ng-message="required">Password is required</div>
                <div ng-message="minlength">Password must have minimum 6 characters</div>
                <div ng-message="maxlength">Password must have maximum 10 characters</div>
              </div>
          </div>

          <div class="pure-control-group">
              <label>Email Address</label>
              <input name="email"
                ng-model="user.email"
                type="email"
                placeholder="Email Address"
                required>
                <div class="field-message" ng-messages="frm.email.$error" ng-if='frm.email.$dirty' ng-cloak>
                  <div ng-message="required">Email is required</div>
                  <div ng-message="email">Must be a valid email</div>
                </div>
          </div>

          <div class="pure-controls">
              <label class="pure-checkbox">
                  <input name="conditions" ng-model="conditions" type="checkbox">
                  I've read the terms and conditions
              </label>
              <button type="submit" class="pure-button pure-button-primary"
               ng-disabled="frm.$invalid || !conditions">Submit</button>
          </div>
      </fieldset>
  </form>
</div>

任何人都有此信息?没有运气找到这方面的文件。

1 个答案:

答案 0 :(得分:12)

AngularJs ngForm direcive不要求该元素为<form>,如果您愿意,可以使用ng-form中的div。例如,从您的小提琴中取出,您可以使用div替换之前的form元素。

<div ng-form="frm" class="pure-form pure-form-aligned">
...
</div>

完整的工作片段:

angular.module('DemoApp', ['ngMessages']);

angular.bootstrap(document.getElementById('demoApp'), ['DemoApp']);
@import "http://yui.yahooapis.com/pure/0.6.0/pure-min.css";
 .container {
  width: 970px;
  padding-top: 10px;
  margin: 0 auto;
}
.pure-form input.ng-dirty.ng-invalid {
  border-color: #e9322d;
}
.field-message {
  display: inline-block;
  color: #e9322d;
  font-size: 90%;
  margin-left: 0.5em;
}
[ng\:cloak],
[ng-cloak],
[data-ng-cloak],
[x-ng-cloak],
.ng-cloak,
.x-ng-cloak {
  display: none !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-messages.js"></script>
<div class="container" id="demoApp">
  <div ng-form="frm" class="pure-form pure-form-aligned" autocomplete="off">
    <fieldset>

      <div class="pure-control-group">
        <label>Username</label>
        <input name="username" ng-model="user.username" type="text" placeholder="Username" required>
        <div class="field-message" ng-messages="frm.username.$error" ng-if='frm.username.$dirty' ng-cloak>
          <div ng-message="required">Username is required</div>
        </div>
      </div>

      <div class="pure-control-group">
        <label>Password</label>
        <input name="password" ng-model="user.password" type="password" placeholder="Password" required ng-minlength="6" ng-maxlength="10">
        <div class="field-message" ng-messages="frm.password.$error" ng-if='frm.password.$dirty' ng-cloak>
          <div ng-message="required">Password is required</div>
          <div ng-message="minlength">Password must have minimum 6 characters</div>
          <div ng-message="maxlength">Password must have maximum 10 characters</div>
        </div>
      </div>

      <div class="pure-control-group">
        <label>Email Address</label>
        <input name="email" ng-model="user.email" type="email" placeholder="Email Address" required>
        <div class="field-message" ng-messages="frm.email.$error" ng-if='frm.email.$dirty' ng-cloak>
          <div ng-message="required">Email is required</div>
          <div ng-message="email">Must be a valid email</div>
        </div>
      </div>

      <div class="pure-controls">
        <label class="pure-checkbox">
          <input name="conditions" ng-model="conditions" type="checkbox">I've read the terms and conditions
        </label>
        <button type="submit" class="pure-button pure-button-primary" ng-disabled="frm.$invalid || !conditions">Submit</button>
      </div>
    </fieldset>
  </div>
</div>