validation - How to validate inputs and prevent save actions using databinding in eclipse? -
i want create input forms validate user input , prevent model being saved invalid data. have been using databinding works point implementation not intuitive like.
imagine input contains '123' , value must not empty. user deletes characters 1 one until empty. databinding validator shows error decoration.
however, if user saves form , reloads it, '1' displayed in field - i.e. last valid input. databinding not transmit invalid value model.
i have changelistener called before databinding @ point invalid state has not been detected.
i error displayed in ui model remains valid (this so). also, long ui contains errors, should not possible save model.
/** * bind text control property in view model **/ protected binding bindtext(databindingcontext ctx, control control, object viewmodel, string property, ivalidator validator) { iobservablevalue value = widgetproperties.text(swt.modify).observe( control); iobservablevalue modelvalue = beanproperties.value( viewmodel.getclass(), property).observe(viewmodel); binding binding = ctx.bindvalue(value, modelvalue, getstrategy(validator), null); binding.gettarget().addchangelistener(listener); controldecorationsupport.create(binding, swt.top | swt.left); return binding; } private updatevaluestrategy getstrategy(ivalidator validator) { if (validator == null) return null; updatevaluestrategy strategy = new updatevaluestrategy(); strategy.setbeforesetvalidator(validator); return strategy; } private ichangelistener listener = new ichangelistener() { @override public void handlechange(changeevent event) { // notify form listeners has changed } }; /** * called form owner check if form contains valid data e.g. before saving **/ public boolean isvalid() { system.out.println("isvalid"); (object o : getdatacontext().getvalidationstatusproviders()) { validationstatusprovider vsp = (validationstatusprovider) o; istatus status = (istatus)vsp.getvalidationstatus() .getvalue(); if (status.matches(istatus.error)) return false; } return true; }
your best bet steer clear of changelistener
s - you've discovered, order of execution either undefined or not helpful in case.
instead, want stick 'observable' opposed 'listener' model long possible. mentioned, create aggregatevalidationstatus
listen overall state of databindingcontext
, has similar effect existing code.
then can either listen directly (as below) affect save ability, or bind bean.
iobservablevalue statusvalue = new aggregatevalidationstatus(dbc, aggregatevalidationstatus. max_severity); statusvalue.addlistener(new ivaluechangelistener() { handlevaluechange(valuechangeevent event) { // change ability save here... } });
Comments
Post a Comment