vValidate allows you to validate inputs by declaring rules on each field. It supports Laravel's validation rules, JavaScript functions, and third-party libraries like validator.js. vValidate also supports validation schemas, allowing you to declare a schema for the entire form. With vValidate, you can enhance forms by adding client-side validation without much JavaScript. The diff tools plugin in vValidate provides an inspector that shows the form's validity, current values, and errors. It allows you to perform actions like clearing the form and forcing validation.
So Form Validation. This is the main reason you are properly using vValidate. vValidate allows you to validate your inputs by declaring rules on each of these fields or inputs. You can use Laravel's style validation rules like this one which is now in its own companion package. But you have other ways to do validation. You can use normal JavaScript functions or you can use any third-party validation library like validator.js.
In this example we are using Yelp which is a very popular front-end validation tool. But what's really strong about it is its way to express form schemas. So vValidate also now supports validation schemas. Validation schemas means you can declare a schema for the entire form and vValidate will be validating the fields correctly. And it will assign the error messages accordingly. Yelp is really good here and it allows you to declare your schemas in a very declarative way in your JavaScript.
Let's talk about the progressive enhancement part. Here we have an example of a very basic form. It's a native form that submits the register action so it will do a full page reload and it will submit these fields to the slash register endpoint. Let's say you have this against some kind of a backend framework like Laravel or Rails and most of the cases you have similar setup to this, but let's say you just want to add some sprinkle of JavaScript, you only want to add some client-side validation and you want to prevent submission for that field without doing too much JavaScript. So you can do that with vValidate by swapping out the form element with the form component and swapping out the input elements with the field component. You can also add error message component for error display and just by assigning the validation schema to the form, you are pretty much done. vValidate will figure out that you are not listening for the submit event so you are not trying to hijack the submission. So that means it will just obey the default behavior only if the form is valid. So if the user tries to submit and everything is valid it will act as if there isn't any kind of Javascript going on here. But if there is some errors it will prevent the submission, giving you exactly what you need.
We also have the diff tools plugin that we implemented recently in 4.5. So right here we have the view diff tools and in the view diff tools you will notice a new inspector called vValidate. If you click on that inspector you will notice vValidate shows you the form as it sees it. At a glance vValidate shows you the validity of the fields, so if i start filling out some fields right here they will start lighting up green. And that at a glance gives you a really good idea of what is valid and what is not. There is also here on the right you can see a snapshot of the form, you can see its current values, any errors if it has any, so if i remove this one you will see the error right here, and all kinds of information that you want to. There is, if you click on the fields you will see a similar summary of state, and you can do some actions here, like if you click on the form you can clear the entire thing, and you can force validate it as well. You can clear a single field and you can force validate it as well.
That's it for this talk, there are a lot more you can do with Visual.JS, which includes array fields, filled meta, localization, handling submissions, and UI library integrations.
Comments