You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Sebastian Gaul <se...@mgvmedia.com> on 2013/02/13 16:46:48 UTC

How to null-check manually converted TextField values?

I have a TextField which overrides it's getConverter method to add a
Joda time converter instead:

new TextField<P>(id) {
    @Override
    public <P> IConverter<P> getConverter(Class<P> type) {
        return (IConverter<P>) new JodaDateTimeConverter();
    }
};

The converter returns null if input was invalid. However, I want to be
able to flag this field as required, and I don't know how to do that:

 - textField.isRequired(true) does not work, because required checks
are done before conversion. This doesn't work for non-empty but
invalid inputs.

 - textField.add(.. some validator ..) does not work because no
validator is called if the converter returned null.

I really don't see an approach to flag my date fields as required. Do
you know how to do that? Probably my approach is not suited at all?

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


Re: How to null-check manually converted TextField values?

Posted by Sebastian Gaul <se...@mgvmedia.com>.
Thanks Fred, that's what I found yesterday night after hours of
searching, too. However, I think I'll go with Sebastiens approach,
because it unifies converter and validation check.

Thanks to both of you!

2013/2/14 Fred!!! <sc...@googlemail.com>:
> Hi,
>
> an other solution is to add a NullAcceptingValidator to your Textfield. Thus
> wicket will pass to IValidator.validate(IValidatable)
>
> See
> http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/validation/INullAcceptingValidator.html
>
> Cheers Fred
>
> Am 14.02.2013 00:55, schrieb Sebastien:
>
>> Hi,
>>
>> Well, the required flag ensures that the input is not empty, not that it
>> is
>> of the correct type...
>>
>> If the conversion fails, is it supposed (I guessed) to throw a
>> ConversionException.
>> As it seems to not be the case, I would have overridden convert input as
>> follow (not tested):
>>
>> class MyJodaDateTextField
>> {
>>     protected void convertInput()
>>     {
>>          super.convertInput();
>>
>>          Date value = this.getConvertedInput();
>>
>>          if (value == null)
>>          {
>>              //handles the error message
>>              ValidationError error = new ValidationError();
>>              error.addKey("MyJodaDateTextField.ConversionError");
>> //wicket6
>>              //error.addMessageKey("MyJodaDateTextField.ConversionError");
>> //wicket1.5
>>              error.setVariable("date", value);
>>              this.error(error);
>>           }
>>      }
>> }
>>
>> MyJodaDateTextField.properties will contain:
>> MyJodaDateTextField.ConversionError='${date}' is not a valid Joda datetime
>>
>> Also pay attention to check the type in getConverter
>>
>>      {
>>          if (Date.class.isAssignableFrom(type))
>>          {
>>              return (IConverter<C>)new JodaDateTimeConverter();
>>          }
>>
>>          return super.getConverter(type);
>>      }
>>
>>
>> Hope this helps,
>> Sebastien.
>>
>> On Wed, Feb 13, 2013 at 4:46 PM, Sebastian Gaul
>> <se...@mgvmedia.com>wrote:
>>
>>> I have a TextField which overrides it's getConverter method to add a
>>> Joda time converter instead:
>>>
>>> new TextField<P>(id) {
>>>      @Override
>>>      public <P> IConverter<P> getConverter(Class<P> type) {
>>>          return (IConverter<P>) new JodaDateTimeConverter();
>>>      }
>>> };
>>>
>>> The converter returns null if input was invalid. However, I want to be
>>> able to flag this field as required, and I don't know how to do that:
>>>
>>>   - textField.isRequired(true) does not work, because required checks
>>> are done before conversion. This doesn't work for non-empty but
>>> invalid inputs.
>>>
>>>   - textField.add(.. some validator ..) does not work because no
>>> validator is called if the converter returned null.
>>>
>>> I really don't see an approach to flag my date fields as required. Do
>>> you know how to do that? Probably my approach is not suited at all?
>>>
>>> ---------------------------------------------------------------------
>>> 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: How to null-check manually converted TextField values?

Posted by Fred!!! <sc...@googlemail.com>.
Hi,

an other solution is to add a NullAcceptingValidator to your Textfield. 
Thus wicket will pass to IValidator.validate(IValidatable)

See 
http://ci.apache.org/projects/wicket/apidocs/6.0.x/org/apache/wicket/validation/INullAcceptingValidator.html

Cheers Fred

