简体   繁体   English

如何使用$ pristine和$ dirty在angular中验证表单?

[英]how to validate form using $pristine and $dirty in angular?

I made a form using this link https://github.com/nimbly/angular-formly and this http://nimbly.github.io/angular-formly/#!/.I I need to validate form .But most of the validation done but angular .But it validate form is not user friendly.I need to validate form using this $pristine and $dirty and change some text as given in default string. 我使用此链接https://github.com/nimbly/angular-formlyhttp://nimbly.github.io/angular-formly/#!/制作了表单。我需要验证表单。但是大多数验证完成但是有角度。但是它验证表单不是用户友好。我需要使用$ pristine和$ dirty来校验表单并更改默认字符串中给出的一些文本。 Plunker http://plnkr.co/edit/7IU7WNKjS6NgTus537K5?p=preview 柱塞http://plnkr.co/edit/7IU7WNKjS6NgTus537K5?p=preview

// Code goes here

angular.module('test', ['formly']);

angular.module('test')
  .controller('TestCtrl', function ($scope) {
    $scope.formFields = [
      {
        "key": "firstName",
        "type": "text",
        "label": "First Name",
        "placeholder": "Jane",
        "required":true
    },{
        "key": "email",
        "type": "email",
        "placeholder": "janedoe@gmail.com",
        "required":true
    },
          {
        "key": "triedEmberSelect",
        "type": "select",
        "label": "Have you tried EmberJs yet?",
        "default": "no",
        "options": [
            {
                "name": "Yes, and I love it!",
                "value": "yesyes"
            },
            {
                "name": "Yes, but I'm not a fan...",
                "value": "yesno"
            },
            {
                "name": "Nope",
                "value": "no"
            }
        ]
    },
              {
        "key": "triedEmberRadio",
        "type": "radio",
        "label": "Have you tried EmberJs yet?",
        "default": "no",
        "options": [
            {
                "name": "Yes, and I love it!",
                "value": "yesyes"
            },
            {
                "name": "Yes, but I'm not a fan...",
                "value": "yesno"
            },
            {
                "name": "Nope",
                "value": "no"
            }
        ]
    }
      ];

          $scope.formOptions = {

        //Set the id of the form
        uniqueFormId: 'myFormId',

        //Hide the submit button that is added automaticaly
        //default: false
        hideSubmit: false,

        //Set the text on the default submit button
        //default: Submit
        submitCopy: 'Login'
    };

    $scope.onSubmit = function() {
        console.log('form submitted:', $scope.result);
    };
      $scope.result = {};
      $scope.okbuttonClick=function () {
        // body...
        console.log($scope.result)
      }
  });

is there any way to validate like that ? 有什么办法可以验证吗? You name is required. 您的姓名为必填项。

can we validate this form like that http://scotch.io/tutorials/javascript/angularjs-form-validation http://code.realcrowd.com/on-the-bleeding-edge-advanced-angularjs-form-validation/ 我们可以验证这种形式吗? http : //scotch.io/tutorials/javascript/angularjs-form-validation http://code.realcrowd.com/on-the-bleeding-edge-advanced-angularjs-form-validation/

i need to validate like that http://angular-js.in/tag/form/ 我需要像这样验证http://angular-js.in/tag/form/

Form "validation" in Angular is not exactly the same thing as what you might expect. Angular中的表单“验证”与您期望的不完全相同。 Angular does validate per your requirements but it does not, by default, do anything ABOUT the validation. Angular会根据您的要求进行验证,但是默认情况下,它不做任何有关验证的事情。 In your plunkr, if you inspect one of the input fields, you will notice the CSS class names are changing on it as you edit. 在您的plunkr中,如果检查输入字段之一,则会注意到在编辑时CSS类名称正在更改。 ng-pristine, ng-dirty, ng-valid, ng-invalid, etc etc. It's up to you to modify your CSS so that this does something visually, Angular will not automatically display any visual errors. ng-pristine,ng-dirty,ng-valid,ng-invalid等。您可以自行修改CSS,以使其在视觉上起作用,Angular不会自动显示任何视觉错误。

Also, Angular does not prevent you from submitting an invalid form. 另外,Angular不会阻止您提交无效的表单。 Your code must check the form to see if it is $invalid. 您的代码必须检查表单以查看其是否为$ invalid。 You can do this in the submit function (recommended), you could disable the submit button if $invalid evaluates to true (also recommended but in addition to in the submit function). 您可以在Submit函数中进行此操作(推荐),如果$ invalid的值为true,则可以禁用Submit按钮(也建议这样做,但在commit函数中也可以)。 You can use ng-show/ng-if/etc directives to show/hide validation errors based on the state of the form and/or fields. 您可以使用ng-show / ng-if / etc指令根据表单和/或字段的状态显示/隐藏验证错误。

Essentially, Angular validation provides the mechanisms to validate the value of individual form elements. 本质上,角度验证提供了验证单个表单元素的值的机制。 It is up to you to decide what to do with it since your business logic is bound to vary. 由于您的业务逻辑必然会发生变化,因此您应自行决定如何处理它。 Your UI needs, too, may vary as some forms wait for the user to submit it before displaying errors, some need to hit ajax endpoints to validate, and others provide feedback per-field as users navigate. 您的UI需求也可能会有所不同,因为某些表单在显示错误之前会等待用户提交,某些表单需要击中Ajax端点以进行验证,而其他表单则在用户导航时按字段提供反馈。 You'll have to implement that functionality yourself or find a third-party module that does it for you. 您必须自己实现该功能或找到一个为您实现此功能的第三方模块。 I don't have experience with any of those, but hopefully this helps fill in some of the blanks for you. 我没有任何经验,但是希望这可以帮助您填补一些空白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM