You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by George Christman <gc...@cardaddy.com> on 2012/03/19 19:25:35 UTC

Re: Server Side Validation with ajax form loop work around

David, I have a working work around, I'm sure it's not the best, but does
work. See code below. 

    @Persist
    private PurchaseRequest prPersist;

    private PurchaseRequest pr;

    private final String NEW_OBJ = "new";

    void onActivate() {
        if(prPersist != null) {
            for(LineItem _lineItem : prPersist.getLineItems()) {
                if(_lineItem.getId() == null ||
!pr.getLineItems().contains(_lineItem)) {
                    _lineItem.setPurchaseRequest(pr);
                    pr.getLineItems().add(_lineItem);
                }
            }
        }
       prPersist = null;
    }

    void onValidateFromPR() {
        if (form.getHasErrors()) {
            prPersist = pr;            
        }
    }

    //Modified Geoff's value encoder. 
    @SuppressWarnings("unchecked")
    public ValueEncoder getEncoderLineItem() {
        return new ValueEncoder<LineItem>() {

            public String toClient(LineItem value) {
                Long id = value.getId();
                return id == null ? NEW_OBJ : id.toString();
            }

            public LineItem toValue(String idAsString) {
                LineItem lineItem = null;

                if (!idAsString.equals(NEW_OBJ)) {
                    Long id = new Long(idAsString);
                    lineItem = (LineItem) session.get(LineItem.class, id);
                }

                // AjaxFormLoop can't handle null obj, so if null we return
a new empty obj.
                lineItem = lineItem == null ? new LineItem() : lineItem;

                if (!request.isXHR()) {
                    lineItem.setPurchaseRequest(pr);
                    pr.getLineItems().add(lineItem);
                }
                
                return lineItem;
            }
        };
    }

Hope this helps. 

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Server-Side-Validation-with-ajax-form-loop-tp5120576p5577886.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Server Side Validation with ajax form loop work around

Posted by Geoff Callender <ge...@gmail.com>.
That's good news. Thanks for letting me know.

Geoff

On 18/04/2012, at 2:31 AM, David Canteros wrote:

> I have done a quick test and it works perfectly!
> Thanks Geoff!
> 
> 
> ------------------------------------------------------------------
> David Germán Canteros
> 
> 
> 2012/4/12 Geoff Callender <ge...@gmail.com>
> 
>> Hi all,
>> 
>> I've taken on board all the suggestions and observations here about
>> AjaxFormLoop problems and fixes, and done a complete rewrite of the
>> JumpStart examples. I've put a beta on the demo site. Is every corner case
>> handled now? I sure hope so. See if you can find a hole in it.
>> 
>>       http://jumpstart.doublenegative.com.au/jumpstart/
>> 
>> Cheers,
>> 
>> Geoff
>> 
>> 
>> On 23/03/2012, at 1:57 AM, George Christman wrote:
>> 
>>> Hi David, after some testing I wanted to provide you with some updates
>> based
>>> on some scenarios I ran into.
>>> 
>>> You'll notice in the !request.isHXR condition, I'm checking for a null
>>> lineItem id. This prevents duplicate objects from being added to the
>>> collection.
>>> 
>>>   @SuppressWarnings("unchecked")
>>>   public ValueEncoder getEncoderLineItem() {
>>>       return new ValueEncoder<LineItem>() {
>>>           public String toClient(LineItem value) {
>>>               Long id = value.getId();
>>>               return id == null ? NEW_OBJ : id.toString();
>>>           }
>>> 
>>>           public LineItem toValue(String idAsString) {
>>>               lineItem = null;
>>> 
>>>               if (!idAsString.equals(NEW_OBJ)) {
>>>                   Long id = new Long(idAsString);
>>>                   lineItem = (LineItem) session.get(LineItem.class, id);
>>>               }
>>> 
>>>               // AjaxFormLoop can't handle null obj, so if null we
>> return
>>> a new empty obj.
>>>               lineItem = lineItem == null ? new LineItem() : lineItem;
>>> 
>>>               if (!request.isXHR() && lineItem.getId() == null) {
>>>                   lineItem.setPurchaseRequest(pr);
>>>                   pr.getLineItems().add(lineItem);
>>>               }
>>>               return lineItem;
>>>           }
>>>       };
>>>   }
>>> 
>>> I also found a cleaner way to copy the persisted data to the new session.
>>> 
>>> 
>>>   void onActivate() {
>>>       System.out.println("onActivate " + this.pr);
>>> 
>>>       if (this.pr == null) {
>>>           this.pr = prPersist != null ? prPersist : new
>> PurchaseRequest();
>>>           prPersist = null;
>>>       }
>>> 
>>>   }
>>> 
>>>   Class<?> onActivate(Long prId) {
>>> 
>>>       PurchaseRequest purchaseRequest = prPersist != null ? prPersist :
>>> (PurchaseRequest) session.get(PurchaseRequest.class, prId);
>>>   }
>>> 
>>> If you have any better solutions, please feel free to share.
>>> 
>>> 
>>> --
>>> View this message in context:
>> http://tapestry.1045711.n5.nabble.com/Server-Side-Validation-with-ajax-form-loop-tp5120576p5586509.html
>>> Sent from the Tapestry - User mailing list archive at Nabble.com.
>>> 
>>> ---------------------------------------------------------------------
>>> 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
>> 
>> 


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


