最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to implement validationrestriction in react-datepicker - Stack Overflow

programmeradmin3浏览0评论

I am trying to implement restriction or validation in a react-datepicker ponent. I am using redux-form for validation and normalization(to implement restriction)

.0.0-rc.1/examples/normalizing/

Question : I have observed that neither normalizing function nor validation functions of redux-form is called when we try to enter something in the field

although this value is not submitted when we submit the form but i need to show some validation error or restrict user from typing invalid characters.

I made a wrapper for the date picker ponent and used it in my form through redux field

my date picker ponent :-

return (
      <div className={"render-date-picker "}>
        <div className="input-error-wrapper">
          {(input.value) ? <label> {placeholder} </label> : ''}
          <DatePicker className="input form-flow" {...input}
            placeholderText={placeholder}
            selected={input.value ? moment(input.value) : null}
            maxDate={maxDate || null}
            minDate={minDate || null}
            dateFormat={isTimePicker ? "LLL" : "DD/MM/YYYY"}
            showYearDropdown
            showMonthDropdown
            disabledKeyboardNavigation
          />

          {touched && error && <span className="error-msg">{t(error)}</span>}
          <span className="bar" style={{ 'display': this.state.is_focus ? 'block' : 'none' }} ></span>
        </div>
      </div>
    );

redux form field :-

<Field
 name="date_of_birth"
 type="text"
 className="input form-flow extra-padding-datepicker"
 ponent={RenderDatePicker}
 maxDate={moment().subtract(18, "years")}
 validate={[required, dateOfBirth]}
 normalize={isValidDateFormat}
 placeholder={t("DOB (DD/MM/YYYY)")}
/>

my normalizing function:-

export const isValidDateFormat = (value, previousValue) => {
    if (value == null || !value.isValid()) {
        return previousValue;
    }
    return value;
}

I am trying to implement restriction or validation in a react-datepicker ponent. I am using redux-form for validation and normalization(to implement restriction)

https://redux-form./6.0.0-rc.1/examples/normalizing/

Question : I have observed that neither normalizing function nor validation functions of redux-form is called when we try to enter something in the field

although this value is not submitted when we submit the form but i need to show some validation error or restrict user from typing invalid characters.

I made a wrapper for the date picker ponent and used it in my form through redux field

my date picker ponent :-

return (
      <div className={"render-date-picker "}>
        <div className="input-error-wrapper">
          {(input.value) ? <label> {placeholder} </label> : ''}
          <DatePicker className="input form-flow" {...input}
            placeholderText={placeholder}
            selected={input.value ? moment(input.value) : null}
            maxDate={maxDate || null}
            minDate={minDate || null}
            dateFormat={isTimePicker ? "LLL" : "DD/MM/YYYY"}
            showYearDropdown
            showMonthDropdown
            disabledKeyboardNavigation
          />

          {touched && error && <span className="error-msg">{t(error)}</span>}
          <span className="bar" style={{ 'display': this.state.is_focus ? 'block' : 'none' }} ></span>
        </div>
      </div>
    );

redux form field :-

<Field
 name="date_of_birth"
 type="text"
 className="input form-flow extra-padding-datepicker"
 ponent={RenderDatePicker}
 maxDate={moment().subtract(18, "years")}
 validate={[required, dateOfBirth]}
 normalize={isValidDateFormat}
 placeholder={t("DOB (DD/MM/YYYY)")}
/>

my normalizing function:-

export const isValidDateFormat = (value, previousValue) => {
    if (value == null || !value.isValid()) {
        return previousValue;
    }
    return value;
}
Share Improve this question edited Dec 1, 2017 at 12:32 vijay 11k12 gold badges66 silver badges81 bronze badges asked Dec 1, 2017 at 12:23 Ayesha MunduAyesha Mundu 1,0952 gold badges11 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

react-datepicker provides onChangeRaw property to get raw (typed) value inside the datePicker. In order to restrict or validate the datepicker input field we need the raw value which is not available on onChange event of the datepicker ponent.

for instance if we observe the onChange event :-

It returns a moment object.

In order to get raw value we use onChangeRaw

The raw value is obtained simply from e.target.value. In my case I simply restrict the user from typing anything in the datepicker input field by a simple function:-

handleChangeRaw = (date) => {
    let s=document.getElementById(date_picker_id)
    s.value =moment(this.props.input.value).format("DD/MM/YYYY");
  }

My datepicker ponent :-

<DatePicker
 .....
 disabledKeyboardNavigation
 onChangeRaw={(e)=>this.handleChangeRaw(e)}
 id={date_picker_id}
 .....
/>

This solved my problem. Hope it is useful :)

发布评论

评论列表(0)

  1. 暂无评论