{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","params":[],"results":{"codes":[]}},"next":{"description":"","pages":[]},"title":"formlyValidationMessages","type":"basic","slug":"formlyvalidationmessages","excerpt":"service","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]","updates":[],"order":3,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"558cb2bd0b236c2500d37cef","user":"54e3723b8ef7552300409bf4","createdAt":"2015-03-28T18:24:28.238Z","githubsync":"","category":{"sync":{"isSync":false,"url":""},"pages":["558cb2bd0b236c2500d37cec","558cb2bd0b236c2500d37ced","558cb2bd0b236c2500d37cee","558cb2bd0b236c2500d37cef"],"title":"API","slug":"api","order":3,"from_sync":false,"reference":false,"_id":"558cb2bc0b236c2500d37cd6","createdAt":"2015-03-28T04:12:21.318Z","project":"5515ba4981faf83900d2b10c","version":"558cb2bc0b236c2500d37cd4","__v":1},"project":"5515ba4981faf83900d2b10c","version":{"version":"6.16.0","version_clean":"6.16.0","codename":"","is_stable":false,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["558cb2bc0b236c2500d37cd5","558cb2bc0b236c2500d37cd6","558cb2bc0b236c2500d37cd7","558cb2bc0b236c2500d37cd8","558cb2bc0b236c2500d37cd9","558cb2bc0b236c2500d37cda"],"_id":"558cb2bc0b236c2500d37cd4","project":"5515ba4981faf83900d2b10c","releaseDate":"2015-06-26T02:02:36.211Z","__v":1,"createdAt":"2015-06-26T02:02:36.211Z","forked_from":"558a9c9f8d30cb2300873ecd"},"__v":0}

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]