Re: Server Side Validation with ajax form loop work around

Posted by David Canteros <da...@gmail.com>.
I have done a quick test and it works perfectly!
Thanks Geoff!


------------------------------------------------------------------
David Germán Canteros


2012/4/12 Geoff Callender <ge...@gmail.com>

> Hi all,
>
> I've taken on board all the suggestions and observations here about
> AjaxFormLoop problems and fixes, and done a complete rewrite of the
> JumpStart examples. I've put a beta on the demo site. Is every corner case
> handled now? I sure hope so. See if you can find a hole in it.
>
>        http://jumpstart.doublenegative.com.au/jumpstart/
>
> Cheers,
>
> Geoff
>
>
> On 23/03/2012, at 1:57 AM, George Christman wrote:
>
> > Hi David, after some testing I wanted to provide you with some updates
> based
> > on some scenarios I ran into.
> >
> > You'll notice in the !request.isHXR condition, I'm checking for a null
> > lineItem id. This prevents duplicate objects from being added to the
> > collection.
> >
> >    @SuppressWarnings("unchecked")
> >    public ValueEncoder getEncoderLineItem() {
> >        return new ValueEncoder<LineItem>() {
> >            public String toClient(LineItem value) {
> >                Long id = value.getId();
> >                return id == null ? NEW_OBJ : id.toString();
> >            }
> >
> >            public LineItem toValue(String idAsString) {
> >                lineItem = null;
> >
> >                if (!idAsString.equals(NEW_OBJ)) {
> >                    Long id = new Long(idAsString);
> >                    lineItem = (LineItem) session.get(LineItem.class, id);
> >                }
> >
> >                // AjaxFormLoop can't handle null obj, so if null we
> return
> > a new empty obj.
> >                lineItem = lineItem == null ? new LineItem() : lineItem;
> >
> >                if (!request.isXHR() && lineItem.getId() == null) {
> >                    lineItem.setPurchaseRequest(pr);
> >                    pr.getLineItems().add(lineItem);
> >                }
> >                return lineItem;
> >            }
> >        };
> >    }
> >
> > I also found a cleaner way to copy the persisted data to the new session.
> >
> >
> >    void onActivate() {
> >        System.out.println("onActivate " + this.pr);
> >
> >        if (this.pr == null) {
> >            this.pr = prPersist != null ? prPersist : new
> PurchaseRequest();
> >            prPersist = null;
> >        }
> >
> >    }
> >
> >    Class<?> onActivate(Long prId) {
> >
> >        PurchaseRequest purchaseRequest = prPersist != null ? prPersist :
> > (PurchaseRequest) session.get(PurchaseRequest.class, prId);
> >    }
> >
> > If you have any better solutions, please feel free to share.
> >
> >
> > --
> > View this message in context:
> http://tapestry.1045711.n5.nabble.com/Server-Side-Validation-with-ajax-form-loop-tp5120576p5586509.html
> > Sent from the Tapestry - User mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > 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: Server Side Validation with ajax form loop work around

Posted by Geoff Callender <ge...@gmail.com>.
Hi all,

