[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] repeating form fields


> > If that's the case, I handle it by adding a unique prefix or suffix to
> > the names of the elements in each "sub-form".
> >
> > Data::FormValidator already handles this well in that you define
> > required fields, optional fields and constraints with a regular
> > expressions, so it's easy to apply logic for "all fields whose name
> > begins with... ".
>
> Except that my set of N widgets may include some number of "empty"
> subforms, which are accepted.  Since D::FV doesn't have a
> dependency_group_regexp (and indeed, I'm not really faulting it, that's
> getting a bit extreme..), that is no longer easy to manage.

Attached is a snippet of validation code I used for a case like this,
where an event could have N seesions, I had to have a variable number of
form rows to handle this. Each session had a start date, a start time,
and a location, all of which depended on each other. I always needed at
least one session to be filled in, and I wanted to mark the first form
as required if nothing was filled in.

I named the form fields in each dependency group with the same numerical
prefix. I used positive numbers for fields that had been filled in, and
negative numbers for the ones that started out blank, so I could tell
them apart. This also helped me identify which field to mark as required
if needed.

I even coded a little JavaScript so you could reload the form to add two
more rows if needed. It would refresh the screen with all the current
data plus two empty rows. (Granted, there was some Perl involved too).

This worked for me, but it would be nice if there was a simpler, more
elegant solution out there for cases like this.

> > The elegant long-term solution to this might be the "XForms" standard
> > proposed by the W3C:
>
> I haven't gone through that yet, but I have reached the conclusion that
> I'm just running into the limits of CGI.

I should have mentioend I think this depends on browser support, which
isn't there yet, so it's not a current viable option, AFAIK.

  Mark

 --
 . . . . . . . . . . . . . . . . . . . . . . . . . . .
   Mark Stosberg            Principal Developer
   suppressed     Summersault, LLC
   765-939-9301 ext 202     database driven websites
 . . . . . http://www.summersault.com/ . . . . . . . .
	my %dependency_groups;
	# I need a way to say here "all session attributes are in a dependency group"
	# an example is:  -4_start_date, -4_end_date
	# Find all the fields that match "session_id"_start_date
	# We keep track of the highest numbered one, because we might need to
	# make it required if nothing else if field in
    my $highest_id = -999;	
	for my $key (keys %$form) {
		if ($key =~ m/^(-?\d+)_start_time$/) {
			my $id = $1;
			$highest_id = $id if ($id >= $highest_id);
			# We only add a dependency group if the start_date contains something
			# to make our Location default work. -mls 
			if ($form->{$key} =~ /\S/) {
				# For each of these session_ids, add a dependency group
				$dependency_groups{$id} = ["${id}_start_time","${id}_end_time","${id}_location"];			
			}
		}
	}

	# There will always be some session rows submitted. 
	# There must be at least one session row with data in it. 
	# If no rows contain a start time, we'll mark the higest one as required. 
	my $something_filled_in = 0;
	for (keys %dependency_groups) {
		$something_filled_in = 1 if ($FORM{$dependency_groups{$_}->[0]}  =~ /\S/);
	}

	my @required = (qw/
			event_name
			presented_by
			description
			credit_offered
			credit_type_id
			skill_level
			external_cost
			category_id/);
	unless ($something_filled_in) {
		#use Data::Dumper;
		#carp "li: $lowest_id : dg $dependency_groups{$lowest_id}";
		#push @required, @{ $dependency_groups{$lowest_id} };
	    push @required,  ("${highest_id}_start_time","${highest_id}_end_time","${highest_id}_location");	
	}
---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed

Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.