【问题标题】:How to disable form control button due to bootstraptable validation failure由于可引导验证失败,如何禁用表单控制按钮
【发布时间】:2019-10-24 08:02:38
【问题描述】:

我有一个包含表单和引导表的反应组件。该表单包含一个提交按钮,如果表格单元格的输入未能通过验证,我想禁用该按钮。我已经实现了验证器,但我找不到将它们的结果与提交按钮的“禁用”属性联系起来的方法。 以下是完整代码的较短版本:

class Car extends Component {
  constructor(props) {
    super(props);
      this.handleSubmit = this.handleSubmit.bind(this);
      this.ifColumns = [
          {text: 'rowID', dataField: '_id', hidden: true},
          {text: 'Engine', dataField: 'name', sort:true, editable: false,               
              headerStyle: (colum, colIndex) => {
                  return { width: '11%', textAlign: 'left' };
              },
              validator: engineValidator
      }
  }
  render()
  {
    return(         
      <div className="col-md-7">
        <Card id='updCarCard'>
          <CardBody>
            <Form model="updCar" onSubmit={(values) => 
              this.handleSubmit(values)}>
                <Row className="form-group">
                  <Label htmlFor="name" md={3}>Car Name</Label>
                  <Col md={9}>
                    <Control.text model=".name" id="name" name="name" placeholder="Car Name" 
                      component={TextInput} withFieldValue
                      validators={
                        {   
                          maxLength: maxLength(15)
                        }
                      }
                    />
                    <Errors className="text-danger" model=".name" show="touched" 
                      messages={
                        {   
                          maxLength: 'Length must be at most 15'
                        }
                      }
                    />
                  </Col>
                </Row>
                <Row className="form-group">
                  <Col md={{size:10, offset: 3}}>
                    <Control.button model="updDevice" disabled={{ valid: false }} className="btn">Update Car</Control.button>
                  </Col>
                <div className="col-md-12">
                  <BootstrapTable striped hover condensed
                    keyField='_id' 
                    data={...}  
                    columns={ this.ifColumns }
                    defaultSorted={[{dataField: 'name',order: 'asc'}] } 
                    cellEdit={ cellEditFactory({ mode: 'click', blurToSave: true }) }/>
                </div>

              </Form>
            </CardBody>
          </Card>
        </div>
  }
}

TextInput 的代码:

export const TextInput = ({withFieldValue, fieldValue, ...props}) => {
  const textClassesNames = ["form-control"];
  console.log("Inside: " + JSON.stringify(fieldValue));
  if (fieldValue.touched) {
    if (fieldValue.valid) textClassesNames.push("is-valid");
    else textClassesNames.push("is-invalid");
  }
  return(
    <input className={textClassesNames.join(" ")} {...props} />
  )
}

关于如何使用表格验证的结果来控制提交按钮的“禁用”属性的任何想法?

【问题讨论】:

    标签: reactjs react-redux-form react-bootstrap-table


    【解决方案1】:

    由于您没有提供任何代码,我想您需要为submit button 按钮添加一个验证状态

    // add disabled state which true on validation success.
    <Control.button model="updDevice" disabled={disabled} className="btn">Update Car</Control.button>
    

    例如:

    const VALIDATION = 'hello';
    
    function ValidateForm() {
      const [value, setValue] = useState('');
      const [disabled, setDisabled] = useState(true);
      return (
        <>
          <input
            value={value}
            onChange={e => {
              const currValue = e.target.value;
              setValue(currValue);
              if (currValue === VALIDATION) {
                setDisabled(prev => !prev);
              }
            }}
          />
          <button disabled={disabled}>submit</button>
        </>
      );
    }
    

    至于react-bootsrap同例:

    const VALIDATION = 'hello';
    
    function ValidateBootstrapForm() {
      const [value, setValue] = useState('');
      const [disabled, setDisabled] = useState(true);
      return (
        <div className="form">
          <Form>
            <Form.Group controlId="formBasicEmail">
              <Form.Control
                value={value}
                onChange={e => {
                  const currValue = e.target.value;
                  setValue(currValue);
                  if (currValue === VALIDATION) {
                    setDisabled(prev => !prev);
                  }
                }}
                placeholder="Enter Hello"
              />
            </Form.Group>
            <Button disabled={disabled} variant="primary" type="submit">
              Submit
            </Button>
          </Form>
        </div>
      );
    }
    

    演示:

    【讨论】:

    • 添加了一些代码,所以你可以看看,也许改变你的答案。
    • 答案是一样的,你错过了'this.handleSubmit'并在你的卡片组件中为验证按钮添加了一个状态。请参阅您应该弄清楚的示例。此外,您无法控制“TextInput”的状态
    • 我不想在点击提交按钮时运行表单验证。正如您在代码中看到的那样,为字段分配了在字段更改时运行的专用验证器。用户可以看到表单包含无效输入,我只想禁用按钮。
    • @omer 我用 bootstrap Form 更新了答案和演示。
    猜你喜欢
    • 2019-11-30
    • 2016-08-15
    • 2013-06-09
    • 2016-02-29
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-25
    相关资源
    最近更新 更多