You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Emanuel Kadziela <ek...@SCANSOFTWARE.COM> on 2005/03/24 14:57:50 UTC

Validating nested Form Components

Hi,

 

I have a problem with validating nested form components over which I am
looping. This is a pretty complex application, so I going to try to
explain it without copying and pasting too much code. 

Basically, one part of my form is a user defined section for collecting
additional information from their customers (in other words, my clients
define some form parameters in their database and those parameters drive
the additional info part of the form). This section is divided into
groups, and each group has a heading, display format (checkboxes, test
fields, etc.) and finally whether the group is required or not. Within
each group the client can then specify any number of inputs that can be
filled out. The way I dress up and validate this section is to show the
required groups in red, non-required in black (default), and to demand
that at least one input in a required group be filled in. Everything
works beautifully except that if the first group in this section is
required, they all end up being required (not just dressed up in red,
but actually failing the validation and reporting errors unless at least
one input is filled in) regardless of what is specified in the database,
and vice versa, if the first group in not required, they all end up not
required. I spent much time debugging this problem, and find that the
trouble stems from the fact that my validator (I call it
RequiredValidator, which implements IValidator and simply checks if a
group is required or not) only gets called ONCE at the beginning of the
loop and not in every iteration. Therefore, it makes sense that the
first group's required setting gets applied to all the rest.

 

I hope the above makes some sense and that someone can help me,
please...


Re: Validating nested Form Components

Posted by Jamie Orchard-Hays <ja...@dang.com>.
Have you come up with a solution? That's a tricky one. I was thinking you 
could create a new validator on each loop--but really you can't.




----- Original Message ----- 
From: "Emanuel Kadziela" <ek...@SCANSOFTWARE.COM>
To: <ta...@jakarta.apache.org>
Sent: Thursday, March 24, 2005 8:57 AM
Subject: Validating nested Form Components


Hi,



I have a problem with validating nested form components over which I am
looping. This is a pretty complex application, so I going to try to
explain it without copying and pasting too much code.

Basically, one part of my form is a user defined section for collecting
additional information from their customers (in other words, my clients
define some form parameters in their database and those parameters drive
the additional info part of the form). This section is divided into
groups, and each group has a heading, display format (checkboxes, test
fields, etc.) and finally whether the group is required or not. Within
each group the client can then specify any number of inputs that can be
filled out. The way I dress up and validate this section is to show the
required groups in red, non-required in black (default), and to demand
that at least one input in a required group be filled in. Everything
works beautifully except that if the first group in this section is
required, they all end up being required (not just dressed up in red,
but actually failing the validation and reporting errors unless at least
one input is filled in) regardless of what is specified in the database,
and vice versa, if the first group in not required, they all end up not
required. I spent much time debugging this problem, and find that the
trouble stems from the fact that my validator (I call it
RequiredValidator, which implements IValidator and simply checks if a
group is required or not) only gets called ONCE at the beginning of the
loop and not in every iteration. Therefore, it makes sense that the
first group's required setting gets applied to all the rest.



I hope the above makes some sense and that someone can help me,
please...



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Validating nested Form Components

Posted by Kent Tong <ke...@cpttm.org.mo>.
Emanuel Kadziela <ekadziela <at> SCANSOFTWARE.COM> writes:

> I spent much time debugging this problem, and find that the
> trouble stems from the fact that my validator (I call it
> RequiredValidator, which implements IValidator and simply checks if a
> group is required or not) only gets called ONCE at the beginning of the
> loop and not in every iteration. Therefore, it makes sense that the
> first group's required setting gets applied to all the rest.

I've made a test page which contains three text fields by
looping. All text fields accept integers only. The first 
text field is required. The page seems to work fine. See 
if it can solve your problem.

LoopValidate.html:
<html>
<body jwcid="@Body">
<span jwcid="@Delegator" delegate="ognl: beans.delegate.firstError"/>
<form jwcid="@Form" listener="ognl:listeners.onSubmit"
delegate="ognl:beans.delegate">
	<span jwcid="@Foreach" source="ognl: {'a', 'b', 'c'}" 
		index="ognl: index">
		<input type="TextField" jwcid="@ValidField" 
			value="ognl: value"
			validator="ognl: validator" displayName="foo"/>
	</span>
	<input type="Submit" value="OK"/>
</form>
</body>
</html>

LoopValidate.page:
<page-specification class="mo.org.cpttm.phonebook.LoopValidate">
	<property-specification name="index" type="int"/>
	<property-specification name="values" type="int[]"
		initial-value="new int[3]"/>
	<bean name="delegate" 
		class="org.apache.tapestry.valid.ValidationDelegate"/>
</page-specification>

LoopValidate.java:
public abstract class LoopValidate extends BasePage {
	public abstract int[] getValues();
	public abstract int getIndex();

	public IValidator getValidator() {
		NumberValidator validator = new NumberValidator();
		validator.setRequired(getIndex() == 0);
		validator.setClientScriptingEnabled(true);
		return validator;
	}
	public int getValue() {
		return getValues()[getIndex()];
	}
	public void setValue(int value) {
		getValues()[getIndex()] = value;
	}
	public void onSubmit(IRequestCycle cycle) {
		ValidationDelegate delegate = 
			(ValidationDelegate) getBeans().getBean("delegate");
		if(delegate.getHasErrors()) {
			return;
		}
		System.out.println("OK");
	}
}


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org