材料UI中所需的属性UI Textfield不起作用



我正在使用React应用程序中的材料UI库的TextField作为输入框。我想在我的TextField中添加必需的属性,以便它在客户端验证,但我注意到所需属性在提交按钮上不工作,并且表单输入发送到单击函数,而无需验证所需的属性。

这是我在渲染中使用的TextField代码

 <form >
          <br></br>
          <br></br>
          <br></br>
          <br></br>
          <TextField
        required
        id="standard-required"
            name="username"
            label="Employee ID"
            value={this.state.username}
            onChange={this.handleChange}
            margin="normal"
            variant="outlined"
          />
          <br></br>
          <TextField style={{width:'14.5%'}}
            name="password"
            id="outlined-password-input"
            label="Password"
            // type="password"
            type={this.state.showPassword ? 'text' : 'password'}
            value={this.state.password}
            isRequired="true"
            onChange={this.handleChange}
            autoComplete="current-password"
            margin="normal"
            variant="outlined"
            InputProps={{
              endAdornment: (
                <InputAdornment position="end">
                  <IconButton
                    aria-label="Toggle password visibility"
                    onClick={this.handleClickShowPassword}
                  >
                    {this.state.showPassword ? <Visibility /> : <VisibilityOff />}
                  </IconButton>
                </InputAdornment>
              ),
            }}
          />
          <br></br>
          <br></br>

          <FormControl variant="outlined" className={styles.formControl} required>
            <InputLabel
              ref={ref => {
                this.InputLabelRef = ref;
              }}
              htmlFor="outlined-age-simple"
            >
              Role
          </InputLabel>
            <Select style={{ width: 220 }}
              name="role"
              value={this.state.role}
              onChange={this.handleChange}
              input={
                <OutlinedInput
                  labelWidth={this.state.labelWidth}
                  name="name"
                  id="outlined-age-simple"
                // value={this.state.role}
                />
              }
            >
              <MenuItem value={'Doctor'}>Doctor</MenuItem>
              <MenuItem value={'Nurse'}>Nurse</MenuItem>
              <MenuItem value={'Receptionist'}>Receptionist</MenuItem>
            </Select>

            <br></br>
            <br></br>
            <Button variant="contained" style={{ backgroundColor: '#2699FB', width: 220 }} onClick={this.handleSubmit}><b style={{ color: '#fff' }}>login</b></Button>
          </FormControl>
        </form>

在这里我更改您的代码 -

   <form onSubmit={this.handleSubmit}>
      <br></br>
      <br></br>
      <br></br>
      <br></br>
      <TextField
   required={true}
    id="standard-required"
        name="username"
        label="Employee ID"
        value={this.state.username}
        onChange={this.handleChange}
        margin="normal"
        variant="outlined"
      />
      <br></br>
      <TextField style={{width:'14.5%'}}
        required={true}
        name="password"
        id="outlined-password-input"
        label="Password"
        // type="password"
        type={this.state.showPassword ? 'text' : 'password'}
        value={this.state.password}
        isRequired="true"
        onChange={this.handleChange}
        autoComplete="current-password"
        margin="normal"
        variant="outlined"
        InputProps={{
          endAdornment: (
            <InputAdornment position="end">
              <IconButton
                aria-label="Toggle password visibility"
                onClick={this.handleClickShowPassword}
              >
                {this.state.showPassword ? <Visibility /> : <VisibilityOff />}
              </IconButton>
            </InputAdornment>
          ),
        }}
      />
      <br></br>
      <br></br>

      <FormControl variant="outlined" className={styles.formControl} required>
        <InputLabel
          ref={ref => {
            this.InputLabelRef = ref;
          }}
          htmlFor="outlined-age-simple"
        >
          Role
      </InputLabel>
        <Select style={{ width: 220 }}
          name="role"
          value={this.state.role}
          onChange={this.handleChange}
          input={
            <OutlinedInput
              labelWidth={this.state.labelWidth}
              name="name"
              id="outlined-age-simple"
            // value={this.state.role}
            />
          }
        >
          <MenuItem value={'Doctor'}>Doctor</MenuItem>
          <MenuItem value={'Nurse'}>Nurse</MenuItem>
          <MenuItem value={'Receptionist'}>Receptionist</MenuItem>
        </Select>

        <br></br>
        <br></br>
        <Button variant="contained" style={{ backgroundColor: '#2699FB', width: 220 }} type="submit"><b style={{ color: '#fff' }}>login</b></Button>
      </FormControl>
    </form>

最新更新