Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #861: HOWTO

File HOWTO, 2.5 kB (added by sebastian.kanthak@muehlheim.de, 3 years ago)

instructions on how to use javascript validation support for rails

Line 
1 HOWTO Use Javascript Validation
2
3 Note: This is just a prototype of Javascript Validation in Rails to get some early feedback on the design and to give people a feel of how it will look like.
4
5
6
7 To try the stuff out, do the following:
8
9 - Apply the patch to your bleeding edge rails from subversion and start a new project that uses your patched version of rails.
10
11 - Add a file "validations.js" to your project's "public/javascripts" directory with the following content:
12
13 function validatePresence(field) {
14   if (field.value.length == 0) {
15     field.focus();
16     field.className = "fieldWithErrors";
17     alert("Field must be present");
18     return false;
19   }
20   return true;
21 }
22
23 function validateLength(field,max_length) {
24   if (field.value.length > max_length) {
25     field.focus();
26     field.className = "fieldWithErrors";
27     alert("Field is too long (maximum "+max_length+")");
28     return false;
29   }
30   return true;
31 }
32
33 - Create some simple model in your db and let rails generate scaffolding code for it.
34
35 - Declare some validations (currently supported: validates_presence_of, validates_length_of with :maximum) in the model
36
37 - Change the layout for your model to include your validations.js, e.g. add
38   <%= javascript_include_tag "validation" %>
39   somewhere in the head tag
40  
41 - Change the new.rhtml view generated by scaffolding like this:
42   - Make the start_form_tag include a "onsubmit='return validateForm(this)'" property, e.g. change it to
43   <%= start_form_tag({:action => 'create'}, { "onsubmit" => "return validateForm(this);" }) %>
44
45   - After the end_form_tag, add another line:
46   <%= javascript_validation_for_fields %>
47  
48 - Start up your application and go to the "new" page for your model. Look at the source. Play with the validation.
49
50 - Send feedbback to sebastian.kanthak@muehlheim.de (skanthak on IRC) or the mailing list
51
52
53
54 Some explanations about the design:
55
56 - We introduce reflection for validations so that we can examine what validations have been defined
57 - We store InstanceTag objects for all fields generated via FormHelper's methods in a @rails_instance_tag attribute
58 - We then generate appropriate JS calls for all fields in our new @rails_instance_tag attribute.
59
60
61 TODO and Issues:
62 - make JS use the error messages used for the validation macros. We need a way to apply the prepend_text and append_text messages of the ActiveRecordHelper's error_message_on method
63 - obviously support more validation macros
64 - add support for users to generate JS code for their own validations ??
65 - consider ":on => create" and ":on => update" options when generting JS code
66 - improve JS library
67