简体   繁体   English

具有动态值的角度形式验证消息

[英]angular-formly validation message with dynamic values

I am using angular-formly and is trying to create a validation that will check if the value from the user is between two numbers that will be dynamic set, so it dose not need to be the same all the time. 我正在使用角度形式,并试图创建一个验证,该验证将检查来自用户的值是否在两个将动态设置的数字之间,因此它不必一直都是相同的。 I thouth the easyes way to do this was with a few variables and the validation works but the message skips the variables when the error accrues. 我认为简单的方法是使用几个变量,并且验证有效,但是当错误发生时,消息会跳过变量。

JS Bin of my problem here 我的问题的JS Bin

This is my validation method code: 这是我的验证方法代码:

//Default values can be changed durign runtime
var rangeMin = 0;
var rangeMax = 100;
var value;

vm.fields = [
  {
    key: 'number',
    type: 'input',
    validators: {
      ipAddress: {
        expression: function(viewValue, modelValue) {
          value = modelValue || viewValue;
          var returning = false;
          if(value > rangeMin &&  value < rangeMax)
          {
            returning = true;
          }
          return returning;
        },
        message: '$viewValue + " not in range of " + rangeMin + " and " + rangeMax'
      }
    },
    templateOptions: {
      label: 'Number',
      type: 'text',
    }
  }];

The issue you were having was in the message property. 您遇到的问题在message属性中。 It's a bit confusing with the way angular treats the message, but basically the value of the message needs to be an expression the way you'd see it in an angular template. 与angular处理消息的方式有些混淆,但是基本上,消息的值必须是一个表达式,就像您在angular模板中看到message的方式一样。 As it was your message property evaluated to: 因为它是您的message属性,评估为:

$viewValue + " not in range of " + rangeMin + " and " + rangeMax

And that's what angular was given. 这就是角度。 So angular was able to evaluate $viewValue properly because that's in the field's $scope , however rangeMin and rangeMax are not in the field's $scope , so those evaluated to empty strings. 所以angular可以正确地评估$viewValue ,因为它位于字段的$scope ,但是rangeMinrangeMax不在字段的$scope ,因此将它们评估为空字符串。

So the solution is to reference the rangeMin and rangeMax directly so what you pass to angular doesn't have to evaluate those. 因此,解决方案是直接引用rangeMinrangeMax ,因此传递给angular的对象不必评估它们。

$viewValue + " not in range of 0 and 100"

To do this, your message property should be: 为此,您的message属性应为:

message: '$viewValue + " not in range of ' + rangeMin + ' and ' + rangeMax + '"'

Here's a working example: https://jsbin.com/fupata/edit 这是一个工作示例: https : //jsbin.com/fupata/edit


Note, you can alternatively provide the rangeMin and rangeMax as data properties in your field options like so: https://jsbin.com/baxise/edit?html,js,console,output 请注意,您也可以在字段选项中提供rangeMinrangeMax作为data属性,如下所示: https : rangeMin , rangeMax , rangeMin , rangeMax

This makes it possible to make things a lot more reusable by allowing you to create a custom type to hold all this validation logic like so: https://jsbin.com/teleko/edit?html,js,output 通过允许您创建一个自定义类型来保存所有这些验证逻辑,这样可以使事情变得更加可重用: https : //jsbin.com/teleko/edit?html,js,output

That last one is how I'd do it :-) Good luck 😀 👍 最后那是我要怎么做:-)祝你好运

In this case you can use a directive use-form-error that helps to create your own checks, like the ng-required, ng-minlength or ng-pattern. 在这种情况下,您可以使用指令use-form-error来帮助创建自己的检查,例如ng-required,ng-minlength或ng-pattern。

 angular.module('ExampleApp', ['use', 'ngMessages']) .controller('ExampleController', function($scope) { $scope.min=1; $scope.max=100; }); 
 .errors { color: maroon } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-messages.min.js"></script> <script src="https://cdn.rawgit.com/Stepan-Kasyanenko/use-form-error/master/src/use-form-error.js"></script> <div ng-app="ExampleApp"> <div ng-controller="ExampleController"> <form name="ExampleForm"> <label>Range from</label> <input ng-model="min" required /><label>to</label> <input ng-model="max" required /> <br> <label>Number in range</label> <input ng-model="num" required use-form-error="notInRange" use-error-expression="!(num*1>min*1 && num*1<max*1)" /> <div ng-messages="ExampleForm.$error" class="errors"> <div ng-message="notInRange"> Number not in range of {{min}} and {{max}} </div> </div> </form> </div> </div> 

Live example jsfiddle . 实时示例jsfiddle

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

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