{"category":{"__v":1,"_id":"56d91c5507ae190b00004467","pages":["56d91c5807ae190b00004483","56d91c5807ae190b00004484","56d91c5807ae190b00004485","56d91c5807ae190b00004486"],"project":"5515ba4981faf83900d2b10c","version":"56d91c5507ae190b00004460","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-03-28T04:12:21.318Z","from_sync":false,"order":3,"slug":"api","title":"API"},"project":"5515ba4981faf83900d2b10c","user":"54e3723b8ef7552300409bf4","version":{"__v":1,"_id":"56d91c5507ae190b00004460","project":"5515ba4981faf83900d2b10c","createdAt":"2016-03-04T05:25:41.052Z","releaseDate":"2016-03-04T05:25:41.052Z","categories":["56d91c5507ae190b00004464","56d91c5507ae190b00004465","56d91c5507ae190b00004466","56d91c5507ae190b00004467","56d91c5507ae190b00004468"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"8.0.0","version":"8.0.0"},"_id":"56d91c5807ae190b00004486","__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-03-28T18:24:28.238Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":32,"body":"This service allows you to control what messages gets added to each field's `validation.messages` which can ultimately be used in an `ng-messages` context to great effect. It has a `messages` property which is what is used to attach the `messages` to the field's config. The messages here should be set as angular expressions (or functions) similar to how `expressionProperties` or `validators` works. You can always interact with `messages` on your own, but there are two helper methods in this service\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"addTemplateOptionValueMessage\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"formlyValidationMessages.addTemplateOptionValueMessage(name, prop, prefix, suffix, alternate);\\n// for example\\nformlyValidationMessages.addTemplateOptionValueMessage('max', 'max', 'The max value allowed is', '', 'Too big');\\nformlyValidationMessages.addTemplateOptionValueMessage('minlength', 'minlength', '', 'is the minimum length', 'Too short');\\nformlyValidationMessages.addTemplateOptionValueMessage('pattern', 'patternValidationMessage', '', '', 'Invalid Input');\\n\\n// the last could be used like so:\\nvar field = {\\n  type: 'whatever',\\n  templateOptions: {\\n    pattern: /some_crazyPattern/,\\n    patternValidationMessage: '\\\"Needs to match \\\" + options.templateOptions.pattern'\\n  }\\n};\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"addStringMessage\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"formlyValidationMessages.addStringMessage(name, string);\\n// for example\\nformlyValidationMessages.addStringMessage('required', 'This field is required');\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"messages\"\n}\n[/block]\nThe methods above simply create a function and add it to the `messages` property on this service. So, you can do this yourself for maximum flexibility\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"formlyValidationMessages.messages.required = getRequiredMessage;\\n\\nfunction getRequiredMessage($viewValue, $modelValue, scope) {\\n  if (scope.data.useGenericMessage) {\\n    return 'This field is required';\\n  } else {  \\n\\t  return scope.to.label + ' is required';\\n  }\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","excerpt":"service","slug":"formlyvalidationmessages","type":"basic","title":"formlyValidationMessages"}

formlyValidationMessages

service

This service allows you to control what messages gets added to each field's `validation.messages` which can ultimately be used in an `ng-messages` context to great effect. It has a `messages` property which is what is used to attach the `messages` to the field's config. The messages here should be set as angular expressions (or functions) similar to how `expressionProperties` or `validators` works. You can always interact with `messages` on your own, but there are two helper methods in this service [block:api-header] { "type": "basic", "title": "addTemplateOptionValueMessage" } [/block] [block:code] { "codes": [ { "code": "formlyValidationMessages.addTemplateOptionValueMessage(name, prop, prefix, suffix, alternate);\n// for example\nformlyValidationMessages.addTemplateOptionValueMessage('max', 'max', 'The max value allowed is', '', 'Too big');\nformlyValidationMessages.addTemplateOptionValueMessage('minlength', 'minlength', '', 'is the minimum length', 'Too short');\nformlyValidationMessages.addTemplateOptionValueMessage('pattern', 'patternValidationMessage', '', '', 'Invalid Input');\n\n// the last could be used like so:\nvar field = {\n type: 'whatever',\n templateOptions: {\n pattern: /some_crazyPattern/,\n patternValidationMessage: '\"Needs to match \" + options.templateOptions.pattern'\n }\n};", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "addStringMessage" } [/block] [block:code] { "codes": [ { "code": "formlyValidationMessages.addStringMessage(name, string);\n// for example\nformlyValidationMessages.addStringMessage('required', 'This field is required');", "language": "javascript" } ] } [/block] [block:api-header] { "type": "basic", "title": "messages" } [/block] The methods above simply create a function and add it to the `messages` property on this service. So, you can do this yourself for maximum flexibility [block:code] { "codes": [ { "code": "formlyValidationMessages.messages.required = getRequiredMessage;\n\nfunction getRequiredMessage($viewValue, $modelValue, scope) {\n if (scope.data.useGenericMessage) {\n return 'This field is required';\n } else { \n\t return scope.to.label + ' is required';\n }\n}", "language": "javascript" } ] } [/block]