react-select icon indicating copy to clipboard operation
react-select copied to clipboard

Add validation support

Open jsingh27 opened this issue 9 years ago • 60 comments

Hi,

Please add validation support as it is a very important part.

jsingh27 avatar Dec 28 '16 08:12 jsingh27

workaround:

export const renderField = ({ input, placeholder, simpleValue, meta: { touched, error }, ...field }) => (
  <div>
    <Select 
      simpleValue={simpleValue}
      errorText={touched && error}
      value={input.value}
      className="defautSelectParent"
      placeholder={placeholder}
      options={field.loadOptions}
      onBlur={() => input.onBlur(input.value)}
      onChange={(value) => { input.onChange(value)} }
      {...field} />
    <div>{(touched && error) && 
      <div style={{'fontSize':'12px','color':'rgb(244, 67, 54)'}}>Required</div>}</div>
  </div>
);

jsingh27 avatar Jan 23 '17 15:01 jsingh27

Basically you are adding a label right? But what about coloring the border in red? Any news on this feature? Can't see it in the demos, neither in docs.

libasoles avatar Sep 04 '17 20:09 libasoles

I made in one text input component custom validation with these codes in it

onChange = (e) => {
    if (this.props.validate) {
      e.target.setCustomValidity(this.props.validate(value));
    }
}

Can you add these lines then We can set some string message to validate property and it will show similar like basic required message.

I check and the correct place is here. https://github.com/JedWatson/react-select/blob/master/src/Select.js#L347 So all is done can you make it?

Other option is this:

addValidationMessage(message) {
    this.input.setCustomValidity(message);
  }

JanneHarju avatar Oct 05 '17 15:10 JanneHarju

+1, also need this here.

Using jquery-validation for form validation.

leite08 avatar Nov 14 '17 20:11 leite08

I'm not sure if this is really the place to put this, but maybe it'll help someone else!

I'm using Bootstrap 4, and I wanted the styling of react-select to be passably similar with regards to focus, and validation colours.

The following isn't perfect, but it was passable for what I needed. (Which is just single selection, no tags, pretty much just the "basic" single select.)

First I added a couple of classes to the Select control which indicate validation status.

if (isValid) {
    combinedClassName += " reactselect-valid";
} else if (isInvalid) {
    combinedClassName += " reactselect-invalid";
}

Then I modified the styles to work with these classes.

Expand to see SASS

@import "~bootstrap/scss/_variables.scss";
@import "~react-select/scss/select.scss";

.Select {
    &.is-open > .Select-control {
        border-color: $component-active-bg;
    }

    &.is-focused > .Select-control {
        border-color: $component-active-bg;
        box-shadow: 0 0 0 $input-focus-width rgba($component-active-bg, .25);
    }

    &.is-focused:not(.is-open) > .Select-control {
        border-color: $component-active-bg;
        box-shadow: 0 0 0 $input-focus-width rgba($component-active-bg, .25);
    }

    &.is-open > .Select-menu-outer {
        border-color: $component-active-bg;
    }

    &.reactselect-valid {
        .Select-control {
            border-color: $form-feedback-valid-color;
        }

        &.is-open > .Select-control {
            border-color: $form-feedback-valid-color;
        }

        &.is-focused > .Select-control {
            border-color: $form-feedback-valid-color;
            box-shadow: 0 0 0 $input-focus-width rgba($form-feedback-valid-color, .25);
        }

        &.is-focused:not(.is-open) > .Select-control {
            border-color: $form-feedback-valid-color;
            box-shadow: 0 0 0 $input-focus-width rgba($form-feedback-valid-color, .25);
        }

        &.is-open > .Select-menu-outer {
            border-color: $form-feedback-valid-color;
        }
    }

    &.reactselect-invalid {
        .Select-control {
            border-color: $form-feedback-invalid-color;
        }

        &.is-open > .Select-control {
            border-color: $form-feedback-invalid-color;
        }

        &.is-focused > .Select-control {
            border-color: $form-feedback-invalid-color;
            box-shadow: 0 0 0 $input-focus-width rgba($form-feedback-invalid-color, .25);
        }

        &.is-focused:not(.is-open) > .Select-control {
            border-color: $form-feedback-invalid-color;
            box-shadow: 0 0 0 $input-focus-width rgba($form-feedback-invalid-color, .25);
        }

        &.is-open > .Select-menu-outer {
            border-color: $form-feedback-invalid-color;
        }
    }
}

ghost avatar Apr 27 '18 12:04 ghost

I found a work around

Setp -1 : Add a variable in the Component Class hiddenInput = null;

Step-2 Add a method

HighlightIfInError = () => { if (this.hiddenInput != undefined) { let el = this.hiddenInput; while ((el = el.parentElement) && !el.classList.contains("Select-control")); if (this.hiddenInput.hasAttribute("required")) { el.style.border = "1px solid red"; } else { el.style.border = ""; } } };

This method will be called from the component where validation is happening this.ComponentRefToComponentHavingReactSelet.current.HighlightIfInError();

Step -3 : Change the componentDidUpdate method

**componentDidUpdate = () => {
    this.hiddenInput = document.getElementById(`chaperone-search-${this.props.row_key}`);
};**

Step 4 : Update the Select component that is a child component

<Select.Async ... required={true} inputProps={{ id: chaperone-search-${this.props.row_key}, name: chaperone_search_${this.props.row_key} }} />

Note : this.props.row_key should be a unique value

Let me know if it helped. For me it worked

p4pramod avatar May 18 '18 08:05 p4pramod

Here's a simple workaround that works well with React Bootstrap plugin

<Select style={{ borderColor: this.state.someValidationError ? "#b94a48" : "#aaa"}} .../>

gtaylor44 avatar Jun 30 '18 13:06 gtaylor44

Any news on this?

andorep avatar Aug 29 '18 18:08 andorep

I needed to mark required fields as such so I ended up passing a custom placeholder when I need to show "Required" validation labels:

placeholder={isValid ? "Normal text placeholder" : <span className="text-danger">Required</span>}

prymitive avatar Sep 05 '18 13:09 prymitive

Yeah.. big one for me. If the user can input invalid data they can break the data model.

InputNeuron avatar Sep 18 '18 13:09 InputNeuron

Is anything planned about this ? I need it too !

parouuu avatar Oct 22 '18 14:10 parouuu

That's a pretty important feature... not everybody wants a controlled component and will just use standard HTML validation for forms. And even if you are fine with a controlled components, it's some validation you don't have to do when the form is submitted. It's a very basic feature to have: required, pattern, ... I'm fine trying to make a PR for it if it'll get accepted and merged

fbarbare avatar Nov 15 '18 10:11 fbarbare

@dcousens @JedWatson

fbarbare avatar Nov 15 '18 10:11 fbarbare

@fbarbare I'm not a maintainer for this project actually! Sorry, can't help on this one :+1:

dcousens avatar Nov 15 '18 23:11 dcousens

@JedWatson is on holiday.

gtaylor44 avatar Nov 16 '18 04:11 gtaylor44

Any updates here? I would also love to see this.

patotoma avatar Nov 27 '18 15:11 patotoma

Looking forward for this feature!

Aym3nTN avatar Nov 30 '18 08:11 Aym3nTN

bump!! looking forward too

mohsinriaz17 avatar Dec 12 '18 07:12 mohsinriaz17

Any updates? Looking forward to this feature.

BalasubramaniM avatar Dec 22 '18 09:12 BalasubramaniM

Has anyone tried making a PR to add it? It doesn't seem like it would be too hard. I suggest doing that instead of asking other people to do it.

IanVS avatar Dec 22 '18 14:12 IanVS