I've taken on board all the suggestions and observations here about AjaxFormLoop problems and fixes, and done a complete rewrite of the JumpStart examples. I've put a beta on the demo site. Is every corner case handled now? I sure hope so. See if you can find a hole in it.

	http://jumpstart.doublenegative.com.au/jumpstart/

Cheers,

Geoff


On 23/03/2012, at 1:57 AM, George Christman wrote:

> Hi David, after some testing I wanted to provide you with some updates based
> on some scenarios I ran into. 
> 
> You'll notice in the !request.isHXR condition, I'm checking for a null
> lineItem id. This prevents duplicate objects from being added to the
> collection. 
> 
>    @SuppressWarnings("unchecked")
>    public ValueEncoder getEncoderLineItem() {
>        return new ValueEncoder<LineItem>() {
>            public String toClient(LineItem value) {
>                Long id = value.getId();
>                return id == null ? NEW_OBJ : id.toString();
>            }
> 
>            public LineItem toValue(String idAsString) {
>                lineItem = null;
> 
>                if (!idAsString.equals(NEW_OBJ)) {
>                    Long id = new Long(idAsString);
>                    lineItem = (LineItem) session.get(LineItem.class, id);
>                }
> 
>                // AjaxFormLoop can't handle null obj, so if null we return
> a new empty obj.
>                lineItem = lineItem == null ? new LineItem() : lineItem;
> 
>                if (!request.isXHR() && lineItem.getId() == null) {
>                    lineItem.setPurchaseRequest(pr);
>                    pr.getLineItems().add(lineItem);
>                }                
>                return lineItem;
>            }
>        };
>    }
> 
> I also found a cleaner way to copy the persisted data to the new session. 
> 
> 
>    void onActivate() {   
>        System.out.println("onActivate " + this.pr);
> 
>        if (this.pr == null) {
>            this.pr = prPersist != null ? prPersist : new PurchaseRequest();
>            prPersist = null;
>        }
> 
>    }
> 
>    Class<?> onActivate(Long prId) {
> 
>        PurchaseRequest purchaseRequest = prPersist != null ? prPersist :
> (PurchaseRequest) session.get(PurchaseRequest.class, prId);
>    }
> 
> If you have any better solutions, please feel free to share. 
> 
> 
> --
> View this message in context: http://tapestry.1045711.n5.nabble.com/Server-Side-Validation-with-ajax-form-loop-tp5120576p5586509.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
> 
> ---------------------------------------------------------------------
> 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: Server Side Validation with ajax form loop work around

Posted by George Christman <gc...@cardaddy.com>.
Hi David, after some testing I wanted to provide you with some updates based
on some scenarios I ran into. 

You'll notice in the !request.isHXR condition, I'm checking for a null
lineItem id. This prevents duplicate objects from being added to the
collection. 

    @SuppressWarnings("unchecked")
    public ValueEncoder getEncoderLineItem() {
        return new ValueEncoder<LineItem>() {
            public String toClient(LineItem value) {
                Long id = value.getId();
                return id == null ? NEW_OBJ : id.toString();
            }

            public LineItem toValue(String idAsString) {
                lineItem = null;

                if (!idAsString.equals(NEW_OBJ)) {
                    Long id = new Long(idAsString);
                    lineItem = (LineItem) session.get(LineItem.class, id);
                }
                
                // AjaxFormLoop can't handle null obj, so if null we return
a new empty obj.
                lineItem = lineItem == null ? new LineItem() : lineItem;

                if (!request.isXHR() && lineItem.getId() == null) {
                    lineItem.setPurchaseRequest(pr);
                    pr.getLineItems().add(lineItem);
                }                
                return lineItem;
            }
        };
    }

I also found a cleaner way to copy the persisted data to the new session. 


    void onActivate() {   
        System.out.println("onActivate " + this.pr);

        if (this.pr == null) {
            this.pr = prPersist != null ? prPersist : new PurchaseRequest();
            prPersist = null;
        }

    }

    Class<?> onActivate(Long prId) {
       
        PurchaseRequest purchaseRequest = prPersist != null ? prPersist :
(PurchaseRequest) session.get(PurchaseRequest.class, prId);
    }

If you have any better solutions, please feel free to share. 


--
View this message in context: http://tapestry.1045711.n5.nabble.com/Server-Side-Validation-with-ajax-form-loop-tp5120576p5586509.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Server Side Validation with ajax form loop work around

Posted by David Canteros <da...@gmail.com>.
Hi George, finally it worked!  Thank you for the help!

Before I was using the ajaxformloop like as Geoff example, with the tree
arrays XXXtoCreate, XXXToChange and XXXtoDelete and the same encoder.  That
made me think,  probably the problem was caused by Geoff's example and not
by the loop component... I'll make some test next days about it.

Regards!

------------------------------------------------------------------
David Germán Canteros


2012/3/19 George Christman <gc...@cardaddy.com>

> Hi David, I'm using the pr object like so
>
> <div t:type="ajaxFormLoop" t:id="lineItem" source="pr.lineItems"
> value="lineItem" addRow="block:addRow" show="show"
> encoder="encoderLineItem">
>
> It seems to be working perfectly for me. I'm only using the prPersist
> object
> to hold a temporary copy of the current pr at validation. When the page
> reloads with the validation errors, I'm using the prPersist to extract and
> populate my current lineItem objects before immediately disregarding. Be
> sure to implement the value encoder in a similar fashion.
>
>
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Server-Side-Validation-with-ajax-form-loop-tp5120576p5578227.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Server Side Validation with ajax form loop work around

Posted by George Christman <gc...@cardaddy.com>.
Hi David, I'm using the pr object like so

<div t:type="ajaxFormLoop" t:id="lineItem" source="pr.lineItems"
value="lineItem" addRow="block:addRow" show="show"
encoder="encoderLineItem">

It seems to be working perfectly for me. I'm only using the prPersist object
to hold a temporary copy of the current pr at validation. When the page
reloads with the validation errors, I'm using the prPersist to extract and
populate my current lineItem objects before immediately disregarding. Be
sure to implement the value encoder in a similar fashion.  



--
View this message in context: http://tapestry.1045711.n5.nabble.com/Server-Side-Validation-with-ajax-form-loop-tp5120576p5578227.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: Server Side Validation with ajax form loop work around

Posted by David Canteros <da...@gmail.com>.
Hi George,
 i've done a quick test and it doesnt work on my code, but i will do a full
test by tomorrow. Only one question: which object are you using in the
ajaxformloop (in the tml code)? pr or prPersist?

Regards and thanks!

------------------------------------------------------------------
David Germán Canteros


2012/3/19 George Christman <gc...@cardaddy.com>

> David, I have a working work around, I'm sure it's not the best, but does
> work. See code below.
>
>    @Persist
>    private PurchaseRequest prPersist;
>
>    private PurchaseRequest pr;
>
>    private final String NEW_OBJ = "new";
>
>    void onActivate() {
>        if(prPersist != null) {
>            for(LineItem _lineItem : prPersist.getLineItems()) {
>                if(_lineItem.getId() == null ||
> !pr.getLineItems().contains(_lineItem)) {
>                    _lineItem.setPurchaseRequest(pr);
>                    pr.getLineItems().add(_lineItem);
>                }
>            }
>        }
>       prPersist = null;
>    }
>
>    void onValidateFromPR() {
>        if (form.getHasErrors()) {
>            prPersist = pr;
>        }
>    }
>
>    //Modified Geoff's value encoder.
>    @SuppressWarnings("unchecked")
>    public ValueEncoder getEncoderLineItem() {
>        return new ValueEncoder<LineItem>() {
>
>            public String toClient(LineItem value) {
>                Long id = value.getId();
>                return id == null ? NEW_OBJ : id.toString();
>            }
>
>            public LineItem toValue(String idAsString) {
>                LineItem lineItem = null;
>
>                if (!idAsString.equals(NEW_OBJ)) {
>                    Long id = new Long(idAsString);
>                    lineItem = (LineItem) session.get(LineItem.class, id);
>                }
>
>                // AjaxFormLoop can't handle null obj, so if null we return
> a new empty obj.
>                lineItem = lineItem == null ? new LineItem() : lineItem;
>
>                if (!request.isXHR()) {
>                    lineItem.setPurchaseRequest(pr);
>                    pr.getLineItems().add(lineItem);
>                }
>
>                return lineItem;
>            }
>        };
>    }
>
> Hope this helps.
>
> --
> View this message in context:
> http://tapestry.1045711.n5.nabble.com/Server-Side-Validation-with-ajax-form-loop-tp5120576p5577886.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>