简体   繁体   English

Material Ui - 在“选择”模式下向 TextField 添加必需属性

[英]Material Ui - Add required attribute to a TextField in "select" mode

I am trying to make "required" a TextField in select mode.我试图在选择模式下使“必需”成为一个 TextField。 I tried to add required prop like in this snippet, but this does not block the submit event if I haven't select anything.我试图在这个片段中添加required道具,但是如果我没有选择任何东西,这不会阻止提交事件。 Although it adds the '*' to the label.尽管它在标签上添加了“*”。

Please check this sandbox请检查此沙箱

 <TextField id="select-currency" select label="Select" value={this.state.currency} onChange={this.handleChange("currency")} required > {currencies.map(option => ( <MenuItem key={option.value} value={option.value}> {option.label} </MenuItem> ))} </TextField>

UPDATE: (Clarification really) I am talking about html5 validation.更新:(真正澄清)我在谈论 html5 验证。 In the sandbox example there are Select and Text fields, setting the text field as required will block the submit event and displays a native html5 error saying "this field is required", this is not the case if the field is "select".在沙箱示例中,有 Select 和 Text 字段,将文本字段设置为 required 将阻止提交事件并显示本机 html5 错误,提示“此字段是必需的”,如果该字段为“select”,则情况并非如此。

Material Ui provides another component Native Select to handle this kind of native validation. Material Ui 提供了另一个组件Native Select来处理这种原生验证。

Please check this example请检查这个例子

编辑材料演示

It was recently implemented in material ui.它最近在材料 ui 中实现。 If you upgrade @material-ui/core to version 4.11.0 it will work https://github.com/mui-org/material-ui/issues/20402如果您将@material-ui/core升级到 4.11.0 版,它将起作用https://github.com/mui-org/material-ui/issues/20402

It's not field responsibility to control form behavoiur.控制形式行为不是现场责任。 It's parent-child relation and top-down data passing.它是父子关系和自上而下的数据传递。

Form (component) provides current value to component (validity information, change handler) to field component.表单(组件)向字段组件提供组件的当前值(有效性信息、更改处理程序)。 Field displays content using styles and elements depending on props ( * when required, error border etc) but it's form' role to decide about value/validity/submiting.字段使用样式和元素显示内容,具体取决于道具( *需要时,错误边框等),但表单的作用是决定值/有效性/提交。

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

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