Am 14.02.2013 00:55, schrieb Sebastien:
> Hi,
>
> Well, the required flag ensures that the input is not empty, not that it is
> of the correct type...
>
> If the conversion fails, is it supposed (I guessed) to throw a
> ConversionException.
> As it seems to not be the case, I would have overridden convert input as
> follow (not tested):
>
> class MyJodaDateTextField
> {
>     protected void convertInput()
>     {
>          super.convertInput();
>
>          Date value = this.getConvertedInput();
>
>          if (value == null)
>          {
>              //handles the error message
>              ValidationError error = new ValidationError();
>              error.addKey("MyJodaDateTextField.ConversionError"); //wicket6
>              //error.addMessageKey("MyJodaDateTextField.ConversionError");
> //wicket1.5
>              error.setVariable("date", value);
>              this.error(error);
>           }
>      }
> }
>
> MyJodaDateTextField.properties will contain:
> MyJodaDateTextField.ConversionError='${date}' is not a valid Joda datetime
>
> Also pay attention to check the type in getConverter
>
>      {
>          if (Date.class.isAssignableFrom(type))
>          {
>              return (IConverter<C>)new JodaDateTimeConverter();
>          }
>
>          return super.getConverter(type);
>      }
>
>
> Hope this helps,
> Sebastien.
>
> On Wed, Feb 13, 2013 at 4:46 PM, Sebastian Gaul <se...@mgvmedia.com>wrote:
>
>> I have a TextField which overrides it's getConverter method to add a
>> Joda time converter instead:
>>
>> new TextField<P>(id) {
>>      @Override
>>      public <P> IConverter<P> getConverter(Class<P> type) {
>>          return (IConverter<P>) new JodaDateTimeConverter();
>>      }
>> };
>>
>> The converter returns null if input was invalid. However, I want to be
>> able to flag this field as required, and I don't know how to do that:
>>
>>   - textField.isRequired(true) does not work, because required checks
>> are done before conversion. This doesn't work for non-empty but
>> invalid inputs.
>>
>>   - textField.add(.. some validator ..) does not work because no
>> validator is called if the converter returned null.
>>
>> I really don't see an approach to flag my date fields as required. Do
>> you know how to do that? Probably my approach is not suited at all?
>>
>> ---------------------------------------------------------------------
>> 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: How to null-check manually converted TextField values?

Posted by Sebastien <se...@gmail.com>.
Hi,

Well, the required flag ensures that the input is not empty, not that it is
of the correct type...

If the conversion fails, is it supposed (I guessed) to throw a
ConversionException.
As it seems to not be the case, I would have overridden convert input as
follow (not tested):

class MyJodaDateTextField
{
   protected void convertInput()
   {
        super.convertInput();

        Date value = this.getConvertedInput();

        if (value == null)
        {
            //handles the error message
            ValidationError error = new ValidationError();
            error.addKey("MyJodaDateTextField.ConversionError"); //wicket6
            //error.addMessageKey("MyJodaDateTextField.ConversionError");
//wicket1.5
            error.setVariable("date", value);
            this.error(error);
         }
    }
}

MyJodaDateTextField.properties will contain:
MyJodaDateTextField.ConversionError='${date}' is not a valid Joda datetime

Also pay attention to check the type in getConverter

    {
        if (Date.class.isAssignableFrom(type))
        {
            return (IConverter<C>)new JodaDateTimeConverter();
        }

        return super.getConverter(type);
    }


Hope this helps,
Sebastien.

On Wed, Feb 13, 2013 at 4:46 PM, Sebastian Gaul <se...@mgvmedia.com>wrote:

> I have a TextField which overrides it's getConverter method to add a
> Joda time converter instead:
>
> new TextField<P>(id) {
>     @Override
>     public <P> IConverter<P> getConverter(Class<P> type) {
>         return (IConverter<P>) new JodaDateTimeConverter();
>     }
> };
>
> The converter returns null if input was invalid. However, I want to be
> able to flag this field as required, and I don't know how to do that:
>
>  - textField.isRequired(true) does not work, because required checks
> are done before conversion. This doesn't work for non-empty but
> invalid inputs.
>
>  - textField.add(.. some validator ..) does not work because no
> validator is called if the converter returned null.
>
> I really don't see an approach to flag my date fields as required. Do
> you know how to do that? Probably my approach is not suited at all?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>