【发布时间】:2018-06-25 11:13:24
【问题描述】:
我正在使用 NGX-Schema-Form https://github.com/makinacorpus/ngx-schema-form 来呈现基于 json 对象的表单字段。
我的问题是:如何将自定义验证错误消息添加到我的架构中,以及如何在 ui 上正确显示它们 (Bootstrap 4.1 based)。
这是标准模式格式的示例:
注意: 模式、minLength、maxLength
mySchema = {
"properties": {
"cellphoneNumber": {
"type": "string",
"title": "Cellphone number",
"placeholder": "e.g. 082 320 2121",
"widget": {
"id": "string"
},
"pattern": "^\\d{10}$",
"minLength": 5,
"maxLength": 12
}
}
但我想做什么:
mySchema = {
"properties": {
"cellphoneNumber": {
"type": "string",
"title": "Cellphone number",
"placeholder": "e.g. 082 320 2121",
"widget": {
"id": "string"
},
pattern: {
value: '^\\d{10}$',
message: 'Cellphone number must be 10 digits'
},
minLength: {
value: '5',
message: 'Please enter at least 5 characters'
}
maxLength: {
value: '14',
message: 'Please enter no more than least 14 characters'
}
}
}
有什么想法吗?
如果表单和字段是 ng-dirty 和 ng-touched,如何覆盖标准字符串小部件以显示此错误。
粗略的例子:
<div class="error mt-2" *ngIf="control.errors && control.pristine == false || control.touched == true">
<div *ngFor="let err of control.errors">
{{ err.message }}
</div>
</div>
我正在为企业级实施寻找可靠的解决方案。
【问题讨论】:
-
您有没有尝试过实现这一目标?
-
是的,我已尝试像在粗略示例中那样显示适当的消息,但返回的错误是标准角度模式、minLength、maxLength 错误消息。:
[ { "code": "PATTERN", "params": [ "^\\d{10}$", "" ], "message": "String does not match pattern ^\\d{10}$: ", "path": "#/" }, -
我创建了自己的 stringWidget,它覆盖了标准 stringWidget,但我不确定如何解决架构错误验证和向这个覆盖模板发送消息的问题。
-
@Jean-pauldeBeer 你解决了吗?
标签: angular forms jsonschema