如何验证一个数字是否大于其他内部形式规则?
表单包含两个项目,这两个项目都是接受一个数字的输入字段。我需要检查第一个输入(start)中输入的值是否总是小于第二(end),否则请显示验证错误。
以上是ANTD表单中的包装器,每个包装器都是form.item
。 我需要这样的显示验证消息,
const App = () => {
const [start, setStart] = useState(1);
const [end, setEnd] = useState(2);
const formRef = useRef();
const handleSubmit = () => {
if (end < start) {
console.log("End cannot be lesser than start");
} else if (end === start) {
console.log("End cannot be same as start");
} else {
console.log("Added :)", start, end);
}
};
return (
<Form
onFinish={handleSubmit}
layout="vertical"
ref={formRef}
requiredMark={false}
>
<div>
<Form.Item
name="formStart"
rules={[
{
required: true,
message: "Please select a form start"
}
]}
>
<InputNumber
placeholder="Start"
min={1}
max={100}
onChange={(e) => setStart(e)}
/>
</Form.Item>
<Form.Item
name="formEnd"
rules={[
{
required: true,
message: "Please select a form end"
}
]}
>
<InputNumber
placeholder="End"
min={1}
max={100}
onChange={(e) => setEnd(e)}
/>
</Form.Item>
<Form.Item>
<button htmlType="submit">Submit</button>
</Form.Item>
</div>
</Form>
);
};
.io/s/basic-usage-antd-4-21-0-forked-4sri66?file=/demo.js" rel="nofollow noreferrer">https://codesandbox.io/s/basic-usage-antd -4-21-0-Forked-4SRI66?file =/demo.js
Form contains two items, both of it is an input field that accepts a number. I need to check if the value entered in the first input (start) is always less than the second (end) or else, show a validation error.
The above is wrapper inside antd form and each of it is a Form.Item
.
I need show validation message like this,
Code:
const App = () => {
const [start, setStart] = useState(1);
const [end, setEnd] = useState(2);
const formRef = useRef();
const handleSubmit = () => {
if (end < start) {
console.log("End cannot be lesser than start");
} else if (end === start) {
console.log("End cannot be same as start");
} else {
console.log("Added :)", start, end);
}
};
return (
<Form
onFinish={handleSubmit}
layout="vertical"
ref={formRef}
requiredMark={false}
>
<div>
<Form.Item
name="formStart"
rules={[
{
required: true,
message: "Please select a form start"
}
]}
>
<InputNumber
placeholder="Start"
min={1}
max={100}
onChange={(e) => setStart(e)}
/>
</Form.Item>
<Form.Item
name="formEnd"
rules={[
{
required: true,
message: "Please select a form end"
}
]}
>
<InputNumber
placeholder="End"
min={1}
max={100}
onChange={(e) => setEnd(e)}
/>
</Form.Item>
<Form.Item>
<button htmlType="submit">Submit</button>
</Form.Item>
</div>
</Form>
);
};
If there is an alternative instead of using setState, It can also use ref (formRef.current)
Code sandbox: https://codesandbox.io/s/basic-usage-antd-4-21-0-forked-4sri66?file=/demo.js
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于自定义验证或有条件验证我们需要在我们的
'valivator'
中包含规则
,如antd
文档。使用这种方法,您可以在不使用内部状态的情况下实现上述验证,也可以无需
useref
。使用ANTD表单时
form.useform
创建形式实例以维护数据存储的几点点。这样,我们可以轻松地获得当前的表单状态,例如值,错误等。form.submit()
函数,它触发了给定函数的onfinish
。因此,我们可以将其用于提交BTN OnClick。这是完整的
检查此 form/api 有关更多
antd form
prop and prop and函数。For custom validations or conditional validations we need to include
'validator'
inside ourrules
as below as per theantd
docs.With this approach, you can achieve above validation without use of internal state and also without
useRef
.Few points for when use antd form
Form.useForm
which create Form instance to maintain data store. With this we can easily get the current form state like values, errors etc.form.submit()
function which trigger the given function for FormonFinish
. So we can use that for SUBMIT Btn onClick.Here is the full demo code in codesandbox.
Check this form/API for more
antd Form
props and functions.您可以使用
useref
:定义参考变量:
将refs传递给输入:
访问输入值:
您还可以通过添加
defaultValue
属性来设置默认值。例如:You can use
useRef
:Define ref variables:
Pass the refs to the inputs:
Access the input values:
You can also set the default values by adding the
defaultValue
attribute. For example: