You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Martin Dietze <di...@fh-wedel.de> on 2007/05/29 17:35:09 UTC

T5: more than one select options set to selected="selected"

Hi,

 I use a custom select model based on an AbstractSelectModel
and using a custom ValueEncoder. When running the code I get
this here generated (newlines added by me *g*):

| <select id="salution" name="salution" tabindex="10">
| <option foo="bar" selected="selected" value="null">bitte wählen...<
|  option disabled="disabled" selected="selected" value="null">---------------<
|  option value="MR">Herr<
|  option value="MRS">Frau
| </select><
|  img alt="[Error]" class="t-error-icon t-invisible" id="salution:icon"
|  src="/my-application-foo/assets/tapestry/field-error-marker.png">

As you see the first two options have the attribute "selected"
which is of course not what I want.

Now I wonder is this a bug or a feature (i.e. my bug *g*)?

Cheers,

Martin

<-------------- snip / my code ---------------->

The relevant snippet from the template looks like this:

| <select name="salutation" id="salutation" tabindex="10"
|         t:type="select"
|         t:id="salution"
|         t:encoder="salutationValueEncoder"
|         t:model="salutationModel"
|         t:value="contact.salutation">
|     <option selected="selected">bitte wählen...</option>
|     <option disabled="disabled">---------------</option>
|     <option>Herr</option>
|     <option>Frau</option>
| </select>

This is the selectmodel:

| public final class SalutationSelectModel extends AbstractSelectModel {
|     
|   private final Messages _msgs;
| 
|   public SalutationSelectModel( final Messages msgs ) {
|     _msgs = msgs;
|   }
| 
|   public List<OptionGroupModel> getOptionGroups() {
|     return null;
|   }
| 
|   public List<OptionModel> getOptions() {
|     final List<OptionModel> result = new ArrayList<OptionModel>();
|     result.add( new OptionModelImpl( _msgs.get( "select_pleaseSelect" ),
|         false, null, new String[0] ) );
|     result.add( new OptionModelImpl( _msgs.get( "select_separator" ),
|         true, null, new String[0] ) );
|     for( Salutation salut : Salutation.values() ) {
|       result.add( new OptionModelImpl( _msgs.get( "salutation_" + salut.name() ),
|           false, salut, new String[0] ) );
|     }
|     
|     return result;
|   }
| 
| }

And (if at all relevant) the valueencoder:

| public class SalutationValueEncoder implements ValueEncoder<Salutation> {
|
|   public String toClient( Salutation value ) {
|     if ( value == null )
|       return "null";
|     return value.name();
|   }
| 
|   public Salutation toValue( String clientValue ) {
|     if ( "null".equals( clientValue ) )
|       return null;
|     return Salutation.valueOf( clientValue );
|   }
| 
| }


-- 
------------- / http://herbert.the-little-red-haired-girl.org / -------------
=+= 
Was ist ein Cluster?
Wenn vier Bratscher unisono spielen.

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


Re: T5: more than one select options set to selected="selected"

Posted by Howard Lewis Ship <hl...@gmail.com>.
Well, obviously there's the issue that you have multiple options with the
same value, and that you are going to great lengths to convert between the
null constant and the "null" string.

Since you are building the SelectModel explicitly, you may want to consider
using OptionGroups as a way to label selections, they way HTML intends,
rather than use null/disabled options.  But test across browsers since they
are very inconsistent in their implementation.

On 5/29/07, Martin Dietze <di...@fh-wedel.de> wrote:
>
> Hi,
>
> I use a custom select model based on an AbstractSelectModel
> and using a custom ValueEncoder. When running the code I get
> this here generated (newlines added by me *g*):
>
> | <select id="salution" name="salution" tabindex="10">
> | <option foo="bar" selected="selected" value="null">bitte wählen...<
> |  option disabled="disabled" selected="selected"
> value="null">---------------<
> |  option value="MR">Herr<
> |  option value="MRS">Frau
> | </select><
> |  img alt="[Error]" class="t-error-icon t-invisible" id="salution:icon"
> |  src="/my-application-foo/assets/tapestry/field-error-marker.png">
>
> As you see the first two options have the attribute "selected"
> which is of course not what I want.
>
> Now I wonder is this a bug or a feature (i.e. my bug *g*)?
>
> Cheers,
>
> Martin
>
> <-------------- snip / my code ---------------->
>
> The relevant snippet from the template looks like this:
>
> | <select name="salutation" id="salutation" tabindex="10"
> |         t:type="select"
> |         t:id="salution"
> |         t:encoder="salutationValueEncoder"
> |         t:model="salutationModel"
> |         t:value="contact.salutation">
> |     <option selected="selected">bitte wählen...</option>
> |     <option disabled="disabled">---------------</option>
> |     <option>Herr</option>
> |     <option>Frau</option>
> | </select>
>
> This is the selectmodel:
>
> | public final class SalutationSelectModel extends AbstractSelectModel {
> |
> |   private final Messages _msgs;
> |
> |   public SalutationSelectModel( final Messages msgs ) {
> |     _msgs = msgs;
> |   }
> |
> |   public List<OptionGroupModel> getOptionGroups() {
> |     return null;
> |   }
> |
> |   public List<OptionModel> getOptions() {
> |     final List<OptionModel> result = new ArrayList<OptionModel>();
> |     result.add( new OptionModelImpl( _msgs.get( "select_pleaseSelect" ),
> |         false, null, new String[0] ) );
> |     result.add( new OptionModelImpl( _msgs.get( "select_separator" ),
> |         true, null, new String[0] ) );
> |     for( Salutation salut : Salutation.values() ) {
> |       result.add( new OptionModelImpl( _msgs.get( "salutation_" +
> salut.name() ),
> |           false, salut, new String[0] ) );
> |     }
> |
> |     return result;
> |   }
> |
> | }
>
> And (if at all relevant) the valueencoder:
>
> | public class SalutationValueEncoder implements ValueEncoder<Salutation>
> {
> |
> |   public String toClient( Salutation value ) {
> |     if ( value == null )
> |       return "null";
> |     return value.name();
> |   }
> |
> |   public Salutation toValue( String clientValue ) {
> |     if ( "null".equals( clientValue ) )
> |       return null;
> |     return Salutation.valueOf( clientValue );
> |   }
> |
> | }
>
>
> --
> ------------- / http://herbert.the-little-red-haired-girl.org /
> -------------
> =+=
> Was ist ein Cluster?
> Wenn vier Bratscher unisono spielen.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship
TWD Consulting, Inc.
Independent J2EE / Open-Source Java Consultant
Creator and PMC Chair, Apache Tapestry
Creator, Apache HiveMind

Professional Tapestry training, mentoring, support
and project work.  http://howardlewisship.com