You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Steven Haines <ly...@yahoo.com> on 2010/03/22 16:09:50 UTC

TextField hosting an Integer

Hi,

I'm creating a text field to host an integer, namely the year that a house was built:
TextField<Integer> yearBuilt = new TextField<Integer>( "yearBuilt" ).setRequired( true );

And I'm using a CompoundPropertyModel that maps "yearBuilt" to an underlying bean property. My problem is that the default value for an integer is "0", so the text field pre-populates its value to "0", which is a rather silly year built date ;)  What I would like to do is mark the field as required and validate against a number range, but display the text field without an initial value. 

I know I can make it a TextField<String> and then validate on submission, but what has me intrigued is my exploration into the DropDownChoice and the creation of renderers. For example, I have a dropdown list from which a user can choose a value between 0 and 50,000 (increments of 1000), but I built a custom renderer that displays "No Coverage" for "0" (insurance industry.) Can I build a custom renderer for the initial value of a text field so that if I see "0" I return "", otherwise I return the actual value, e.g. 2001?

Thanks in advance,
Steve


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


Re: TextField hosting an Integer

Posted by Swanthe Lindgren <sw...@megasol.se>.
And add RangeValidator.

//Swanthe

On 2010-03-22 17:00, James Carman wrote:
> Change your property to Integer and not int?  That way, it can be null
> (which would show up as empty on the text field).
>
> On Mon, Mar 22, 2010 at 11:09 AM, Steven Haines<ly...@yahoo.com>  wrote:
>    
>> Hi,
>>
>> I'm creating a text field to host an integer, namely the year that a house was built:
>> TextField<Integer>  yearBuilt = new TextField<Integer>( "yearBuilt" ).setRequired( true );
>>
>> And I'm using a CompoundPropertyModel that maps "yearBuilt" to an underlying bean property. My problem is that the default value for an integer is "0", so the text field pre-populates its value to "0", which is a rather silly year built date ;)  What I would like to do is mark the field as required and validate against a number range, but display the text field without an initial value.
>>
>> I know I can make it a TextField<String>  and then validate on submission, but what has me intrigued is my exploration into the DropDownChoice and the creation of renderers. For example, I have a dropdown list from which a user can choose a value between 0 and 50,000 (increments of 1000), but I built a custom renderer that displays "No Coverage" for "0" (insurance industry.) Can I build a custom renderer for the initial value of a text field so that if I see "0" I return "", otherwise I return the actual value, e.g. 2001?
>>
>> Thanks in advance,
>> Steve
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>>      
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
>    



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


Re: TextField hosting an Integer

Posted by James Carman <jc...@carmanconsulting.com>.
Change your property to Integer and not int?  That way, it can be null
(which would show up as empty on the text field).

On Mon, Mar 22, 2010 at 11:09 AM, Steven Haines <ly...@yahoo.com> wrote:
> Hi,
>
> I'm creating a text field to host an integer, namely the year that a house was built:
> TextField<Integer> yearBuilt = new TextField<Integer>( "yearBuilt" ).setRequired( true );
>
> And I'm using a CompoundPropertyModel that maps "yearBuilt" to an underlying bean property. My problem is that the default value for an integer is "0", so the text field pre-populates its value to "0", which is a rather silly year built date ;)  What I would like to do is mark the field as required and validate against a number range, but display the text field without an initial value.
>
> I know I can make it a TextField<String> and then validate on submission, but what has me intrigued is my exploration into the DropDownChoice and the creation of renderers. For example, I have a dropdown list from which a user can choose a value between 0 and 50,000 (increments of 1000), but I built a custom renderer that displays "No Coverage" for "0" (insurance industry.) Can I build a custom renderer for the initial value of a text field so that if I see "0" I return "", otherwise I return the actual value, e.g. 2001?
>
> Thanks in advance,
> Steve
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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


Re: TextField hosting an Integer

Posted by Thierry Peng <pe...@glue.ch>.
Steven Haines schrieb:
> Hi,
>
> I'm creating a text field to host an integer, namely the year that a house was built:
> TextField<Integer> yearBuilt = new TextField<Integer>( "yearBuilt" ).setRequired( true );
>
> And I'm using a CompoundPropertyModel that maps "yearBuilt" to an underlying bean property. My problem is that the default value for an integer is "0", so the text field pre-populates its value to "0", which is a rather silly year built date ;)  What I would like to do is mark the field as required and validate against a number range, but display the text field without an initial value. 
>
> I know I can make it a TextField<String> and then validate on submission, but what has me intrigued is my exploration into the DropDownChoice and the creation of renderers. For example, I have a dropdown list from which a user can choose a value between 0 and 50,000 (increments of 1000), but I built a custom renderer that displays "No Coverage" for "0" (insurance industry.) Can I build a custom renderer for the initial value of a text field so that if I see "0" I return "", otherwise I return the actual value, e.g. 2001?
>
> Thanks in advance,
> Steve
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>   
overwrite the getConverter on your textfield

@Override
    public IConverter getConverter(Class<?> type)
    {
      NumberConverter fc = new NumberConverter()
      {
        @Override
        public String convertToString(Object value, Locale locale)
        {
            //insert your conversion logic here
        }
      };
      return fc;
    }

best

-- 
Thierry Peng


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