Form validation with Nooku Framework
Responsive validation feedback on forms is no longer a “nice to have” feature in modern web apps, it’s a standard that users have come to expect to be there.
In Nooku Framework Alpha 3, we added a new API that loads the forms validation library in MooTools More for you. And we implemented it in Nooku Server Alpha 3 to improve the usability of refactored components.
Today I’ll show you how easy it is to get started by simply adding CSS classes to your form inputs. Then I’ll show you how easy it is to create your own input validators for when you need to validate something custom.
Let’s get started
This tutorial will assume that you have a Nooku component already setup, with forms and everything else needed for the application to work. This tutorial aims to help you enhance your form, not build one from scratch.
The example component is just like a simplified com_weblinks, it renders a set of links.
So without further ado, this is the basic layout we’ll be enhancing:
<form action=”” class=”-koowa-form”> <input type=”text” name=”title” value=”<?= @escape($treasure->title) ?>” /> <input type=”text” name=”url” value=”<?= @escape($treasure->url) ?>” /> </form>
The first thing you need to do, is to load the validation behavior.
You do that by placing this call somewhere close to where you load koowa.js
<?= @helper(‘behavior.validator’) ?>
So in this form, there’s a couple of things needed to be improved. The title is a required field, the url field is required as well, but it also needs to be validated as a proper url.
The way to do that is by adding CSS classes on the form inputs and instructing the validator on how to validate the form.
<input type=”text” name=”title” value=”<?= @escape($treasure->title) ?>” class=”required” />
Valid
Invalid
<input type=”text” name=”url” value=”<?= @escape($treasure->url) ?>” class=”required validate-url” />
Valid
Invalid
The result:
<?= @helper(‘behavior.validator’) ?> <form action=”” class=”-koowa-form”> <input type=”text” name=”title” value=”<?= @escape($treasure->title) ?>” class=”required” /> <input type=”text” name=”url” value=”<?= @escape($treasure->url) ?>” class=”required validate-url” /> </form>
Keep your cool
Now with these added, we already have very basic validation up and running. When you’re having fun, it’s easy to run a bit crazy and add anything and every possible kind of validation you can to make the most out of what you can do. But you need to stay cool and always have the purpose of what you’re doing crystal clear in your mind. The form validation is meant to help the user workflow be as smooth and painless as it can be.
For example, we don’t add a validator on slug/alias inputs that bugs the user if he forgot to lowercase it properly simply because slug fields can be transformed to lowercase and transliterated automatically in PHP.
Rule of thumb
Rule of thumb : Our first goal should always be to make the application do as much as it can to aid the user. If it can’t, then we can help the user by validating his input.
So the forms validation that calls for user action should only be done on things that our application can’t figure out by itself .
Next time we’ll take a look at how we can automate things a bit more.
-
http://blog.hersoncduz.com Herson Cruz
-
http://pulse.yahoo.com/_ERSDRML2R7NQGXUCO7LIGXRVSU Alexander
-
http://www.facebook.com/stipsan Stian Emil Didriksen
-
http://pulse.yahoo.com/_ERSDRML2R7NQGXUCO7LIGXRVSU Alexander


