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>
4
  • Please help me ? Commented Dec 5, 2018 at 9:17
  • Can you please add your submit function too or create a sandbox? Commented Dec 5, 2018 at 9:28
  • @TriyugiNarayanMani. Actually I am working on Wizard Form . {current < this.steps.length - 1 && ( <Button type="primary" htmlType="submit" style={{ background: '#ff9700', fontWeight: 'bold', border: 'none', float: 'right', }} > Next <Icon type="right" /> </Button> )} Commented Dec 5, 2018 at 9:31
  • Thank You please help me it really urgent . We have some deadline that's why Commented Dec 5, 2018 at 9:35

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

it doesnt work, if user uploads and then deletes the picture the form indeed goes to next step isntead of displaying error message and staying in current step
@NikosM. yes Validation is already implemented but when user delete image it going to next
@NikosM. Could you please help me
@NikosM. Updated the answer.
@TriyugiNarayanMani Thank You it working , please also have a look to this question ( stackoverflow.com/questions/53631198/… )
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.