Skip to content

Commit 562d237

Browse files
committed
Every validatoin condition can pass their own error message.
1 parent c0d8da7 commit 562d237

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/App.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react';
22
import './App.css';
33
import { TextInput, Validate } from './utils/form_helpers';
4+
import { inputType } from './utils/Constants';
45

56
class App extends Component {
67
constructor() {
@@ -13,9 +14,16 @@ class App extends Component {
1314
placeholder: 'What is your name',
1415
valid: false,
1516
touched: false,
17+
errorMessage: "",
1618
validationRules: {
17-
minLength: 3,
18-
isRequired: true
19+
20+
isRequired: {
21+
message: "Name is required",
22+
},
23+
minLength: {
24+
value: 2,
25+
message: "Min length is required",
26+
}
1927
}
2028
}
2129
}
@@ -34,7 +42,10 @@ class App extends Component {
3442
};
3543
updatedFormElement.value = value;
3644
updatedFormElement.touched = true;
37-
updatedFormElement.valid = Validate(value, updatedFormElement.validationRules);
45+
const { isValid, message } = Validate(value, updatedFormElement.validationRules);
46+
47+
updatedFormElement.valid = isValid;
48+
updatedFormElement.errorMessage = message;
3849

3950
updatedControls[name] = updatedFormElement;
4051

@@ -63,6 +74,8 @@ class App extends Component {
6374
label="Name"
6475
name="name"
6576
placeholder={this.state.formControls.name.placeholder}
77+
// helpText="Name must be greater than 6 character"
78+
errorMessage={this.state.formControls.name.errorMessage}
6679
value={this.state.formControls.name.value}
6780
onChange={this.changeHandler}
6881
touched={this.state.formControls.name.touched}

0 commit comments

Comments
 (0)