在我的React应用程序的一个表单中,如果类别设置为滚动,我想输入一组输入(长度和仪表)必填。我该怎么做?
<Form.Group as={Col}>
<label>Category</label>
<Form.Control
as="select"
name="category"
defaultValue={this.state.category}
onChange={this.catControl}
>
<option>printed</option>
<option>roll</option>
</Form.Control>
</Form.Group>
<Form.Row>
<Form.Group as={Col}>
<label>Length(cm)</label>
//required if category is set to "roll". How can I do that?
<Form.Control name="length" defaultValue={this.state.length} />
</Form.Group>
<Form.Group as={Col}>
<label>Gauge(mm)</label>
<Form.Control name="gauge" defaultValue={this.state.gauge} />
</Form.Group>
</Form.Row>
答案 0 :(得分:2)
在html部分中,您可以执行以下操作:
<Form.Control name="length" defaultValue={this.state.length} required={ this.state.category==='roll'}/>