nishtahir / gwt-pectin

Automatically exported from code.google.com/p/gwt-pectin
0 stars 0 forks source link

FormattedFieldImpl is not doing anything about FormatException thrown by the Format object #24

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
The following applies to pectin 1.6.

Looking at FormattedFieldModelImpl.writeTextToValue(String), the
FormatException thrown by the Format object is caught and is handled by the
FormatExceptionPolicy object.

However, the FormatExceptionPolicy object instantiated in the class, does
nothing ( onFormatException method does nothing).

I'm aware that there's a setter exposed by the FormattedFieldModelImpl but
this setter is not called by anyone. Because we create instances of
FormattedFieldModelImpl through the DSL that hides the actual class that
gets instantantiated, we can't really get to this setter.

As a result, I have no way of hooking in and displaying error message on
value change event. Is there a way for us to create FormattedFieldModel
object with configurable FormatExceptionPolicy at the time of creation? I'm
also open to suggestions as to how I can intercept FormatException on value
change event. Thanks.

Original issue reported on code.google.com by eric.suk...@gmail.com on 28 May 2010 at 12:39

GoogleCodeExporter commented 8 years ago
I think adding the ability to specify the policy on the builder API is 
worthwhile so
I'll put that in.  As an aside the default policy implementation just leaves 
the last
valid value in place so that any fields that may be computed from it remain 
stable
until a new valid value comes along.

As for displaying validation info the validation plugin is the intended way to 
do it.
 The formatted field validation code supports both text and value validators, so you
can add a text validator that will run before any value validators.  This allows
things like a text validator that ensures the text is a valid date, while value
validators ensure the date is in the correct range.  Value validators only run 
if the
text validators don't report errors.  

As for validating on value change, that's something the validation plugin is
currently missing.  Ideally I'd like to add support for validation triggers so 
you
can do things like 

validationField(aField)
  .using(aValidator)
  .trigger(ON_EDIT, ON_BLUR) 

and so forth.  There's some early thoughts at
https://wave.google.com/wave/?nouacheck#restored:wave:googlewave.com!w%252BvIXX8
JY-A
if your interested or would like to comment.  I've used this approach in Swing 
and it
has worked quite well so I'm hoping it will translate across without to much 
trouble.

I'm not sure at what point I'll get around to working on that so in the mean 
time you
might be able do the following, although it sounds like this would be the same
problem you face with setting the exception policy via a DSL (but I'll mention 
it out
of interest).

formattedField.getTextModel().addValueChangeHandler(new 
ValueChangeHandler<String>()
{
   public void onValueChange(ValueChangeEvent<String> stringValueChangeEvent)
   {
      getValidationManager(ValidatedFormModel.this)
         .getValidator(age)
         .validate();
   }
});

The down side of the above is that it will validation on all changes to the 
value,
not just user edits.  So if your domain data ever has invalid values then 
you'll get
validation messages popping up.  To overcome this you could probably set a flag 
while
you're loading from a bean and only call validate when the flag isn't set.

If that's not going to be possible then using the format policy is probably 
your best
option.  You'd need to do the same flag trick as above in your policy if your 
models
ever contain invalid values. 

I'll see if I can get the ability to configure the policy via the builder API 
in 0.8. 

Original comment by andrew.pietsch on 28 May 2010 at 2:34

GoogleCodeExporter commented 8 years ago
The builder now gives the option to specify a format along with an exception 
policy.

formattedFieldOfType(Integer.class)
  .using(integerFormat, exceptionPolicy)
  .boundTo(...);

formattedListOfType(Integer.class)
  .using(integerFormat, exceptionPolicy)
  .boundTo(...);

Fixed in 0.8.

Original comment by andrew.pietsch on 9 Jun 2010 at 2:46