wForms - Server-side handling of the Repeat Behavior (part 2)

This post is part of the wForms Documentation. wForms is an open-source javascript library that adds commonly needed behaviors to traditional web forms without the need for any programming skill.For additional help, visit the wForms forum.

The Repeat behavior is certainly the most powerful functionality of the wForms extension and enabling it in a web form is dead simple (class=’repeat’ anyone?). Processing the form submission is trickier though, but the real difficulty with repeated groups arises when you want to populate a form with previously submitted data. You basically need to re-engineer server-side what wForms does client-side.

The Form

Let’s start with a simple form and let’s make the fieldset repeatable by using wForms’ ‘repeat’ class.

<form action="">
<fieldset id="myfieldset" class="repeat“>
<label for=”field1″>Label 1: </label>
<input type=”text” id=”field1″ name=”field1″ />
<label for=”field2″>Label 2: </label>
<input type=”text” id=”field2″ name=”field2″ />
</fieldset>
</form>




Submitted Data

Now, assuming that this form was previously submitted with 3 rows, the data collected on the server contains the following fields:

Field Name Field Value Comment
field1 value1… original row
field2 value2…  
field1-2 value3… second row
field2-2 value4…  
field1-3 value5… third row
field2-3 value6…  
myfieldset-rc 3 row counter

Populating back the form

In order to show the form back to the user, you need to :

  • Loop 3 times over the <fieldset> element.
  • Set the value of each field.
  • Appends the row index suffix to the id, name and for attributes for every tag in the second and third rows (the first row is always left unchanged by wForms).
  • Change the class from ‘repeat’ to ‘removeable’ (sic) for the second and third rows.
  • Create the hidden counter field. The name of this field is constructed by adding ‘-RC’ to the id of the tag with the ‘repeat’ class (here ‘myfieldset’).

If the form is correctly set up, wForms will be able to carry on from there, allowing adding more rows or removing the existing ones.

Sequence gaps

Now, as I mentioned previously, the -RC field in wForms does not exactly behave like a row counter. It indicates the highest row index reached. If the user deleted a row before submitting the form, you’ll end up with a gap in the sequence that you need to accommodate for. In the loop logic, you can tell if the row was deleted if you can’t find any name/value pair in your submitted dataset for the given row index.

Code Sample (PHP)

Note: Updated on June 15th 2007.

<?php
$max = (int) $_POST["myfieldset-RC"];
for($i=1;$i<=$max;$i++) {
if($i==1) {
$repeatclass = "repeat";
$rowIndex = "";
}
else {
$repeatclass = "removeable";
$rowIndex = "-".$i;
}
if(isset($_POST["field1".$rowIndex]) && isset($_POST["field2".$rowIndex])) { // account for sequence gap.
?>
<fieldset id="myfieldset<?php echo($rowIndex)?>" class="<?php echo($repeatclass)?>">
<label for="field1<?php echo($rowIndex)?>">Label 1: </label>
<input type="text" id="field1<?php echo($rowIndex)?>" name="field1<?php echo($rowIndex)?>" value="<?php echo($_POST["field1".$rowIndex])?>" />
<label for="field2<?php echo($rowIndex)?>">Label 2: </label>
<input type="text" id="field2<?php echo($rowIndex)?>" name="field2<?php echo($rowIndex)?>" value="<?php echo($_POST["field2".$rowIndex])?>" />
</fieldset>
<?php
}
}

echo "“;

?>

Result












Update: Comments are now closed for this post, but you can go to the wForms forum if you have any question or comment. Thanks !

5 month anniversary

The Form Assembly’s 5 month anniversary is closing in, and I must say, I’m quite impressed and happy with the success this site has encountered.

a redesign is coming…Form Assembly 2.0 Preview

Over 1200 users have registered, thousands more have been able to use the Form Builder anonymously and close to 6700 forms have been created so far.

wForms, the open-source javascript library behind some of the cool features offered by the Form Builder, has been translated in 10 languages and has been through 11 revisions, thanks to the help and feedback from many users. In all modesty, it probably has become the best and most robust javascript library for web forms.

The Form Garden has received less attention and I’ve neglected it a bit, but I’ll try to add more stylesheets to the 7 already there. If you’d like to contribute CSS stylesheets to the garden but don’t know how to get started, drop me an email, I’ll help you out.

So, thanks to you all, your comments and encouragements have all been really helpful.
Keep them coming. I’d love to hear what features you’d like to see added to the Form Assembly.

Technorati Tags: , ,

New CSS Stylesheets in the Form Garden

I added a couple more CSS stylesheets to the Form Garden.

  • timetrack is based on the look & feel of the time-tracker application.
  • black is, guess what, a black variation of the stylesheet above.

Usage Remarks

All stylesheets in the Form Garden are free and should work with any web form.

The stylesheets are designed for forms built with semantic HTML (using tags like <label>, <fieldset>, <legend>, etc…)

The CSS also styles wForms related elements :

  • .field-hint (instructions showing after a field)
  • .errFld (field with a validation error)
  • .errMsg (message reporting the validation error)
  • .duplicateLink, .removeLink (links generated by wForms’ Repeat behavior)
  • etc..

If you don’t use wForms, you can remove these definitions from the stylesheet.

CSS Conventions

Additionally, here are some of the conventions I adopted to allow for better control over the styling of a form.

  • The label position, relative to the field, is indicated by the classes .preField and .postField. For instance:

    <label class="preField" ... >Field Label</label> <input type='text' ... />


    <input type='checkbox' ... /> <label class="postField" ... >Field Label</label>
  • The primary action of a form, usually the submit button, is differentiated from less important actions (like cancel) by the use of the .primaryAction and .secondaryAction classes. The style for the .primaryAction class usually has a stronger visual weight (bolder or larger font for instance).

One last tip…

Vertical alignment of several fields can be achieved by using the width and inline-block CSS properties:
.preField {
display: -moz-inline-box; /* inline-block for firefox */
display: inline-block;
width: expression('7em'); /* Min-width for IE6 */
min-width: 7em;
}

Note: The CSS shown accounts for browser discrepancies

example


That’s it for today. If you’d like to contribute to the Form Garden, email me your stylesheet and any associated images. Thanks!

Technorati Tags: ,

You are currently browsing The Form Assembly weblog archives for August, 2005.

Search the Blog Archive

 

The Form Assembly blog is powered by WordPress ~ Entries (RSS) and Comments (RSS).