{"id":2745,"date":"2009-12-14T22:22:12","date_gmt":"2009-12-14T21:22:12","guid":{"rendered":"http:\/\/www.chipwreck.de\/blog\/?p=2745"},"modified":"2016-12-31T02:09:16","modified_gmt":"2016-12-31T01:09:16","slug":"mootools-formvalidator-how-to-use-part-1","status":"publish","type":"post","link":"https:\/\/www.chipwreck.de\/blog\/2009\/12\/14\/mootools-formvalidator-how-to-use-part-1\/","title":{"rendered":"Mootools FormValidator &#8211; How to use (Part 1)"},"content":{"rendered":"<div class=\"contentnavi\">\n<p class=\"contentnav1 center\">\n<span class=\"pink\">Tutorial Part 1<\/span> &bull;<br \/>\n<a href=\"\/blog\/2009\/12\/28\/mootools-formvalidator-how-to-use-part-2\/\">Part 2<\/a> &bull;<br \/>\n<a href=\"\/blog\/2010\/01\/12\/mootools-formvalidator-how-to-use-part-3\/\">Part 3<\/a> &bull;<br \/>\n<a href=\"\/blog\/2010\/01\/19\/mootools-formvalidator-how-to-use-part-4\/\">Part 4<\/a> &bull;<br \/>\n<a href=\"\/blog\/2009\/12\/23\/mootools-formvalidator-demo\/\">Live demo<\/a>\n<\/p>\n<\/div>\n<p>One of the nicest Mootools.more components is the <a href=\"http:\/\/mootools.net\/docs\/more\/Forms\/Form.Validator\" class=\"external\">FormValidator<\/a> &#8211; but the documentation is incomplete and IMHO not very easy to comprehend.<\/p>\n<p>After using the FormValidator a lot, I decided to begin writing a tutorial in several parts. <!--more--><\/p>\n<h2>Overview<\/h2>\n<p><em>Note: MooTools more version 1.2.4+ is being used.<\/em><\/p>\n<p>First have a look at the example: <a href=\"\/blog\/2009\/12\/23\/mootools-formvalidator-demo\/\">Form Validation Demo &raquo;<\/a><\/p>\n<p>Each input-field in the form can have a list of validators. A validator checks one specific aspect, for example minimum length. There are a lot of pre-defined validators (see mootols\/docs).<\/p>\n<p>To make validation nearly automatic, we initialize an instance of FormValidator.Inline for the form.<\/p>\n<h2>FormValidator initialization<\/h2>\n<p>First we initialize the FormValidator.Inline-object. The first parameter is the CSS-id of the form, the second parameter are options:<\/p>\n<p><strong>stopOnFailure<\/strong> means this: If one validation fails, all other fields are temporarily\tignored. This is more user-friendly &#8211; otherwise clicking through the fields would show one error message after another..<\/p>\n<p><strong>useTitles<\/strong> allows us to overwrite the error messages with the title-attribute, you can see this in the last field (&#8220;terms of service&#8221;) &#8211; where we show a custom message.<\/p>\n<p><strong>errorPrefix<\/strong> is a text that is shown before all error messages, defaults to &#8220;Error: &#8220;. But an error message is enough, no need to repeat that it&#8217;s an error, so I set to an empty string.<\/p>\n<p>The <strong>onFormValidate<\/strong> method is called if everything is all right &#8211; in this case, we submit the form.<\/p>\n<p><strong>onElementValidate<\/strong> is called after each Validation. This is useful to handle some fields specifically &#8211; here for demonstration purposes: An annoying alert.<\/p>\n<pre escaped=\"true\" lang=\"javascript\">\r\nwindow.addEvent('domready', function() {\r\n\tmyFormValidator = new Form.Validator.Inline($('register'), {\r\n\t\tstopOnFailure: true,\r\n\t\tuseTitles: true,\r\n\t\terrorPrefix: \"\",\r\n\t\t\t\t\r\n\t\tonFormValidate: function(passed, form, event) {\r\n\t\t\tif (passed) {\r\n\t\t\t\tform.submit();\r\n\t\t\t}\r\n\t\t},\r\n\r\n\t\tonElementValidate: function(passed, element, validator, is_warn) {\r\n\t\t\tif (element.get('name') == 'agb' && !passed) {\r\n\t\t\t\talert('Hey, the terms are really important!');\r\n\t\t\t}\r\n\t\t}\r\n\t});\r\n});\r\n<\/pre>\n<h2>Field validation<\/h2>\n<p>All validators for a field are simply listed as classes, here an example:<\/p>\n<p><code escaped=\"true\" lang=\"html\" nowrap=\"false\">&lt;input class=\"required validate-email emailUnique\" type=\"text\" name=\"profile[email]\" \/&gt;<br \/>\n<\/code><\/p>\n<p>Here three rules are applied:<\/p>\n<ul>\n<li><strong>required<\/strong> &#8211; The field can&#8217;t be empty<\/li>\n<li><strong>validate-email<\/strong> &#8211; The field has to contain a valid email<\/li>\n<li>..and a custom validator &#8211; explained in Part 2<\/li>\n<\/ul>\n<h4>Length<\/h4>\n<p><code escaped=\"true\" lang=\"html\" nowrap=\"false\">&lt;input type=\"text\" class=\"minLength:2\" name=\"profile[firstname]\" \/&gt;<br \/>\n<\/code><\/p>\n<p>The firstname should be at least 2 characters long. Note that &#8220;<strong>required<\/strong>&#8221; isn&#8217;t necessary anymore, since 2 characters already imply that something is filled in.<\/p>\n<h4>More length<\/h4>\n<p><code escaped=\"true\" lang=\"html\" nowrap=\"false\">&lt;input type=\"text\" class=\"validate-alphanum minLength:3 maxLength:30\"  name=\"profile[username]\" maxlength=\"30\" \/&gt;<br \/>\n<\/code><\/p>\n<p>A custom validator (explained in Part 2) called &#8220;alphaNumFix&#8221; and more restrictions: minimum 3, maximum 30 chars.<\/p>\n<h4>Checkboxes, custom error message<\/h4>\n<p><code escaped=\"true\" lang=\"html\" nowrap=\"false\">&lt;input type=\"checkbox\" class=\"validate-required-check\" title=\"Please agree to the terms of service.\" name=\"terms\"  value=\"1\" \/&gt;<br \/>\n<\/code><\/p>\n<p>The title is used as custom error message, validate-required-check does exactly what it sounds like&#8230;<\/p>\n<h4>Password repeat<\/h4>\n<p><code escaped=\"true\" lang=\"html\" nowrap=\"false\">&lt;input type=\"password\" class=\"required validate-match matchInput:'password' matchName:'Password'\" name=\"profile[password2]\" \/&gt;<br \/>\n<\/code><\/p>\n<p>This is the &#8220;repeat your password&#8221;-field. <strong>validate-match<\/strong> is exactly for such cases.<\/p>\n<p>Note the properties:<\/p>\n<ul>\n<li><strong>matchInput<\/strong> &#8211; CSS-id of the other field, which has to match<\/li>\n<li><strong>matchName<\/strong> &#8211; How the other field is called in the error message.<\/li>\n<\/ul>\n<p>If we do not specify the matchName, this would be the error message:<br \/>\n<em>&#8220;This field needs to match the profile[password] field&#8221;.<\/em><br \/>\nNow it&#8217;s clearer:<br \/>\n<em>&#8220;This field needs to match the Password field&#8221;<\/em><\/p>\n<h2>Styling the output<\/h2>\n<p>These are the CSS classes used by the FormValidator:<\/p>\n<pre lang=\"css\">.validation-passed { background-color: #99cc99 !important; }\r\n  \/* This style is applied to input fields after successful validation *\/\r\n\r\n.validation-advice { background-color: #cc9999; color: white; margin: 3px; padding: 3px; } \r\n  \/* This style is applied to the error messages *\/\r\n\r\n.validation-failed { background-color: #cc9999; }\r\n  \/* This style is applied to input fields after validation failed *\/<\/pre>\n<p>&nbsp;<\/p>\n<p><em>Part 2 can be found here: <a href=\"\/blog\/2009\/12\/28\/mootools-formvalidator-how-to-use-part-2\/\">How to use (Part 2) &raquo;<\/a><\/em><\/p>\n<h4><strong>Comments\/Feedback? Now collected in <a href=\"\/blog\/2010\/01\/19\/mootools-formvalidator-how-to-use-part-4\/\">part 4 &raquo;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>Tutorial Part 1 &bull; Part 2 &bull; Part 3 &bull; Part 4 &bull; Live demo One of the nicest Mootools.more components is the FormValidator &#8211; but the documentation is incomplete and IMHO not very easy to comprehend. After using the FormValidator a lot, I decided to begin writing a tutorial in several parts.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[14],"tags":[10,54],"class_list":["post-2745","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-mootools","tag-track"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paPEN-Ih","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/posts\/2745","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/comments?post=2745"}],"version-history":[{"count":0,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/posts\/2745\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/media?parent=2745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/categories?post=2745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chipwreck.de\/blog\/wp-json\/wp\/v2\/tags?post=2745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}