-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.jsx
139 lines (123 loc) · 3.8 KB
/
app.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"use strict";
var React = require("react");
var ReactDOM = require("react-dom")
var Joi = require("joi");
var JoiValidationStrategy = require("joi-validation-strategy");
var ReactValidationMixin = require("react-validation-mixin");
var ValidatedInput = React.createClass({
renderHelpText: function(message) {
return (
<span className="help-block">
{message}
</span>
);
},
render: function() {
var error
= this.props.getValidationMessages(
this.props.name);
var formClass = "form-group";
if (error.length > 0) {
formClass = formClass + " has-error";
}
return (
<div className={formClass}>
<label className="control-label" htmlFor={this.props.name}>
{this.props.label}
</label>
<input className="form-control" {...this.props}/>
{this.renderHelpText(error)}
</div>
);
}
});
var Demo = React.createClass({
validatorTypes: {
userName: Joi.string().required()
.label("User Name"),
password: Joi.string().required()
.regex(/[a-zA-Z0-9]{3,30}/)
.label("Password")
},
getValidatorData: function() {
return this.state;
},
getInitialState: function() {
return {
userName: "",
password: ""
};
},
onSubmit(event) {
event.preventDefault();
// Handle field level validations
var onValidate = function(error) {
if (error) {
if (error.userName) {
alert(error.userName);
}
if (error.password) {
alert(error.password);
}
}
// Handle form level validations
var passwordContainsUserName
= this.state.password.indexOf(
this.state.userName) > -1;
if (this.state.userName
&& passwordContainsUserName) {
alert("Password cannot contain the user name.");
return;
}
if (!error) {
alert("Account created!");
}
};
this.props.validate(onValidate.bind(this));
},
onChange: function(event) {
var state = {};
state[event.target.name] = event.target.value;
this.setState(state);
},
render: function() {
return (
<div className="container">
<form onSubmit={this.onSubmit}>
<ValidatedInput
name="userName"
type="text"
ref="userName"
placeholder="Enter User Name"
label="User Name"
value={this.state.userName}
onChange={this.onChange}
onBlur={this.props.handleValidation("userName")}
getValidationMessages=
{this.props.getValidationMessages}/>
<ValidatedInput
name="password"
className="form-control"
type="text"
ref="password"
placeholder="Enter Password"
label="Password"
value={this.state.password}
onChange={this.onChange}
onBlur={this.props.handleValidation("password")}
getValidationMessages=
{this.props.getValidationMessages}/>
<button className="btn btn-success" type="submit">
Submit
</button>
</form>
</div>
);
}
});
// Decorate our component with validations support
var ValidationDemo
= ReactValidationMixin(JoiValidationStrategy)(Demo);
ReactDOM.render(
<ValidationDemo/>,
document.getElementById("view"));