Skip to content

Commit c9d4421

Browse files
author
Dustyn Blackmore
committed
Fix Validation Bug
Found I had handled the switch (type) of input in the same way for each type. I have left the switch, as I know it will be needed on checkbox types (and others), but removed duplicate code. Additionally; reset state of the base validationErrorMessage to ''. This requires a minor fix as part of another issue to provide default validation failing onComponentDidMount.
1 parent 868397a commit c9d4421

1 file changed

Lines changed: 7 additions & 29 deletions

File tree

src/components/Input.js

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,8 @@ class Input extends Component {
227227
let thisValue = null;
228228

229229
switch (this.props.type) {
230-
case "button":
231-
thisValue = clonedEvent.target.value;
232-
break;
233-
case "radio":
234-
thisValue = clonedEvent.target.value;
235-
break;
236-
case "text":
237-
thisValue = clonedEvent.target.value;
238-
break;
239-
case "textarea":
240-
thisValue = clonedEvent.target.value;
241-
break;
242230
default:
243-
thisValue = clonedEvent.target.value || clonedEvent.value || false;
231+
thisValue = clonedEvent.target.value;
244232
break;
245233
}
246234

@@ -406,30 +394,18 @@ class Input extends Component {
406394
handleHasValidation(event) {
407395
let value = undefined;
408396
switch (this.props.type) {
409-
case "radio":
410-
value = event.target.on;
411-
break;
412-
413-
case "text":
414-
value = event.target.value;
415-
break;
416-
417-
case "textarea":
418-
value = event.target.value;
419-
break;
420-
421397
default:
422-
value = true;
398+
value = event.target.value;
423399
break;
424400
}
425401
let validationResult = undefined;
426-
let validationFailureIndex = -1;
402+
427403
if (this.props.validation instanceof Array) {
428404
validationResult = this.props.validation.map(aFunction => {
429405
return aFunction.callback(value);
430406
});
431407

432-
validationFailureIndex = validationResult.indexOf(false);
408+
let validationFailureIndex = validationResult.indexOf(false);
433409
if (validationFailureIndex === -1) {
434410
validationResult = true;
435411
} else {
@@ -448,6 +424,8 @@ class Input extends Component {
448424
}
449425
}
450426

427+
if (validationResult) { this.setState({ validationErrorMessage: '' }) };
428+
451429
this.setState({
452430
isValid: validationResult
453431
});
@@ -561,7 +539,7 @@ class Input extends Component {
561539
if (this.props.validation) {
562540
thisValidation = (
563541
<p className={validationClassNames}>
564-
{this.state.validationErrorMessage || "invalid"}
542+
{this.state.validationErrorMessage}
565543
</p>
566544
);
567545
}

0 commit comments

Comments
 (0)