简体   繁体   English

使用availity-reactstrap-validation,我在点击提交时运行一个onClick事件,这取消了实际的验证

[英]Using availity-reactstrap-validation, I run an onClick event when hitting submit, and this is cancelling out the actual validation

Just to preface this, I'm learning from scratch so please be gentle.只是作为序言,我从头开始学习,所以请保持温和。 I've tried for a few days to work this out and this is my last resort (I like to try and work it out myself where possible).我已经尝试了几天来解决这个问题,这是我最后的手段(我喜欢在可能的情况下尝试自己解决)。

I have the following code in a Reactstrap modal (just the relevant part):我在 Reactstrap 模态中有以下代码(只是相关部分):

Modal.js Modal.js

                  const { toggle, onSave } = this.props;
                      return ( 
                         <ModalBody>
                          <AvForm> 
                           <AvGroup>
                            <Label for="description">Description</Label>
                            <AvInput
                                type="text"
                                name="description"
                                value={this.state.activeItem.description}
                                onChange={this.handleChange}
                                placeholder="Tool description"
                                required
                            />
                            <AvFeedback valid>
                                Looking good
                            </AvFeedback>
                            <AvFeedback>
                                Uh oh! Looks like a blank string
                            </AvFeedback>

and a button:

                            <Button onClick={() => onSave(this.state.activeItem)}>
                              Save
                            </Button>
                      </AvForm>
                     </ModalBody>

App.js (again, the relevant parts, I think) App.js (再次,相关部分,我认为)

             toggle = () => {
               this.setState({ modal: !this.state.modal });
             };

             handleSubmit = item => {
             this.toggle();
             if (item.id) {
               axios
                 .put(`http://localhost:8000/api/todos/${item.id}/`, item)
                 .then(res => this.refreshList());
               return;
           }
           axios
             .post("http://localhost:8000/api/todos/", item)
             .then(res => this.refreshList());
           };

render() {
  return( 

       ...

     {this.state.modal ? (
          <Modal
            activeItem={this.state.activeItem}
            toggle={this.toggle}
            onSave={this.handleSubmit}
          />
        ) : null}

    ...

If I remove the onClick event, the form validation works as you'd expect.如果我删除 onClick 事件,表单验证将按您的预期工作。 I'm just not sure how to tell it to first perform the validation, and if it passes then go ahead and submit, otherwise display the validation warnings and leave the form open...我只是不知道如何告诉它首先执行验证,如果它通过然后继续提交,否则显示验证警告并保持表单打开......

Thanks a lot for the help!非常感谢您的帮助!

Try to use onValidSubmit of AvForm.尝试使用 AvForm 的 onValidSubmit。 According to documentation, this callback is called only when every field is valid when submitted:根据文档,只有在提交时每个字段都有效时才会调用此回调:

handleSubmit(){
  onSave(this.state.activeItem)
}

<AvForm onValidSubmit={this.handleSubmit}>
...
</AvForm>

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

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