![](/img/trans.png)
[英]react-hook-form: Validation not working when using onBlur mode
[英]react-hook-form Controller onBlur callback not triggering validation
想要在用戶 onBlurs 時修剪輸入字段。
<Controller
...
onBlur={([e]) => {
const { value } = e.target;
const trimmedValue = value.trim();
console.log('trim here: ', value, value.length, trimmedValue.length);
if (trimmedValue === '') {
console.log('error!!!');
return trimmedValue;
}
return trimmedValue;
}} />
規則:
rules={{
pattern: {
value: new RegExp(firstName.validationString, 'i'),
message: 'First name must be 2 - 100 characters with no numbers.',
},
required: firstName.mandatory && 'Must fill in first name',
}}
function 被觸發並到達 if 語句。 但是即使我根據需要設置了規則,也不會觸發錯誤。
在您的寄存器中使用它或 controller
validate: (value) => { return !!value.trim()}
看看這里: https://github.com/react-hook-form/react-hook-form/issues/1650
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.