【发布时间】: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