OnValidate 进行更改是否为时已晚?
我看到的每个 OnValidate 示例本质上都是只读的(检查值并抛出异常)。我想更进一步,修改值(可以进行明确的修复)作为验证过程的一部分。 OnValidate
回调在管道中进行修改是否太晚了(不破坏其他假设)?或以其他方式灰心丧气?
对于一个(人为的)示例,假设我要在 OnValidate 中修剪空格或将空字符串合并为空字符串。
Every OnValidate
example I seem to see is essentially read-only (checking values, and throwing exceptions). I would like to go a step further and modify values (where unambiguous fix-up is possible) as part of the validation process. Is the OnValidate
callback too late in the pipeline for making modifications (without breaking other assumptions)? or otherwise discouraged?
For a (contrived) example, suppose I were to trim whitespace or coalesce null strings to empty strings in OnValidate
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜这取决于您希望它如何为用户工作。
您可以在值更改(文本更改等)或项目失去焦点时执行此操作。
我猜 OnValidate 是为了显示总体摘要。
就我个人而言,我更喜欢“显示我的错误,但我进行更改以使其正确”。例如,手机上的预测文本,它的自动更正功能通常是错误的,但也许我只是输入有趣的内容。
Guess it depends on how you want it to work for the user.
You could possibly do it when the value changes (textchanged etc), or when the item loses focus.
OnValidate is for kind of showing an overall summary I guess.
Personally I would prefer that "my error is shown, but I make the change to get it correct".. eg predictive text on mobile phones it's Auto Correct is often wrong, but maybe I just type funny.