我如何根据按钮单击验证表单字段?

How can i validate form fields based on button click?

我有 3 个按钮。 2 在 ng-repeat 里面,一个在 ng-repeat 外面,所以 如果用户单击这些按钮,我想显示必填字段警报。

如果用户单击“检查 0”,我只需要验证第一个对象,如果缺少任何表单数据值,我必须提醒用户“这个(用户名)是必填字段。

如果用户单击“检查 1”按钮,我只需要验证第二个对象,如果缺少任何表单数据值,我必须提醒用户“这个(用户名)是必填字段。

如果用户单击“检查所有”按钮,我必须检查两个对象,如果两个对象中缺少任何字段,我必须用对象索引提醒字段名称。

如果用户单击“全部选中”按钮和“选中”按钮,我如何显示必填字段请帮助我

Demo

var app = angular.module("app", ['ngMessages']);
app.controller("myCtrl", function($scope) {
    $scope.users = [
        {
        "inputName":"aj",
        "inputPassword":"1234",
        "inputEmail":"aj@g.in",
        },{
        
        "inputName":"gj",
        "inputPassword":"1234",
        "inputEmail":"gj@g.in",
        }
    ];
     $scope.myFunc = function(formValidation) {
        console.log(formValidation)
    };
    $scope.checkall = function(formValidation) {
        console.log(formValidation)
    };
});
<body translate="no" >
<button ng-click="checkall(formValidation)">check all</button>
  <body ng-app="app" ng-controller="myCtrl" >
<div ng-repeat="user in users">
  <script type="text/ng-template" id="generic-messages">
    <p ng-message="required">This field is required.</p>
    <p ng-message="minlength">This field is too short.</p>
    <p ng-message="maxlength">This field is too long.</p>
  </script>


  <form name="formValidation">
<button ng-click="myFunc(formValidation)">check {{$index}}</button>
    <label>Username (Using Dirty)</label>
    <input type="text" name="username" ng-model="user.inputName" ng-minlength="6" ng-maxlength="12" ng-pattern="/^\w+$/" required>
    <div ng-messages="formValidation.username.$error" ng-show="formValidation.username.$dirty">
      <p ng-message="pattern">Username can only be alphanumeric with an optional underscore.</p>
      <p ng-message="maxlength">Username cannot be longer than 12 characters.</p>
      <div ng-messages-include="generic-messages"></div>
    </div>

    <label>Password (Using Touched)</label>
    <input type="text" name="userPassword" ng-model="user.inputPassword" ng-minlength="6" ng-maxlength="12" required>
    <div ng-messages="formValidation.userPassword.$error" ng-show="formValidation.userPassword.$touched">
      <div ng-messages-include="generic-messages"></div>
    </div>

    <label>Email (Using Dirty)</label>
    <input type="email" name="userEmail" ng-model="user.inputEmail" required>
    <div ng-messages="formValidation.userEmail.$error" ng-show="formValidation.userEmail.$dirty">
      <p ng-message="required">This field is required.</p>
      <p ng-message="email">Please enter a valid email address.</p>
    </div>
  </form>
  </div>
</body>

您应该分开表格,以便控制每个表格。

创建一个数组变量$scope.forms = [];,并使用$index设置每个表单的名称:

<form name="forms[{{$index}}]">

现在您可以使用 $index 控制每个表单:

<button ng-click="myFunc($index)">check {{$index}}</button>

并用它来显示错误或其他任何内容:

$scope.myFunc = function(formIndex) {
    console.log(formIndex);
    console.log($scope.forms[formIndex]);
};

$setSubmitted()也可以用于不同的情况。

demo 如果您需要进一步的帮助,请告诉我

编辑

单击 'check all' 时,一种方法是将所有表单设置为已提交并显示字段错误:

$scope.checkall = function(form) {
    $scope.forms[0].$setSubmitted();
    $scope.forms[1].$setSubmitted();
    console.log('form 1 is valid: ', $scope.forms[0].$valid);
    console.log('form 1 username field', $scope.forms[1].username);
    console.log('form 2 is valid: ', $scope.forms[1].$valid);
};

您可以像这样访问每个表单(如果您需要动态方法,也可以在 $scope.forms 上循环):

$scope.forms[0]

或表单字段及其属性:

$scope.forms[1].username

为了在点击'check all'按钮后显示错误信息,我把每个字段的条件改成这样:

ng-show="forms[$index].$submitted"

在此处查看更新的演示:DEMO