HTML5 form required attribute. Set custom validation message?

前端 未结 14 1651
你的背包
你的背包 2020-11-22 01:26

I\'ve got the following HTML5 form: http://jsfiddle.net/nfgfP/

相关标签:
14条回答
  • 2020-11-22 01:56

    I have a simpler vanilla js only solution:

    For checkboxes:

    document.getElementById("id").oninvalid = function () {
        this.setCustomValidity(this.checked ? '' : 'My message');
    };
    

    For inputs:

    document.getElementById("id").oninvalid = function () {
        this.setCustomValidity(this.value ? '' : 'My message');
    };
    
    0 讨论(0)
  • 2020-11-22 01:59

    Adapting Salar's answer to JSX and React, I noticed that React Select doesn't behave just like an <input/> field regarding validation. Apparently, several workarounds are needed to show only the custom message and to keep it from showing at inconvenient times.

    I've raised an issue here, if it helps anything. Here is a CodeSandbox with a working example, and the most important code there is reproduced here:

    Hello.js

    import React, { Component } from "react";
    import SelectValid from "./SelectValid";
    
    export default class Hello extends Component {
      render() {
        return (
          <form>
            <SelectValid placeholder="this one is optional" />
            <SelectValid placeholder="this one is required" required />
            <input
              required
              defaultValue="foo"
              onChange={e => e.target.setCustomValidity("")}
              onInvalid={e => e.target.setCustomValidity("foo")}
            />
            <button>button</button>
          </form>
        );
      }
    }
    

    SelectValid.js

    import React, { Component } from "react";
    import Select from "react-select";
    import "react-select/dist/react-select.css";
    
    export default class SelectValid extends Component {
      render() {
        this.required = !this.props.required
          ? false
          : this.state && this.state.value ? false : true;
        let inputProps = undefined;
        let onInputChange = undefined;
        if (this.props.required) {
          inputProps = {
            onInvalid: e => e.target.setCustomValidity(this.required ? "foo" : "")
          };
          onInputChange = value => {
            this.selectComponent.input.input.setCustomValidity(
              value
                ? ""
                : this.required
                  ? "foo"
                  : this.selectComponent.props.value ? "" : "foo"
            );
            return value;
          };
        }
        return (
          <Select
            onChange={value => {
              this.required = !this.props.required ? false : value ? false : true;
              let state = this && this.state ? this.state : { value: null };
              state.value = value;
              this.setState(state);
              if (this.props.onChange) {
                this.props.onChange();
              }
            }}
            value={this && this.state ? this.state.value : null}
            options={[{ label: "yes", value: 1 }, { label: "no", value: 0 }]}
            placeholder={this.props.placeholder}
            required={this.required}
            clearable
            searchable
            inputProps={inputProps}
            ref={input => (this.selectComponent = input)}
            onInputChange={onInputChange}
          />
        );
      }
    }
    
    0 讨论(0)
提交回复
热议问题