struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dave Newton <new...@pingsite.com>
Subject Re: DynaActionForm, Boolean checkboxes, and Initial
Date Wed, 12 Oct 2005 16:14:44 GMT
katre wrote:

>The basic problem, as before, is that it is totally impossible in struts
>to default a checkbox to true, and still be able to detect when it is
>uncheked by a user.
>  
>
Wow. All those apps of mine, ruined ;)

>Someone, please prove me wrong! Tell me struts isn't this limited!  Tell
>me how to fix this!  Otherwise, I'll have to go add javascript hackery
>and extra hidden fields to every JSP that uses a checkbox, and that's a
>lot.
>  
>
That's crazy.

Um, well, what _I've_ been doing is setting the appropriate ActionForm 
value before I display the form, not in reset but in the Action's 
execute method (reset works too, but because I've never used 
validate="true" I'm doing setup stuff in the Action anyway). After form 
submision I look at the form value of the checkbox (I usually use 
Boolean, but I almost always use DynaValidatorActionForms, so YMMV on 
that one).

Somehow I can create and edit records with checkboxes despite the "total 
impossibility" :D Sometimes I have to marshall the checkbox value into 
an integer if that's what I'm using as a flag in the DB.

I've appended some old code that shows (more or less) one way of 
approaching this (and I eagerly await the code style comments ;)

Dave

// Code edited to protect the not-so-innocent and hide framework details
// Loads the required OM on an edit; before showing the form, i.e. GET 
request
        AnnouncementType anntype = 
AnnouncementTypePeer.retrieveByEventAndType(event_.getEventId(), typeId_);
        if (anntype == null) {
            request_.setAttribute("dontShowForm", Boolean.TRUE); // <-- 
Go ahead, mock me!
            return errorResourceToInput(request_, mapping_, 
"anntypebean.error.editRetrieval");
        }

        DynaActionForm form = (DynaActionForm) form_;
        BeanUtils.copyProperties(form, anntype);

        // Mock me some more!
        Boolean clientP = anntype.getClientAnnouncement() > 0 ? 
Boolean.TRUE : Boolean.FALSE;
        Boolean cbModeP = anntype.getCbMode() > 0 ? Boolean.TRUE : 
Boolean.FALSE;
        Boolean epcModeP = anntype.getEpcMode() > 0 ? Boolean.TRUE : 
Boolean.FALSE;
       
        form.set("clientAnnouncementP", clientP);
        form.set("cbModeP", cbModeP);
        form.set("epcModeP", epcModeP);

// Put form values back into OM and update after POST
            AnnouncementType announcementType = 
AnnouncementTypePeer.retrieveByEventAndType(event_.getEventId(), id_);
            if (announcementType == null) {
                request_.setAttribute("dontShowForm", Boolean.TRUE);
                return errorResourceToInput(request_, mapping_, 
"anntypebean.error.editRetrieval");
            }
            BeanUtils.copyProperties(announcementType, form_);
            announcementType.setAnnouncementPrice(safeMoney((String) 
form_.get("announcementPrice")));
            announcementType.setClientAnnouncement((Boolean) 
form_.get("clientAnnouncementP")); // <-- Yes, I'm a lisp programmer
            announcementType.setCbMode((Boolean) form_.get("cbModeP"));
            announcementType.setEpcMode((Boolean) form_.get("epcModeP"));
            AnnouncementTypePeer.doUpdate(announcementType);



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


Mime
View raw message