Home » Eclipse Projects » JFace » [databinding] custom validators(attach more than one validator)
[databinding] custom validators [message #648815] |
Fri, 14 January 2011 15:58 |
Rose-Red Messages: 3 Registered: January 2011 |
Junior Member |
|
|
Hi,
I have two custom validators and been trying to attach them to the same field, but one of them is being ignored.
Basically, the code below works fine. I am observing a text field and both of my custom validators are triggered properly.
bindingContext.bindValue(
SWTObservables.observeText(nameText, SWT.FocusOut),
EMFObservables.observeValue(person, Literals.PERSON__NAME),
new UpdateValueStrategy(UpdateValueStrategy.POLICY_CONVERT).
setAfterConvertValidator(new RegularExpressionValidator("Steve\\d{1,}")).
setAfterGetValidator(new MandatoryFieldValidator()),
null);
Now the following code seems to be ignoring IntegerValidator and shows the validation error that I have set up for the second validator. Here I am observing a number field and have created a custom converter as well which converts empty string into a null if needed or uses a default converter if the field is not empty.
bindingContext.bindValue(
SWTObservables.observeText(ageText, SWT.FocusOut),
EMFObservables.observeValue(person, Literals.PERSON_AGE),
ageTargetStrategy.setAfterGetValidator(new IntegerValidator()).
setAfterGetValidator(new RangedIntegerValidator(10, 50)),
new UpdateValueStrategy().setConverter(NumberToStringConverter.fromInteger(true)));
RangedIntegerValidator is a subclass of the IntegerValidator. If I apply them separately, it works fine. Therefore, I wonder whether it is at all possible to have more than one validator attached to the same field.
I am a bit confused about setAfterGetValidator, setAfterConvertValidator and setBeforeSetVaidator methods. I have been playing around with all of them, but haven't got the satisfying result.
Any help would be appreciated
|
|
| |
Re: [databinding] custom validators [message #649194 is a reply to message #648815] |
Tue, 18 January 2011 06:59 |
Daniel Krügler Messages: 853 Registered: July 2009 |
Senior Member |
|
|
On 2011-01-14 16:58, adrijukas@yahoo.com wrote:
> Hi,
>
> I have two custom validators and been trying to attach them to the same
> field, but one of them is being ignored.
>
> Basically, the code below works fine. I am observing a text field and
> both of my custom validators are triggered properly.
>
>
> bindingContext.bindValue(
> SWTObservables.observeText(nameText, SWT.FocusOut),
> EMFObservables.observeValue(person, Literals.PERSON__NAME),
> new UpdateValueStrategy(UpdateValueStrategy.POLICY_CONVERT).
> setAfterConvertValidator(new RegularExpressionValidator("Steve\\d{1,}")).
> setAfterGetValidator(new MandatoryFieldValidator()), null);
>
>
> Now the following code seems to be ignoring IntegerValidator and shows
> the validation error that I have set up for the second validator. Here I
> am observing a number field and have created a custom converter as well
> which converts empty string into a null if needed or uses a default
> converter if the field is not empty.
>
>
> bindingContext.bindValue(
> SWTObservables.observeText(ageText, SWT.FocusOut),
> EMFObservables.observeValue(person, Literals.PERSON_AGE),
> ageTargetStrategy.setAfterGetValidator(new IntegerValidator()).
> setAfterGetValidator(new RangedIntegerValidator(10, 50)),
> new
> UpdateValueStrategy().setConverter(NumberToStringConverter.f romInteger(true)));
>
> RangedIntegerValidator is a subclass of the IntegerValidator. If I apply
> them separately, it works fine. Therefore, I wonder whether it is at all
> possible to have more than one validator attached to the same field.
>
> I am a bit confused about setAfterGetValidator, setAfterConvertValidator
> and setBeforeSetVaidator methods. I have been playing around with all of
> them, but haven't got the satisfying result.
>
> Any help would be appreciated
setAfterGetValidator does set a single new validator, it does *not* add
one (thus the name), which is clearly documented. If you want to have
more than one validator per target, you should provide a validator that
refers to a collection of validators. Inside it's validate function it
will traverse through all referenced validators until the first status
is reported that is not an OK status.
Btw: This is not the same as MultiValidator. You will use
MultiValidator, if you want to ensure validation among *more* than one
target at the same time.
HTH & Greetings from Bremen,
Daniel Krügler
|
|
| |
Goto Forum:
Current Time: Fri Sep 20 20:21:16 GMT 2024
Powered by FUDForum. Page generated in 0.03227 seconds
|