Add validation support
Hi,
Please add validation support as it is a very important part.
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>
);
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.
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);
}
+1, also need this here.
Using jquery-validation for form validation.
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;
}
}
}
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
Here's a simple workaround that works well with React Bootstrap plugin
<Select
style={{ borderColor: this.state.someValidationError ? "#b94a48" : "#aaa"}} .../>
Any news on this?
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>}
Yeah.. big one for me. If the user can input invalid data they can break the data model.
Is anything planned about this ? I need it too !
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
@dcousens @JedWatson
@fbarbare I'm not a maintainer for this project actually! Sorry, can't help on this one :+1:
@JedWatson is on holiday.
Any updates here? I would also love to see this.
Looking forward for this feature!
bump!! looking forward too
Any updates? Looking forward to this feature.
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.