Skip to content

Commit f999612

Browse files
committed
edits
1 parent 4a14286 commit f999612

File tree

1 file changed

+34
-34
lines changed

1 file changed

+34
-34
lines changed

src/v2/cookbook/form-validation.md

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ order: 1.2
88

99
Form validation is natively supported by the browser, but sometimes different browsers will handle things in a manner which makes relying on it a bit tricky. Even when validation is supported perfectly, there may be times when custom validations are needed and a more manual, Vue-based solution may be more appropriate. Let's begin with a simple example.
1010

11-
For my first example, let's create something as simple as possible. Given a form of three fields, make two required. Let's look at the HTML first:
11+
Given a form of three fields, make two required. Let's look at the HTML first:
1212

1313
``` html
1414
<form id="app" @submit="checkForm" action="/something" method="post">
@@ -21,34 +21,34 @@ For my first example, let's create something as simple as possible. Given a form
2121
</p>
2222

2323
<p>
24-
<label for="name">Name<label>
25-
<input type="text" name="name" id="name" v-model="name">
24+
<label for="name">Name<label>
25+
<input type="text" name="name" id="name" v-model="name">
2626
</p>
2727

2828
<p>
29-
<label for="age">Age<label>
30-
<input type="number" name="age" id="age" v-model="age">
29+
<label for="age">Age<label>
30+
<input type="number" name="age" id="age" v-model="age">
3131
</p>
3232

3333
<p>
34-
<label for="movie">Favorite Movie<label>
35-
<select name="movie" id="movie" v-model="movie">
36-
<option>Star Wars</option>
37-
<option>Vanilla Sky</option>
38-
<option>Atomic Blonde</option>
39-
</select>
34+
<label for="movie">Favorite Movie<label>
35+
<select name="movie" id="movie" v-model="movie">
36+
<option>Star Wars</option>
37+
<option>Vanilla Sky</option>
38+
<option>Atomic Blonde</option>
39+
</select>
4040
</p>
4141

4242
<p>
43-
<input type="submit" value="Submit">
43+
<input type="submit" value="Submit">
4444
</p>
4545

4646
</form>
4747
```
4848

4949
Let's cover it from the top. The form tag has an ID that we'll be using for the Vue component. There's a submit handler that you'll see in a bit, and the action is a fake URL that would point to something real on a server someplace (where you have backup server-side validation of course).
5050

51-
Beneath that there isa paragraph that shows or hides itself based on an error state. This is a personal preference of mine when building forms. I like a nice simple list of errors on top of the form. You may like error handling by the fields themselves. Use what works. Also note I'm going to fire my validation on submit rather than as every field is modified. Again, this is a personal preference.
51+
Beneath that there is a paragraph that shows or hides itself based on an error state. This will render a simple list of errors on top of the form. Also note we fire the validation on submit rather than as every field is modified.
5252

5353
The final thing to note is that each of the three fields has a corresponding v-model to connect them to values we will work with in the JavaScript. Now let's look at that.
5454

@@ -93,26 +93,26 @@ For the second example, the second text field (age) was switched to email which
9393
</p>
9494

9595
<p>
96-
<label for="name">Name<label>
97-
<input type="text" name="name" id="name" v-model="name">
96+
<label for="name">Name<label>
97+
<input type="text" name="name" id="name" v-model="name">
9898
</p>
9999

100100
<p>
101-
<label for="email">Email<label>
102-
<input type="email" name="email" id="email" v-model="email">
101+
<label for="email">Email<label>
102+
<input type="email" name="email" id="email" v-model="email">
103103
</p>
104104

105105
<p>
106-
<label for="movie">Favorite Movie<label>
107-
<select name="movie" id="movie" v-model="movie">
108-
<option>Star Wars</option>
109-
<option>Vanilla Sky</option>
110-
<option>Atomic Blonde</option>
111-
</select>
106+
<label for="movie">Favorite Movie<label>
107+
<select name="movie" id="movie" v-model="movie">
108+
<option>Star Wars</option>
109+
<option>Vanilla Sky</option>
110+
<option>Atomic Blonde</option>
111+
</select>
112112
</p>
113113

114114
<p>
115-
<input type="submit" value="Submit">
115+
<input type="submit" value="Submit">
116116
</p>
117117

