简体   繁体   English

使用React Select的自定义验证消息

[英]Custom validation message with React Select

I want to internationalize the form validation messages. 我想国际化表单验证消息。 I managed to do that for standard inputs using this solution . 我设法使用此解决方案对标准输入执行此操作 For React Select, though, it's behaving a little differently. 但是,对于React Select,它的行为有所不同。 Apparently , all that is needed is to override onChange and onInvalid inside an inputProps prop. 显然 ,所有需要的是覆盖onChangeonInvalid内部inputProps道具。

However, it seems that the embedded <input/> doesn't fire its onChange function when its value changes. 但是,似乎嵌入式<input/>的值更改时不会触发其onChange函数。 That means that, once the Select is invalid, it never changes back to valid. 这意味着,一旦Select无效,它就永远不会变回有效状态。 Another thing that might be related is that the embedded <input/> 's value is null after an option is selected. 可能相关的另一件事是,在选择选项后,嵌入的<input/>的值为null

Here is a CodeSandbox that shows how far I've got. 是一个CodeSandbox,显示了我走了多远。 The first Select works as expected, but with the default validation message. 第一个Select可以正常工作,但是带有默认的验证消息。 The second Select have a custom message, but doesn't work as expected. 第二个Select具有自定义消息,但不能按预期方式工作。 The <input/> is there to show that onChange is not being called in the embedded <input/> . <input/>表示在嵌入式<input/>未调用onChange

Please let me know if it's possible to make it behave as expected. 请让我知道是否有可能使其表现出预期的效果。

Thanks in advance. 提前致谢。

I managed to do something usable but, as I said in comments, it's not pretty. 我设法做了一些有用的事情,但是,正如我在评论中说的那样,这并不漂亮。 If that link goes dead, it involves setting required to true / false in order to try to make the message appear only once, and using setCustomValidity whenever the message inevitably appears. 如果该链接失效,则需要将required设置设置为true / false ,以尝试使消息仅显示一次,并在不可避免地出现消息时使用setCustomValidity

I'll raise an issue in React Select because I think there has to be a better way, as it's shown in the example with pure <input/> . 我将在React Select中提出一个问题,因为我认为必须有更好的方法,如示例中使用纯<input/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM