Skip to content

Commit 62d0c63

Browse files
committed
Fixed example.
1 parent e2a50b7 commit 62d0c63

File tree

6 files changed

+45
-48
lines changed

6 files changed

+45
-48
lines changed

validation/controllers/default.js

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,9 @@
11
exports.install = function() {
2-
F.route('/', view_homepage);
3-
F.route('/', view_homepage, ['xhr', 'post']);
2+
F.route('/', 'index');
3+
F.route('/', json_contactform, ['post', '*Contact']);
44
};
55

6-
function view_homepage() {
6+
function json_contactform() {
77
var self = this;
8-
9-
if (!self.xhr) {
10-
self.meta('Validation example');
11-
self.view('index', { LoginName: '@' });
12-
return;
13-
}
14-
15-
var result = self.validate(self.post, ['FirstName', 'LastName', 'Age', 'Email', 'Terms'], 'Form');
16-
17-
// Documentation: ErrorBuilder
18-
if (result.hasError()) {
19-
result.replace('@Email', self.post.Email);
20-
self.json(result);
21-
return;
22-
}
23-
24-
self.json({ r: true });
8+
self.$save(self.callback());
259
}

validation/definitions/validation.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

validation/models/contact.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
NEWSCHEMA('Contact').make(function(schema) {
2+
3+
schema.define('firstname', 'Capitalize(30)', true);
4+
schema.define('lastname', 'Capitalize(30)', true);
5+
schema.define('email', 'Email', true);
6+
schema.define('age', 'Number', true);
7+
schema.define('terms', 'Boolean', true);
8+
9+
schema.setPrefix('error-');
10+
11+
schema.setValidate(function(name, value) {
12+
switch (name) {
13+
case 'age':
14+
return value < 17 ? 'You are too young.' : value > 40 ? 'You are too old' : true;
15+
}
16+
});
17+
18+
schema.setSave(function(error, model, options, callback) {
19+
callback(SUCCESS(true));
20+
});
21+
22+
});

validation/resources/default.resource

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FormFirstName : Please enter your first name
2-
FormLastName : Please enter your last name
3-
FormEmail : Email (@Email) is not valid
4-
FormTerms : You must agree
1+
error-firstname : Please enter your first name
2+
error-lastname : Please enter your last name
3+
error-email : Email is not valid
4+
error-terms : You must agree

validation/views/index.html

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,49 @@
22
<form action="#" method="POST" id="f">
33
<div>
44
<div>First name:</div>
5-
<div><input type="text" name="FirstName" maxlength="200" /></div>
5+
<div><input type="text" name="firstname" maxlength="200" /></div>
66
</div>
77
<br />
88
<div>
99
<div>Last name:</div>
10-
<div><input type="text" name="LastName" maxlength="200" /></div>
10+
<div><input type="text" name="lastname" maxlength="200" /></div>
1111
</div>
1212
<br />
1313
<div>
1414
<div>E-mail:</div>
15-
<div><input type="text" name="Email" value="@" maxlength="200" /></div>
15+
<div><input type="text" name="email" value="@" maxlength="200" /></div>
1616
</div>
1717
<br />
1818
<div>
1919
<div>Age:</div>
20-
<div><input type="text" name="Age" maxlength="2" /></div>
20+
<div><input type="text" name="age" maxlength="2" /></div>
2121
</div>
2222
<br />
2323
<div>
24-
<div><label><input type="checkbox" name="Terms" value="1" /> I agree</label></div>
24+
<div><label><input type="checkbox" name="terms" value="1" /> I agree</label></div>
2525
</div>
2626
<div id="error"></div>
2727
</form>
2828
<br />
2929
<button>Submit</button>
3030

31-
<script type="text/javascript">
31+
<script>
3232
$(document).ready(function() {
33-
$('button').bind('click', function() {
34-
$.post('/?ts=' + new Date().getTime(), $('#f').serialize(), function(d) {
33+
$('button').on('click', function() {
34+
$.post('/?ts=' + Date.now(), $('#f').serialize(), function(d) {
35+
3536
var err = $('#error');
36-
d = JSON.parse(d);
37+
$('input').removeClass('error');
38+
3739
if (d instanceof Array) {
3840

3941
err.empty();
42+
4043
d.forEach(function(o) {
44+
$('input[name="' + o.name + '"]').addClass('error');
4145
err.append('<div>' + o.error + '</div>');
4246
});
47+
4348
err.show();
4449
return;
4550
};

validation/views/layout.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
body { padding: 50px; margin: 0; font:normal 12px Arial; color: gray }
1313
#error { display: none; background-color: black; color: white; margin: 20px 0 0 0; }
1414
#error > div { border-bottom: 1px solid gray; padding: 3px 5px; }
15+
.error { background-color: red; }
1516
</style>
1617
</head>
1718
<body>

0 commit comments

Comments
 (0)