Skip to content

Commit a1fa9c3

Browse files
cfjedimasterphanan
andauthored
Update form-validation.md (#2410)
* Update form-validation.md Updates the server side code. Fixes #2391 * Update form-validation.md I removed the server side code and updated the form code to use the right URL. * Update form-validation.md Update related to comments. * Update src/v2/cookbook/form-validation.md * Update src/v2/cookbook/form-validation.md * Update src/v2/cookbook/form-validation.md * Update src/v2/cookbook/form-validation.md Co-authored-by: Phan An <me@phanan.net>
1 parent a22da0e commit a1fa9c3

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/v2/cookbook/form-validation.md

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -333,21 +333,27 @@ We set up the total value as a computed value, and outside of that bug I ran int
333333

334334
## Server-side Validation
335335

336-
In my final example, we built something that makes use of Ajax to validate at the server. The form will ask you to name a new product and will then check to ensure that the name is unique. We wrote a quick [OpenWhisk](http://openwhisk.apache.org/) serverless action to do the validation. While it isn't terribly important, here is the logic:
336+
In my final example, we built something that makes use of Ajax to validate at the server. The form will ask you to name a new product and will then check to ensure that the name is unique. We wrote a quick [Netlify](https://netlify.com/) serverless action to do the validation. While it isn't terribly important, here is the logic:
337337

338338
``` js
339-
function main(args) {
340-
return new Promise((resolve, reject) => {
341-
// bad product names: vista, empire, mbp
342-
const badNames = ['vista', 'empire', 'mbp'];
339+
exports.handler = async (event, context) => {
340+
341+
const badNames = ['vista', 'empire', 'mbp'];
342+
const name = event.queryStringParameters.name;
343+
344+
if (badNames.includes(name)) {
345+
return {
346+
statusCode: 400,
347+
body: JSON.stringify({error: 'Invalid name passed.'})
348+
}
349+
}
343350

344-
if (badNames.includes(args.name)) {
345-
reject({error: 'Existing product'});
346-
}
351+
return {
352+
statusCode: 204
353+
}
347354

348-
resolve({status: 'ok'});
349-
});
350355
}
356+
351357
```
352358

353359
Basically any name but "vista", "empire", and "mbp" are acceptable. Ok, so let's look at the form.
@@ -389,7 +395,7 @@ Basically any name but "vista", "empire", and "mbp" are acceptable. Ok, so let's
389395
There isn't anything special here. So let's go on to the JavaScript.
390396

391397
``` js
392-
const apiUrl = 'https://openwhisk.ng.bluemix.net/api/v1/web/rcamden%40us.ibm.com_My%20Space/safeToDelete/productName.json?name=';
398+
const apiUrl = 'https://vuecookbook.netlify.com/.netlify/functions/product-name?name=';
393399

394400
const app = new Vue({
395401
el: '#app',
@@ -407,13 +413,12 @@ const app = new Vue({
407413
this.errors.push('Product name is required.');
408414
} else {
409415
fetch(apiUrl + encodeURIComponent(this.name))
410-
.then(res => res.json())
411-
.then(res => {
412-
if (res.error) {
413-
this.errors.push(res.error);
414-
} else {
415-
           // redirect to a new URL, or do something on success
416-
alert('ok!');
416+
.then(async res => {
417+
if (res.status === 204) {
418+
alert('OK');
419+
} else if (res.status === 400) {
420+
let errorResponse = await res.json();
421+
this.errors.push(errorResponse.error);
417422
}
418423
});
419424
}

0 commit comments

Comments
 (0)