Michael Schuerig
2006-Aug-14 21:23 UTC
[Rails] [ANN] Plugins: Validation Reflection and Client-Side Validation
I''ve just put two plugins on RubyForge. Included below are the READMEs. You can get the plugins at svn://rubyforge.org//var/svn/valirefl/validation_reflection/trunk svn://rubyforge.org//var/svn/clientsidevali/client_side_validation/trunk Michael Validation Reflection ==================== Version 0.2, 2006-08-06 This plugin adds reflective access to validations - ModelClass.reflect_on_all_validations - ModelClass.reflect_on_validations_for(:property) = Deprecation Notice Version 0.1 had supplied three methods - validates_presence_of_mandatory_content_columns - validates_lengths_of_string_attributes - validates_all_associated These have been removed. Please use the Enforce Schema Rules plugin instead http://enforce-schema-rules.googlecode.com/svn/trunk/enforce_schema_rules/ Client Side Validation ===================== Version 0.2, 2006-08-06 Requires Validation Reflection plugin. This plugin is only a tool, it leaves some work for you to do. The plugin happily handles the validation part, but it is your job to handle the notification. Let''s look at the simplest case first. In public/javascripts/application.js put: Form.Validator.installForAllValidatedForms(); This installs a validator for each form on the page which has the class "validated". If any of the form''s input elements are or become invalid, they are marked with the class "invalid". If you have something like this .invalid { border: 1px solid #f00; } somewhere in your stylesheet(s), invalid fields will be marked with a read border. This works for * :validates_presence_of * :validates_length_of * :validates_numericality_of * :validates_inclusion_of * :validates_format_of - as far as regular expressions are matched the same in Ruby and JavaScript == Localization Without further doing, the validator is not set up to handle any specific locale. For instance, dates assumed to be in ISO 8601 format, say 2006-08-13. To make the validator handle a specific locale, you need to load a JavaScript file with validators for that locale. Put <%= javascript_include_tag ''validators-en'' %> as the last javascript_include_tag in your layout. Currently, there are validators-en and validators-de. And all they do is handle the different date formats. == Custom Checking If you need client-side validations that can''t be expressed with these means, you''ll have to write a validation function yourself. Again in application.js Object.extend(Form.Validator.Validators, { very_special: function(value) { // Leave the check if there actually is a value to the // validation function set up for :validates_presence_of. if (!this.value) { return true; } // Do the ckecking... return true; } }); Then, in a view where a text field (or whatever it is) needs to be subject to this very special test <%= text_field :object, :attribute, :class => ''very_special'' %> That''s it. === Complicated Checks The declarative wiring of validation functions can be used for functions that need to take into account the values of multiple input elements. Writing these functions is a bit tricky. If you really need such a thing, look at the code for <tt>Form.Validator.Validators#exactly</tt> and <tt>Form.Validator.Validators#different</tt>. == Notifying Users of Validation Results Form.Validator.install(''person_form'', { onElementStatusChange: { person_last_name: function(isValid) { if (!isValid) alert(this.value + " is not a valid last name."); }, }, onFormStatusChange: function(isValid) { if (!isValid) alert("Your form is not valid!"); } }); If you provide your own +onElementStatusChange+ function for reporting a status change, this overrides the default behavior of adding/removing the "invalid" class to the input element. You can explicitly invoke that behavior like this person_last_name: function(isValid) { Form.Validator.switchClassNameInvalid(this, isValid); if (!isValid) alert(this.value + " is not a valid last name."); }, Obviously, just popping up an alert box or putting a border around an element isn''t very user friendly. Especially so for a validator like this that checks continuously. Therefore you need to come up with a way to notify your users of errors in a way that meshes well with the rest of the application. Currently, there is no way to find out which specific validation failed for a given input element. As validations don''t have any identifiert I''m hard pressed for a good idea. -- Michael Schuerig mailto:michael@schuerig.de http://www.schuerig.de/michael/
Has anyone used this plugin? I have installed both validation_reflection and client_side_validation, added class="validated" to my form, Form.Validator.installForAllValidatedForms( ); in application.js, .invalid { ... } in stylesheet. In IE 6, when I load the page I get ''object doesn''t support this property ...''. In FF no error. But in both I can submit the form with erros, no javascript alert or smt... Do you know another plugin for client side validation? Michael Schuerig wrote:> I''ve just put two plugins on RubyForge. Included below are the READMEs. > You can get the plugins at > > svn://rubyforge.org//var/svn/valirefl/validation_reflection/trunk > svn://rubyforge.org//var/svn/clientsidevali/client_side_validation/trunk > > Michael > > > Validation Reflection > ====================> > Version 0.2, 2006-08-06 > > This plugin adds reflective access to validations > > - ModelClass.reflect_on_all_validations > - ModelClass.reflect_on_validations_for(:property) > > > = Deprecation Notice > > Version 0.1 had supplied three methods > > - validates_presence_of_mandatory_content_columns > - validates_lengths_of_string_attributes > - validates_all_associated > > These have been removed. Please use the Enforce Schema Rules plugin > instead > > > http://enforce-schema-rules.googlecode.com/svn/trunk/enforce_schema_rules/ >-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Schuerig
2006-Nov-06 10:35 UTC
Re: [ANN] Plugins: Validation Reflection and Client-Side Val
On Monday 06 November 2006 01:36, George wrote:> Has anyone used this plugin?Yes, me... but I''m the author. Are you using the latest version? There was a small error in a previous version that didn''t load the locale-specific validation functions.> I have installed both validation_reflection and > client_side_validation, added class="validated" to my form, > Form.Validator.installForAllValidatedForms( ); in application.js, > .invalid { ... } in stylesheet. > In IE 6, when I load the page I get ''object doesn''t support this > property ...''. In FF no error. But in both I can submit the form with > erros, no javascript alert or smt...As far as my own experience goes, client-side validation works in Firefox, IE6, and Konqueror. The fault I mentioned above may well be what''s causing the problems you''re seeing. Michael -- Michael Schuerig mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org http://www.schuerig.de/michael/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---