Skip to content

Commit 171e198

Browse files
lex111sdras
authored andcommitted
Fix code style in Form Validation recipe (#1682)
* Fix code style in Form Validation recipe * More style code slight improvement in Form Validation recipe
1 parent 4fe97c4 commit 171e198

File tree

1 file changed

+82
-57
lines changed

1 file changed

+82
-57
lines changed

src/v2/cookbook/form-validation.md

Lines changed: 82 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,28 @@ The final thing to note is that each of the three fields has a corresponding `v-
5454

5555
``` js
5656
const app = new Vue({
57-
el:'#app',
58-
data:{
59-
errors:[],
60-
name:null,
61-
age:null,
62-
movie:null
57+
el: '#app',
58+
data: {
59+
errors: [],
60+
name: null,
61+
age: null,
62+
movie: null
6363
},
6464
methods:{
65-
checkForm:function(e) {
66-
if(this.name && this.age) return true;
65+
checkForm: function (e) {
66+
if (this.name && this.age) {
67+
return true;
68+
}
69+
6770
this.errors = [];
68-
if(!this.name) this.errors.push("Name required.");
69-
if(!this.age) this.errors.push("Age required.");
71+
72+
if (!this.name) {
73+
this.errors.push('Name required.');
74+
}
75+
if (!this.age) {
76+
this.errors.push('Age required.');
77+
}
78+
7079
e.preventDefault();
7180
}
7281
}
@@ -122,26 +131,33 @@ While the change here is small, note the `novalidate="true"` on top. This is imp
122131

123132
``` js
124133
const app = new Vue({
125-
el:'#app',
126-
data:{
127-
errors:[],
128-
name:null,
129-
email:null,
130-
movie:null
134+
el: '#app',
135+
data: {
136+
errors: [],
137+
name: null,
138+
email: null,
139+
movie: null
131140
},
132-
methods:{
133-
checkForm:function(e) {
141+
methods: {
142+
checkForm: function (e) {
134143
this.errors = [];
135-
if(!this.name) this.errors.push("Name required.");
136-
if(!this.email) {
137-
this.errors.push("Email required.");
138-
} else if(!this.validEmail(this.email)) {
139-
this.errors.push("Valid email required.");
144+
145+
if (!this.name) {
146+
this.errors.push("Name required.");
147+
}
148+
if (!this.email) {
149+
this.errors.push('Email required.');
150+
} else if (!this.validEmail(this.email)) {
151+
this.errors.push('Valid email required.');
152+
}
153+
154+
if (!this.errors.length) {
155+
return true;
140156
}
141-
if(!this.errors.length) return true;
157+
142158
e.preventDefault();
143159
},
144-
validEmail:function(email) {
160+
validEmail: function (email) {
145161
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
146162
return re.test(email);
147163
}
@@ -197,29 +213,36 @@ Note the set of inputs covering the five different features. Note the addition o
197213

198214
``` js
199215
const app = new Vue({
200-
el:'#app',
216+
el: '#app',
201217
data:{
202-
errors:[],
203-
weapons:0,
204-
shields:0,
205-
coffee:0,
206-
ac:0,
207-
mousedroids:0
218+
errors: [],
219+
weapons: 0,
220+
shields: 0,
221+
coffee: 0,
222+
ac: 0,
223+
mousedroids: 0
208224
},
209-
computed:{
210-
total:function() {
211-
// must parse cuz Vue turns empty value to string
212-
return Number(this.weapons)+
213-
Number(this.shields)+
214-
Number(this.coffee)+
225+
computed: {
226+
total: function () {
227+
// must parse because Vue turns empty value to string
228+
return Number(this.weapons) +
229+
Number(this.shields) +
230+
Number(this.coffee) +
215231
Number(this.ac+this.mousedroids);
216232
}
217233
},
218234
methods:{
219-
checkForm:function(e) {
235+
checkForm: function (e) {
220236
this.errors = [];
221-
if(this.total != 100) this.errors.push("Total must be 100!");
222-
if(!this.errors.length) return true;
237+
238+
if (this.total != 100) {
239+
this.errors.push('Total must be 100!');
240+
}
241+
242+
if (!this.errors.length) {
243+
return true;
244+
}
245+
223246
e.preventDefault();
224247
}
225248
}
@@ -237,16 +260,16 @@ In my final example, we built something that makes use of Ajax to validate at th
237260

238261
``` js
239262
function main(args) {
240-
241263
return new Promise((resolve, reject) => {
242-
243264
// bad product names: vista, empire, mbp
244-
let badNames = ['vista','empire','mbp'];
245-
if(badNames.includes(args.name)) reject({error:'Existing product'});
246-
resolve({status:'ok'});
247-
265+
const badNames = ['vista', 'empire', 'mbp'];
266+
267+
if (badNames.includes(args.name)) {
268+
reject({error: 'Existing product'});
269+
}
270+
271+
resolve({status: 'ok'});
248272
});
249-
250273
}
251274
```
252275

@@ -280,22 +303,24 @@ There isn't anything special here. So let's go on to the JavaScript.
280303
const apiUrl = 'https://openwhisk.ng.bluemix.net/api/v1/web/rcamden%40us.ibm.com_My%20Space/safeToDelete/productName.json?name=';
281304

282305
const app = new Vue({
283-
el:'#app',
284-
data:{
285-
errors:[],
286-
name:''
306+
el: '#app',
307+
data: {
308+
errors: [],
309+
name: ''
287310
},
288311
methods:{
289-
checkForm:function(e) {
312+
checkForm: function (e) {
290313
e.preventDefault();
314+
291315
this.errors = [];
292-
if(this.name === '') {
293-
this.errors.push("Product name is required.");
316+
317+
if (this.name === '') {
318+
this.errors.push('Product name is required.');
294319
} else {
295-
fetch(apiUrl+encodeURIComponent(this.name))
320+
fetch(apiUrl + encodeURIComponent(this.name))
296321
.then(res => res.json())
297322
.then(res => {
298-
if(res.error) {
323+
if (res.error) {
299324
this.errors.push(res.error);
300325
} else {
301326
           // redirect to a new URL, or do something on success

0 commit comments

Comments
 (0)