Hi,

I’m having some trouble getting a DropDownChoice component to bind the value
I want to the supplied model and could use some help. More specifically,
I’ve created a custom dropdown by extending DropDownChoice (see below)
because I need to use a custom value for the hidden value attribute in the
HTML option tag and a more readable version of that value for display. The
hidden value needs to be bound to the model, not the display value.
Everything renders and binds to the model properly the first time the user
fills out and submits the form. The problem appears when the user returns to
the form (which will display his/her previous choices) and tries to update
the selection in the dropdown. For some reason unknown to me the when the
user updates the form, the value from the dropdown that is passed to the
model is the (undesired) display value instead of the (desired) hidden value
attribute data that is passed to it the first time the form is submitted.

Thoughts?

Thanks,
Peter



Package…
Import…

public class DateDropDownChoice extends DropDownChoice implements
Serializable {
    // values to go into the hidden value attribute in the HTML option tag
    // AND to go into the model/database
    final static private String[] ids =
{"20031","20032","20033","20034","20041",…};

    // values to be displayed in the dropdown
    final static private String[] values ={"2003, Fall","2003,
Winter","2003, Spring","2003, Summer","2004, Fall",…};

    public DateDropDownChoice(String id, IModel iModel) {
        super(id, iModel);
        ArrayList choices = new ArrayList();

        int i = 0;
        for(;i<ids.length;i++)
           // choices.add(new DateChoice(ids[i],values[i]));
              choices.add(values[i]);

        this.setChoices(choices);
        this.setChoiceRenderer(new CustomChoiceRenderer());
    }



    class CustomChoiceRenderer implements IChoiceRenderer{


        public Object getDisplayValue(Object obj) {
            return obj.toString();
        }

        public String getIdValue(Object obj, int i) {

            /**
             * An "i" of -1 is passed when a previous selection
             * has been made and is stored, so just return the obj string
value
             * because it is a properly stored value such as "20063" from
the previous submission
             *
             * An "i" > 0 means that I need to look up the
appropriate/corresponding
             * value from the ids array.
             *
             */

            String returnVal = i<0?obj.toString():ids[i];
            System.out.println("returnVal: "+returnVal+" :: ID:: "+i);
            return returnVal;
        }
    }
}

And then in my form I set up the custom dropdown.

//…

DateDropDownChoice studentSince =
                     new DateDropDownChoice("studentSince",new
PropertyModel(iModel,"studentSince"));
             add(studentSince);

//…

-- 
View this message in context: 
http://www.nabble.com/DropDownChoice-inconsistent-value-returned-tf2971300.html#a8314245
Sent from the Wicket - User mailing list archive at Nabble.com.


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Wicket-user mailing list
Wicket-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to