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

Re: [cgiapp] RFC: Data::FormValidator 4.0


On Sat, 02 Jul 2005 00:41:33 -0400, Jason Purdy wrote:

Hi Jason

> # now here's my problem ... now I have to create a db handle # when
> one already exists ... if I only had access to the cgiapp # $self
> instance. }

But you do; you just haven't seen how to yet:

# -----------------------------------------------

sub setup
{
	my($self) = @_;

	$self -> param(entity_profile =>
	{
		optional_regexp => qr/(?:input_(?:cost_centre|fund|gl)_code_\d+_\d+)/,
		...
		constraint_regexp_map =>
		{
			qr/input_(?:cost_centre|fund|gl)_code_\d+_\d+/ =>
			{
				constraint_method	=> \&validate_report_context,
				name				=> 'Report context',
			},
		}
	}
	...

Jump down to validate_update_entity
and keep reading...

# -----------------------------------------------
# Warning: This is a sub, not a method.
# Hence we do not start with my($self).

sub validate_report_context
{
	my($dfv)    = @_;
	my($self)   = $dfv -> get_input_data();
	my($entity) = $self -> param('entity');
	my($code)   = $self -> validate_report_code($dfv -> 
get_current_constraint_value() );
	my($result) = 1;
	$result     = 0 if ($code eq '-'); # See validate_report_code.

	if ($result)
	{
		# Build up the entity to be saved to disk if everything validates.

		$$entity{'entity_report_context'}{$code} = 1;

		$self -> param(entity => $entity);
	}

	$result;

}	# End of validate_report_context.

# -----------------------------------------------

sub validate_update_entity
{
	my($self)   = @_;
	my($entity) = $self -> param('database') -> build_fake_entity(); # Don't hit 
db.

	$self -> param(entity => $entity);

	my($result) = Data::FormValidator -> check($self, $self -> 
param('entity_profile') ); # Calls validate_report_context

....

Notes:

o See how I pass around a variable, $self -> param('entity'), which holds the 
outcome of each step in the validation process, and that this can be repeated 
endlessly for any number of attributes of an entity, or for any number of 
variables. Ultimately, if all valid, those variables gets written to the db

o The regexp_map does not need that strange syntax:
	params => [\...]
to get values into the validation subs

o The CGI::App self gets used in the call to check(...) in such a way as to make 
it available within every validation sub
-- 
Cheers
Ron Savage, suppressed on 2/07/2005
http://savage.net.au/index.html
Let the record show: Microsoft is not an Australian company



---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
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.