如何在必需和无效上显示不同的消息? [英] how to show different message on required and invalid?

查看:22
本文介绍了如何在必需和无效上显示不同的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能告诉我如何在必需和无效时显示不同的消息吗?换句话说,使用插件从 json 制作一个表单.其中有一些必需的参数.还有一些我需要验证示例电子邮件".当如果用户没有填写该字段,则用户按下提交"按钮,它会显示此字段是必需的".如果用户填写了值但不是相同的模式,则它会说无效值".我们可以在不同的情况下显示不同的消息.

can you please tell me how to show different message on required and invalid ?In other word .make a form from json using plugin .In that there are some required parameter .and some I need to validate example "Email".When user press "submit" button if user did not fill the field it show "this field is required" .and if user fill the value but not same patten than it say "invalid value".can we show different message as different situation .

我用过这个插件https://github.com/Textalk/angular-schema-form/blob/master/docs/index.md#validation-messages我做了这个 plunkerhttp://plnkr.co/edit/ZNJO3x3IqajjdMNStJMF?p=preview

i used this plugin https://github.com/Textalk/angular-schema-form/blob/master/docs/index.md#validation-messages and I make this plunker http://plnkr.co/edit/ZNJO3x3IqajjdMNStJMF?p=preview

angular.module('test',['schemaForm']).controller('FormController', function($scope,$http){
   $scope.schema = {
    type: "object",
    properties: {
      name: { type: "string", minLength: 2, title: "Name", description: "Name or alias" ,required:true,"default": "dddd"},
      "student": { type: "string", title: "studentname", description: "Name or student" ,required:false},

      "email": {
      "title": "Email",
      "type": "string",
       "pattern":"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",

      "description": "Email will be used for evil.",
      required:true
    },
      title: {
        type: "string",
        required:true,
        enum: ['dr','jr','sir','mrs','mr','NaN','dj'],

      }
    }
  };

  $scope.form = [
    "*",
    {
      type: "submit",
      title: "Save"
    }
  ];

推荐答案

正如你从 官方 angular-schema-form 文档

默认情况下,所有错误消息都来自架构验证器 tv4,这可能适合您,也可能不适合.如果您在表单定义中提供了一个validationMessage 属性,并且它的值是一个字符串,该字符串将用于任何验证错误.

Per default all error messages comes from the schema validator tv4, this might or might not work for you. If you supply a validationMessage property in the form definition, and if its value is a string that will be used instead on any validation error.

如果你需要更细粒度的控制,你可以提供一个对象,而不是与 tv4 的错误代码匹配的键.参见 tv4.errorCodes

例如

{
  key: "address.street",
  validationMessage: {
    tv4.errorCodes.STRING_LENGTH_SHORT: "Address is too short, man.",
    "default": "Just write a proper address, will you?"   //Special catch all error message
  }
}

您还可以在 formDefaults 中设置全局验证消息,请参阅全局选项.

You can also set a global validationMessage in formDefaults see Global Options.

有关更多错误消息,请访问此链接

So for more error messages, visit this link

这篇关于如何在必需和无效上显示不同的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