2

I am working on project in reactjs, I implemented some validation in form. I implemented a logic if someone click on next without uploading photo and video then display error to user , validation is working fine in first attempt it working but when user delete a photo or video then click on next the form is not validating . Could you please help me how to validate form I am using ant-design form I will share code with you please help me i am really stuck

Please View Image of Form

Code

 <Form onSubmit={this.handleSubmit}>
      <FormItem>
        {getFieldDecorator('picture', {
          rules: [
            {
              required: true,
              message: 'Please upload picture!',
            },
          ],
        })(
          <Dragger {...props}>
            <p className="ant-upload-drag-icon">
              <Icon type="upload" />
            </p>
            <p className="ant-upload-text">
              Click or drag photo to this area to upload
            </p>
          </Dragger>,
        )}
      </FormItem>

      <PhotoText>Select a Video to Upload</PhotoText>
      <FormItem>
        {getFieldDecorator('video', {
          rules: [
            {
              required: true,
              message: 'Please upload video!',
            },
          ],
        })(
          <Dragger>
          <p className="ant-upload-drag-icon">
            <Icon type="upload" />
          </p>
          <p className="ant-upload-text">
            Click or drag Video to this area to upload
          </p>
        </Dragger>,
        )}
      </FormItem>
    </Form>
Jon
  • 461
  • 2
  • 7
  • 17

1 Answers1

1

Since, you are using wizard form so you need to pass this.props.form in each steps as given below:

<Step1 form={this.props.form} />

And then in next(), you can use validateFieldsAndScroll to check for the validation as given below:

next() {
    if (this.state.current == 0) {
        this.props.form.validateFieldsAndScroll(["picture"], (err, values) => {
            if (!err) {
                const current = this.state.current + 1;
                this.setState({ current });
            }
        });
    }
}

I have created a working demo.

Edit

Then, you need to write your own logic in some way because I don't think Ant-design provides such functionality. For example, instead of if (!err) you can write as follows:

if (!err && values.picture && values.picture.fileList.length > 0)

And then you can write some logic to display custom validation message as:

{picture && picture.fileList.length <= 0 ? "Please upload picture!" : ""}

Please check the updated demo.

Triyugi Narayan Mani
  • 3,039
  • 8
  • 36
  • 56