ANTD打印2个错误,用于使用预定义规则的自定义验证器
我正在使用ReactJ中的ANTD文本区域,并且具有一些预定义的规则,如:
const validationRules = [
{
required: true,
message: 'Required',
},
{
min: MIN_LENGTH,
message: `min ${MIN_LENGTH} characters required`,
},
];
形式项目如下:
<Form.Item name="myField" rules={validationRules}>
<TextArea
className="mt-32"
rows={6}
placeholder={`Your answer should be between ${MIN_LENGTH} to ${MAX_LENGTH} characters`}
minLength={MIN_LENGTH}
maxLength={MAX_LENGTH}
value={value}
/>
</Form.Item>;
现在要添加自定义验证规则,即如果用户进入2个以上的尾部空间,请警告他。
为此,我创建了一个自定义验证器AS:
async function validateEmptySpaces(rule: any, inputValue: string) {
const trimmedValue = inputValue.trim();
const diffInLength = inputValue.length - trimmedValue.length;
if (diffInLength > 1) {
throw new Error("remove empty spaces");
}
}
因此,我最终将此验证器添加到MU规则中,为:
const validationRules = [
{
required: true,
message: "Required",
},
{
min: MIN_LENGTH,
message: `min ${MIN_LENGTH} characters required`,
},
{ validator: validateEmptySpaces },
];
I am using an antd text area in reactjs, and have some predefined rules as :
const validationRules = [
{
required: true,
message: 'Required',
},
{
min: MIN_LENGTH,
message: `min ${MIN_LENGTH} characters required`,
},
];
Form Item is as follows:
<Form.Item name="myField" rules={validationRules}>
<TextArea
className="mt-32"
rows={6}
placeholder={`Your answer should be between ${MIN_LENGTH} to ${MAX_LENGTH} characters`}
minLength={MIN_LENGTH}
maxLength={MAX_LENGTH}
value={value}
/>
</Form.Item>;
Now want to add a custom validation rule i.e. to warn user if he enters more than 2 tailing spaces.
For this I have created a custom validator as:
async function validateEmptySpaces(rule: any, inputValue: string) {
const trimmedValue = inputValue.trim();
const diffInLength = inputValue.length - trimmedValue.length;
if (diffInLength > 1) {
throw new Error("remove empty spaces");
}
}
So I add this validator to mu rules at the end as :
const validationRules = [
{
required: true,
message: "Required",
},
{
min: MIN_LENGTH,
message: `min ${MIN_LENGTH} characters required`,
},
{ validator: validateEmptySpaces },
];
Now this is How I get to see in UI
Here instead of 1 error at a time, it shows both the errors. How can I make it show only one error message at once like:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论