Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [databinding] custom validators(attach more than one validator)
[databinding] custom validators [message #648815] Fri, 14 January 2011 10:58 Go to next message
Rose-Red is currently offline 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 #649032 is a reply to message #648815] Mon, 17 January 2011 05:58 Go to previous messageGo to next message
Rose-Red is currently offline Rose-Red
Messages: 3
Registered: January 2011
Junior Member
Can somebody please help me with this?
Re: [databinding] custom validators [message #649194 is a reply to message #648815] Tue, 18 January 2011 01:59 Go to previous messageGo to next message
Daniel Krügler is currently offline 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
Re: [databinding] custom validators [message #649307 is a reply to message #649194] Tue, 18 January 2011 12:00 Go to previous message
Rose-Red is currently offline Rose-Red
Messages: 3
Registered: January 2011
Junior Member
Thanks for the reply.
I used one validator after all and it does it's job in a similar fashion that you explained. I just wish there were more examples out there related to databinding, would have saved time Very Happy
Previous Topic:No selection in TableViewer on right mouse-click
Next Topic:subclassing AbstractInformationControlManager
Goto Forum:
  


Current Time: Mon Apr 21 05:41:46 EDT 2014

Powered by FUDForum. Page generated in 0.02457 seconds