-
有需求场景如下,rules校验失败,子元素需要显示一个红色的边框 <Form.Item
name="mobile"
rules={[{ validator: checkPhone }]}
>
<Input placeholder="联系电话" />
</Form.Item> |
Beta Was this translation helpful? Give feedback.
Answered by
awmleer
Jun 20, 2022
Replies: 3 comments 6 replies
-
写了一个满足某个校验规则时,字体变大的 export default () => {
const onFinish = (values: any) => {
console.log(values)
}
const [isBigfontSize, setFontSize] = useState(false)
const checkMobile = (_: any, value: string) => {
if (value === '111') {
setFontSize(true)
return Promise.resolve()
}
setFontSize(false)
return Promise.reject(new Error('现在输入的不是111'))
}
return (
<>
<Form
layout='horizontal'
onFinish={onFinish}
footer={
<Button block type='submit' color='primary' size='large'>
提交
</Button>
}
>
<Form.Item
label='手机号'
name='mobile'
rules={[{ validator: checkMobile }]}
>
<Input
placeholder='输入111时字体会变大'
style={{
'--font-size': `${!isBigfontSize ? '12px' : '20px'}`,
}}
/>
</Form.Item>
</Form>
</>
)
} |
Beta Was this translation helpful? Give feedback.
1 reply
-
@yangdepp @awmleer 我新接手了一个项目,就是上面demo的那种写法。其实这种写法还是没有实际上解决【子元素无法感知rules的校验结果】这个问题,它是依靠额外的状态来处理的。另外这里如果是多个字段,多个校验的情况下,这种写法的弊端和维护性无疑是更难以把握的。是否可以考虑用render props的方式来实现。如下: <Form.Item
name="mobile"
rules={[{ validator: checkPhone }]}
>
{({ value, onChange, error }) => (
<div>
<Input
placeholder="联系电话"
style={error ? errorStyle: {}}
value={value}
onChange={onChange}
/>
{error && <div>error.message</div>}
</div>
)}
</Form.Item> |
Beta Was this translation helpful? Give feedback.
4 replies
-
写了个 demo,应该解决了你说的问题 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
awmleer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
写了个 demo,应该解决了你说的问题