You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/v2/cookbook/form-validation.md
+34-34Lines changed: 34 additions & 34 deletions
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ order: 1.2
8
8
9
9
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.
10
10
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:
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).
50
50
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.
52
52
53
53
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.
54
54
@@ -93,26 +93,26 @@ For the second example, the second text field (age) was switched to email which
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:
153
153
154
154
<pdata-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 <ahref="https://codepen.io/cfjedimaster/pen/vWqNXZ/">form validation 2</a> by Raymond Camden (<ahref="https://codepen.io/cfjedimaster">@cfjedimaster</a>) on <ahref="https://codepen.io">CodePen</a>.</p>
@@ -187,15 +187,15 @@ For the third example, we've built something you've probably seen in survey apps
187
187
</p>
188
188
189
189
<p>
190
-
<inputtype="submit"value="Submit">
190
+
<inputtype="submit"value="Submit">
191
191
</p>
192
192
193
193
</form>
194
194
```
195
195
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.
197
197
198
-
```
198
+
```js
199
199
constapp=newVue({
200
200
el:'#app',
201
201
data:{
@@ -233,7 +233,7 @@ We set up the total value as a computed value, and outside of that bug I ran int
233
233
234
234
## Server-side Validation
235
235
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:
237
237
238
238
```js
239
239
functionmain(args) {
@@ -263,12 +263,12 @@ Basically any name but "vista", "empire", and "mbp" are acceptable. Ok, so let's
@@ -313,9 +313,9 @@ We start off with a variable representing the URL of the API that is running on
313
313
<pdata-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 <ahref="https://codepen.io/cfjedimaster/pen/BmgzeM/">form validation 4</a> by Raymond Camden (<ahref="https://codepen.io/cfjedimaster">@cfjedimaster</a>) on <ahref="https://codepen.io">CodePen</a>.</p>
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:
0 commit comments