118118
</form>
@@ -149,7 +149,7 @@ const app = new Vue({
149149
})
150150
```
151151

152-
As you can see, we've added `validEmail` as a new method and it is simply called from `checkForm`. Nothing too crazy, but a good example I think. You can play with this example here:
152+
As you can see, we've added `validEmail` as a new method and it is simply called from `checkForm`. You can play with this example here:
153153

154154
<p data-height="265" data-theme-id="0" data-slug-hash="vWqNXZ" data-default-tab="html,result" data-user="cfjedimaster" data-embed-version="2" data-pen-title="form validation 2" class="codepen">See the Pen <a href="https://codepen.io/cfjedimaster/pen/vWqNXZ/">form validation 2</a> by Raymond Camden (<a href="https://codepen.io/cfjedimaster">@cfjedimaster</a>) on <a href="https://codepen.io">CodePen</a>.</p>
155155
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
@@ -187,15 +187,15 @@ For the third example, we've built something you've probably seen in survey apps
187187
</p>
188188

189189
<p>
190-
<input type="submit" value="Submit">
190+
<input type="submit" value="Submit">
191191
</p>
192192

193193
</form>
194194
```
195195

196-
Note the set of inputs covering the five different features. Note the addition of .number to the v-model attibute. This tells Vue to cast the value to a number when you use it. However, there is a bug with this feature such that when the value is blank, it turns back into a string. You'll see the workaround below. To make it a bit easier for the user, we also added a current total right below so they can see, in real time, what their total is. Now let's look at the JavaScript.
196+
Note the set of inputs covering the five different features. Note the addition of .number to the v-model attribute. This tells Vue to cast the value to a number when you use it. However, there is a bug with this feature such that when the value is blank, it turns back into a string. You'll see the workaround below. To make it a bit easier for the user, we also added a current total right below so they can see, in real time, what their total is. Now let's look at the JavaScript.
197197

198-
```
198+
``` js
199199
const app = new Vue({
200200
el:'#app',
201201
data:{
@@ -233,7 +233,7 @@ We set up the total value as a computed value, and outside of that bug I ran int
233233

234234
## Server-side Validation
235235

236-
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 serverless action to do the validation. While it isn't terribly important, here is the logic:
236+
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:
237237

238238
``` js
239239
function main(args) {
@@ -263,12 +263,12 @@ Basically any name but "vista", "empire", and "mbp" are acceptable. Ok, so let's
263263
</p>
264264

265265
<p>
266-
<label for="name">New Product Name: </label>
267-
<input type="text" name="name" id="name" v-model="name">
266+
<label for="name">New Product Name: </label>
267+
<input type="text" name="name" id="name" v-model="name">
268268
</p>
269269

270270
<p>
271-
<input type="submit" value="Submit">
271+
<input type="submit" value="Submit">
272272
</p>
273273

274274
</form>
@@ -313,9 +313,9 @@ We start off with a variable representing the URL of the API that is running on
313313
<p data-height="265" data-theme-id="0" data-slug-hash="BmgzeM" data-default-tab="js,result" data-user="cfjedimaster" data-embed-version="2" data-pen-title="form validation 4" class="codepen">See the Pen <a href="https://codepen.io/cfjedimaster/pen/BmgzeM/">form validation 4</a> by Raymond Camden (<a href="https://codepen.io/cfjedimaster">@cfjedimaster</a>) on <a href="https://codepen.io">CodePen</a>.</p>
314314
<script async src="https://production-assets.codepen.io/assets/embed/ei.js"></script>
315315

316-
## Vue Form Validation Libraries
316+
## Alternative Patterns
317317

318-
While this cookbook entry focused on doing form validation "by hand", there are, of course, some great Vue libraries that will handle a lot of this for you. They include:
318+
While this cookbook entry focused on doing form validation "by hand", there are, of course, some great Vue libraries that will handle a lot of this for you. Switching to a prepackage library may impact the final size of your application, but the benefits could be tremendous. You have code that is (most likely) heavily tested and also updated on a regular basis. Some examples of form validation libraries for Vue include:
319319

320320
* [vuelidate](https://github.com/monterail/vuelidate)
321321
* [VeeValidate](http://vee-validate.logaretm.com/)

0 commit comments

Comments
 (0)