简体   繁体   English

在某些表单字段上进行React-final-form触发器验证

[英]React-final-form Trigger validation on some form fields

I want to validate specific fields on some action. 我想验证某些操作上的特定字段。

I have a form with multiple fields and inside form, there is a section containing 4 fields and a separate button like Test connection . 我有一个包含多个字段的表单,并且在内部表单中,有一个包含4个字段的部分和一个类似Test connection的单独按钮。 So, on pressing that button I want to send an API call but before that, I want to show validation errors on those specific fields. 因此,在按下该按钮时,我想发送一个API调用,但在此之前,我想在这些特定字段上显示验证错误。 I could not find a way to do that. 我找不到办法。 If you guys know, let me know. 如果你们知道,请告诉我。 Thanks in advance. 提前致谢。

 <Form onSubmit={handleSubmit} initialValues={initData} render={({ handleSubmit, form, submitting, pristine, values, }) => { return ( <form onSubmit={handleSubmit}> <div className="form-body"> <h2>New Data Source</h2> <FormGroup> <Label for="name" sm={3}> Name </Label> <Col sm={6}> <Field name="name" placeholder="Name" innerRef={dataSourceNameRef} aria-label="name" component={FormInput} validate={composeValidators(isRequired)} /> </Col> </FormGroup> <div className="grey"> <Row> <Col xs={12} sm={6} md={4}> <FormGroup> <Label for="name">Hostname</Label> <Field name="hostname" placeholder="Hostname" aria-label="hostname" component={FormInput} validate={composeValidators(isRequired)} /> </FormGroup> </Col> <Col xs={12} sm={5} md={2}> <FormGroup> <Label for="port">Port</Label> <Field name="port" placeholder="Port" aria-label="port" component={FormInput} validate={isRequired} /> </FormGroup> </Col> </Row> <Row> <Col xs={12} sm={6} md={4}> <FormGroup> <Label for="name">Username</Label> <Field name="username" placeholder="Username" aria-label="name" component={FormInput} validate={isRequired} /> </FormGroup> </Col> </Row> <Row> <Col xs={12} sm={6} md={4}> <FormGroup> <Label for="name">Password</Label> <Field name="password" placeholder="Password" aria-label="password" type={values.showPassword ? 'text' : 'password'} component={FormInput} validate={composeValidators(isRequired)} /> </FormGroup> </Col> <Col xs={12} sm={6} md={4} className="checkbox-field"> <Field name="showPassword" aria-label="show password" component={FormInput} type="checkbox" label="Show Password" /> <Label for="name"> Show Password</Label> </Col> </Row> <Field name="testConnection" validateFields={['username', 'hostname', 'password']} component={() => ( <Button color="secondary" onClick={() => testConnection(values)} > Test Connection </Button> )} /> </div> </div> <div className="form-footer justify-right"> <Button type="submit" disabled={submitting || pristine} color="primary" > Create </Button> </div> </form> ); }} /> 

Basically, you just have to run your validation and only run the API call if your validations pass. 基本上,您只需要运行验证,并且仅在验证通过时才运行API调用。 There's an example below, but it would be great if you could share your code to help others understand where you're at. 下面有一个示例,但是如果您可以共享代码以帮助其他人了解您的位置,那就太好了。

const handleCick = () => {
  if (invalid) {
    setInvalidPopup(true);
    return;
  }

  //fetch...
};

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

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