Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material UI DatePicker set to Required

Tags:

reactjs

i am using the Material UI DatePicker in my react app and need to set the date to be required?

Do I need to create a custom validator checking value on submit or is there a prop I can use to set field to required?

<DatePicker
            spacing={2}
            PopoverProps={{ container: this.props.container }}
            onChange={this.updateEndDate}
            value={this.state.endDate}
            initialFocusedDate={this.state.startDate}
            minDate={this.state.startDate}
            disablePast="true"
            disableToolbar="true"
            variant="inline"
            label="End Date"

           // I have tried adding required, required="true", required={true}


          />

1 Answers

You can set required in a DatePicker by adding the slotProps prop for textfield (you can read more about it in the mui component API section for datepicker):

<DatePicker
        spacing={2}
        PopoverProps={{ container: this.props.container }}
        onChange={this.updateEndDate}
        value={this.state.endDate}
        initialFocusedDate={this.state.startDate}
        minDate={this.state.startDate}
        disablePast="true"
        disableToolbar="true"
        variant="inline"
        label="End Date"

       // I have tried adding required, required="true", required={true}

        slotProps={{
              textField: {
                required: true,
              },
            }}
      />

though it won't show any errors, it sticks with the required property. I'll update the answer if I find a way to add the inherited required dialogue box to it

like image 190
HarshS11 Avatar answered Feb 06 '26 12:02

HarshS11



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!