You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Barry Books <tr...@gmail.com> on 2013/03/22 14:20:07 UTC

How do you create a composite form component?

Usually creating Tapestry components is easy but I've run in a
problem. I've kinda solved it but I'm wondering if I'm missing
something.

I'd like to create a component called DateRange which contains two
DateFields and takes a parameter of type Range which contains a
startDate and endDate. The solution seems easy just create a component
then put two datefields in it. The problem is you can't use label on
that because the DateRange does not implement Field. Fair enough just
implement Field.  I copied RadioGroup because it's seemed like a good
starting point. Now I wanted to use it in a BeanEditForm and ran into
another problem. If range is null I'd like to create it but there does
not seem to be an onPrepareForSubmit event. I've managed to work thru
that also but the whole result seems messy and I still have not really
figured out where to check that startDate < endDate.

Has anyone tried to create composite form components and if so what
are the secrets?

Thanks
Barry

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


Re: How do you create a composite form component?

Posted by Barry Books <tr...@gmail.com>.
The JIRA is interesting and I agree it does seem like a lot of
plumbing to do this. I suspect this could also be solved with an
@CompositeField annotation and a worker. Your example is similar to
what I came up with except I need to do validation so I have to create
a Runable.

private static class Submit implements ComponentAction<DateRange> {
	          private static final long serialVersionUID = -7984673040135949374L;
	
	          private final String controlName;
	
	          Submit(String controlName) {
	              this.controlName = controlName;
	          }
	
	          public void execute(DateRange component) {
	              component.submit(controlName);
	          }
	
	          @Override
	          public String toString() {
	              return String.format("DateRange.Setup[%s]", controlName);
	          }
	      }
	
	void setupRender() {
		formSupport.store(this,new
Submit(formSupport.allocateControlName(getClientId())));
		if ( range == null ) {
			range = new Range();
		}
	}
	
	public void submit(String controlName) {
		if ( range == null ) {
			range = new Range();
		}
		formSupport.defer( new Runnable() {			
			@Override
			public void run() {
				if ( range.getStartDate().after( range.getEndDate()) ){
					tracker.recordError("start must be before end");
				}
			}
		});
	}

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


Re: How do you create a composite form component?

Posted by Geoff Callender <ge...@gmail.com>.
If you'd like to see component writing made simpler, then please vote here:

	https://issues.apache.org/jira/browse/TAP5-1618

It proposes replacing all that plumbing (store ComponentAction, storeAndExecute ComponentAction) with event handlers (eg. onPrepare, onValidate, onSuccess, etc).

Cheers,

Geoff

On 23/03/2013, at 3:28 PM, Geoff Callender wrote:

> This may help:
> 
> 	http://jumpstart.doublenegative.com.au/jumpstart/examples/component/subformasafield1
> 
> On 23/03/2013, at 1:53 AM, Barry Books wrote:
> 
>> That's what I tried first and I did not like the path I was headed
>> down. Perhaps I'm spoiled by how easy it is to create a component
>> that's not used as a form element. I was really just curious if I was
>> missing something obvious.
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>> 
> 


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


Re: How do you create a composite form component?

Posted by Geoff Callender <ge...@gmail.com>.
This may help:

	http://jumpstart.doublenegative.com.au/jumpstart/examples/component/subformasafield1

On 23/03/2013, at 1:53 AM, Barry Books wrote:

> That's what I tried first and I did not like the path I was headed
> down. Perhaps I'm spoiled by how easy it is to create a component
> that's not used as a form element. I was really just curious if I was
> missing something obvious.
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
> 


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


Re: How do you create a composite form component?

Posted by Barry Books <tr...@gmail.com>.
That's what I tried first and I did not like the path I was headed
down. Perhaps I'm spoiled by how easy it is to create a component
that's not used as a form element. I was really just curious if I was
missing something obvious.

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


Re: How do you create a composite form component?

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Fri, 22 Mar 2013 10:56:25 -0300, Barry Books <tr...@gmail.com> wrote:

> I did and I'm using store() to make it work, it just seems messy when
> all I really want is something like this

Have you tried subclassing AbstractField?

-- 
Thiago H. de Paula Figueiredo

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


Re: How do you create a composite form component?

Posted by Barry Books <tr...@gmail.com>.
I did and I'm using store() to make it work, it just seems messy when
all I really want is something like this

DateRange implements Label {

@Parameter
private String label;

@Parameter
@Property
private Range range;

onPrepareFromSubmit() {
   if ( range == null ) {
       range = new Range();
   }
}

onValidate() {
  if ( startdate >= enddate ) {
      validation exception;
  }

String getLabel() {
   return label;
}

}

I guess I need to create an AbstractFormComposite and hide all the
complexity there.

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


Re: How do you create a composite form component?

Posted by Taha Siddiqi <ta...@gmail.com>.
Hi Barry

Have you looked at FormSupport particularly store() and storeAndExecute()

http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/services/FormSupport.html#storeAndExecute(T, org.apache.tapestry5.ComponentAction)

You can register ComponentActions there.

regards
Taha

On Mar 22, 2013, at 6:50 PM, Barry Books wrote:

> Usually creating Tapestry components is easy but I've run in a
> problem. I've kinda solved it but I'm wondering if I'm missing
> something.
> 
> I'd like to create a component called DateRange which contains two
> DateFields and takes a parameter of type Range which contains a
> startDate and endDate. The solution seems easy just create a component
> then put two datefields in it. The problem is you can't use label on
> that because the DateRange does not implement Field. Fair enough just
> implement Field.  I copied RadioGroup because it's seemed like a good
> starting point. Now I wanted to use it in a BeanEditForm and ran into
> another problem. If range is null I'd like to create it but there does
> not seem to be an onPrepareForSubmit event. I've managed to work thru
> that also but the whole result seems messy and I still have not really
> figured out where to check that startDate < endDate.
> 
> Has anyone tried to create composite form components and if so what
> are the secrets?
> 
> Thanks
> Barry
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>