简体   繁体   English

当表单验证出现错误时,如何防止按钮提交表单工作?

[英]How to prevent the button Submit form works when there is an error for form validation?

I have built a form and added validation for this form, required fields.我已经构建了一个表单并为此表单添加了验证,必填字段。 When clicking on the button "Calculate saving ranges" if no entry filled, it still links to the result page.如果没有填写条目,则单击“计算保存范围”按钮时,它仍会链接到结果页面。 How to prevent the button links to the result page when there is an error message displayed, otherwise it works?显示错误信息时如何防止按钮链接到结果页面,否则有效? My code is here我的代码在这里

import React, { Component } from "react";
import "./style.css";
import { Button, Form, Row, Col} from "react-bootstrap";

class Calculator extends Component {
  constructor(props) {
    super(props);
    this.state = {
      numberBuilding: "",
      squareFootage: "",
      tenant: "",
      floor: "",
      bill: "",
      zipcode: "",
      squareFootageError: "",
      floorError: "",
      zipcodeError: ""
    };
  }

  handleSquare = (event) => {
    this.setState({ squareFootage: event.target.value });
  };

  handleFloor = (event) => {
    this.setState({ floor: event.target.value });
  };

  handleZipCode = (event) => {
    this.setState({ zipcode: event.target.value });
  };

  closeModal = () => this.setState({ isOpen: false });
  validate = () =>{
    let squareFootageError = " ";
    let floorError = " ";
    let zipcodeError = " ";

    if (!this.state.squareFootage){
      squareFootageError = "Please input a number, e.g. 12300";
    }

    if (!this.state.floor){
      floorError = "Please input a number of floors, e.g. 12";
    }

    if (!this.state.zipcode){
      zipcodeError = " Please input a ZIP code or PIN code if outside the US e.g., 10023 or L5V1N3";
    }

    if (squareFootageError || floorError || zipcodeError){
      this.setState({squareFootageError, floorError, zipcodeError });
      return false;
    }
    return true;
  }

  handleSubmit = (event) => {
    event.preventDefault();
    const isValid = this.validate();
    if (isValid){
      console.log(this.state);
    }
    this.props.history.push({
      isOpen: true,
      state: {
        value:
          "$" +
          Math.floor(1.69 * this.state.squareFootage * (10 / 100)) +
          "- $" +
          Math.floor(1.69 * this.state.squareFootage * (30 / 100))
      }
    });
  
  };

  render() {
    return (
      <div className="calculator">
          
           {
          /*Condition*/ !this.state.isOpen ? (
            /** if true return form */

            <Form.Group as={Row} controlId="formHorizontalFootage">
              <Form.Label column sm={6}>
                Square footage of buildings*
              </Form.Label>
              <Col sm={6}>
                <Form.Control
                  type="squareFootage"
                  placeholder="sq ft"
                  value={this.state.squareFootage}
                  onChange={this.handleSquare}
                  required
                />
                <div style={{fontSize: 14, color: 'red'}}>{this.state.squareFootageError}</div>
                <Form.Text className="text-muted">
                Please input a number, e.g. 12300
                </Form.Text>
              </Col>
            </Form.Group>

           
            <Form.Group as={Row} controlId="formHorizontalNumber">
              <Form.Label column sm={6}>
                Number of floors*
              </Form.Label>
              <Col sm={6}>
                <Form.Control
                  type="number"
                  placeholder="number"
                  value={this.state.floor}
                  onChange={this.handleFloor}
                  required
                />
                <Form.Text className="text-muted">
                  Total number of floors across all buildings
                </Form.Text>
                <div style={{fontSize: 14, color: 'red'}}>{this.state.floorError}</div>
              </Col>
            </Form.Group>

            

            <Form.Group as={Row} controlId="formPlaintextZip">
              <Form.Label column sm={6}>
                Zip Code*
              </Form.Label>
              <Col sm={6}>
                <Form.Control
                  type="zipcode"
                  placeholder="xxxxx"
                  value={this.state.zipcode}
                  onChange={this.handleZipCode}
                  required
                />
                <div style={{fontSize: 14, color: 'red'}}>{this.state.zipcodeError}</div>
                <Form.Text className="text-muted">
                Please input a ZIP code or PIN code if outside the US e.g., 10023 or L5V1N3
                </Form.Text>
              </Col>
            </Form.Group>

            <Button onClick={this.handleSubmit}>
              Calculate your Savings Range
            </Button>
            <Form.Text className="text-muted">* Required Field</Form.Text>
          </Form>

) : (
  /** else return yourresult div */
  <div className="result">
    <h2>You could save between</h2>
    <h1>{this.state.value}</h1>
    <h2> annually </h2>
    &nbsp;&nbsp;
    <button onClick={this.handle}
    class="btn btn-secondary">
        Use Calculator again</button>
  </div>
)
}


</div>
    );
  }
}

export default Calculator;

You can find my code is here as well: https://codesandbox.io/s/calculation-form-uxip8?file=/src/components/Calculator.jsx你也可以在这里找到我的代码: https : //codesandbox.io/s/calculation-form-uxip8?file=/src/components/Calculator.jsx

Can someone stop by and give me some help?有人可以停下来给我一些帮助吗? Thanks a lot!非常感谢!

You can return if isValid is false .如果isValidfalse您可以返回。

Forked your code here: https://codesandbox.io/s/calculation-form-forked-j3uhz?file=/src/components/Calculator.jsx在这里分叉你的代码: https ://codesandbox.io/s/calculation-form-forked-j3uhz?file =/ src/components/Calculator.jsx

    if (isValid) {
      console.log(this.state);
    } else {
      return;
    }

Also, isValid never becomes true because of these lines:此外,由于这些行, isValid永远不会变为真:

    let squareFootageError = " ";
    let floorError = " ";
    let zipcodeError = " ";

by setting default values to " " (a string composed of a space), this condition: if (squareFootageError || floorError || zipcodeError) always works, and thus validate() never returns true .通过将默认值设置为“”(一个由空格组成的字符串),这个条件: if (squareFootageError || floorError || zipcodeError)始终有效,因此validate()永远不会返回true If it is not clear I can also edit your codesandbox to show you.如果不清楚,我也可以编辑您的代码和框以显示给您。

You shall set default values to empty strings like this:您应将默认值设置为空字符串,如下所示:

    let squareFootageError = "";
    let floorError = "";
    let zipcodeError = "";

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

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