How to display form validation constraints message on GWT Material inputs using Editor and Validation Framework from Presenter's class? -
i use:
after constraints validation display error messages. supported gwt material inputs, materialtextbox
using method: materialtextbox.seterror("please provide name");
the problem can executed view class:
public class loginview extends viewwithuihandlers<loginuihandlers> implements loginpresenter.myview { interface binder extends uibinder<widget, loginview> {} /** driver link proxy bean view. */ public interface editordriver extends simplebeaneditordriver<loginmodel, loginview> { } @uifield materialtextbox email; @uifield materialtextbox password; @uifield materialbutton loginbutton; @uifield materialcheckbox keepmeloggedincheckbox; @inject loginview(binder uibinder) { initwidget(uibinder.createandbindui(this)); addclickhandlertologinbutton(); } //@uihandler("loginbutton") private void onloginbuttonclick(clickevent e){ getuihandlers().onloginbuttonclick(); } private void addclickhandlertologinbutton() { loginbutton.addclickhandler(new clickhandler() { @override public void onclick(clickevent event) { onloginbuttonclick(event); } }); } @override public simplebeaneditordriver<loginmodel, ?> createeditordriver() { editordriver driver = gwt.create(editordriver.class); driver.initialize(this); return driver; } public void test() {} }
but editor/validation action in presenter
, , not have there view connection use:
public class loginpresenter extends presenter<loginpresenter.myview, loginpresenter.myproxy> implements loginuihandlers { public interface myview extends beaneditview<loginmodel>, hasuihandlers<loginuihandlers> {} public static final type<revealcontenthandler<?>> slot_login = new type<revealcontenthandler<?>>(); @proxystandard @nametoken(nametokens.login) public interface myproxy extends proxyplace<loginpresenter> {} // editor private simplebeaneditordriver<loginmodel, ?> editordriver; private static final loginservice service = gwt.create(loginservice.class); private loginmodel model = new loginmodel("",""); @override public void onloginbuttonclick() { if (editordriver.isdirty()) { model = editordriver.flush(); validatemodel(); if (editordriver.haserrors()) { materialtoast.firetoast("errors occur"); stringbuilder errorbuilder = new stringbuilder(); (editorerror error : editordriver.geterrors()) { errorbuilder.append(error.getmessage() + "\n"); } materialtoast.firetoast(errorbuilder.tostring()); } else { service.login( model, new methodcallback<integer>() { @override public void onsuccess(method method, integer response) { materialtoast.firetoast("succefully set info. status code: " + response); } @override public void onfailure(method method, throwable exception) { materialtoast.firetoast("error setting"); } }); } } else { materialtoast.firetoast("data has not changed"); } } private void validatemodel() { validator validator = validation.builddefaultvalidatorfactory().getvalidator(); set<constraintviolation<loginmodel>> violations = validator.validate(model); if (violations.size() > 0) { editordriver.setconstraintviolations(new arraylist<constraintviolation<?>>(violations)); } } @inject loginpresenter(eventbus eventbus,myview view, myproxy proxy) { super(eventbus, view, proxy, revealtype.root); getview().setuihandlers(this); editordriver = getview().createeditordriver(); editordriver.edit(model); } }
i think should add interface declare methods access inputs in view. , implementation of that. don't know how. please me.
i've make working. not quite sure did happened start work. share working code:
presenter
package pl.korbeldaniel.cms.client.login; import java.util.arraylist; import java.util.set; import javax.validation.constraintviolation; import javax.validation.validation; import javax.validation.validator; import org.fusesource.restygwt.client.method; import org.fusesource.restygwt.client.methodcallback; import gwt.material.design.client.ui.materialtoast; import com.google.gwt.core.shared.gwt; import com.google.gwt.editor.client.simplebeaneditordriver; import com.google.gwt.event.shared.gwtevent.type; import com.google.inject.inject; import com.google.web.bindery.event.shared.eventbus; import com.gwtplatform.mvp.client.presenter; import com.gwtplatform.mvp.client.annotations.proxystandard; import com.gwtplatform.mvp.client.proxy.proxyplace; import com.gwtplatform.mvp.client.annotations.nametoken; import com.gwtplatform.mvp.client.proxy.revealcontenthandler; import com.gwtplatform.mvp.client.hasuihandlers; import pl.korbeldaniel.cms.client.editor.beaneditview; import pl.korbeldaniel.cms.client.model.loginmodel; import pl.korbeldaniel.cms.client.place.nametokens; import pl.korbeldaniel.cms.client.service.loginservice; public class loginpresenter extends presenter<loginpresenter.myview, loginpresenter.myproxy> implements loginuihandlers { @proxystandard @nametoken(nametokens.login) public interface myproxy extends proxyplace<loginpresenter> {} public interface myview extends beaneditview<loginmodel>, hasuihandlers<loginuihandlers> {} public static final type<revealcontenthandler<?>> slot_login = new type<revealcontenthandler<?>>(); // editor private simplebeaneditordriver<loginmodel, ?> editordriver; private static final loginservice service = gwt.create(loginservice.class); private loginmodel model = new loginmodel(); @override public void onloginbuttonclick() { if (editordriver.isdirty()) { model = editordriver.flush(); validatemodel(); if (editordriver.haserrors()) { materialtoast.firetoast("errors occur"); } else { service.login( model, new methodcallback<integer>() { @override public void onsuccess(method method, integer response) { materialtoast.firetoast("succefully set info. status code: " + response); } @override public void onfailure(method method, throwable exception) { materialtoast.firetoast("error setting"); } }); } } else { materialtoast.firetoast("data has not changed"); } } private void validatemodel() { validator validator = validation.builddefaultvalidatorfactory().getvalidator(); set<constraintviolation<loginmodel>> violations = validator.validate(model); gwt.log(string.valueof(violations.size())); if (violations.size() > 0) { editordriver.setconstraintviolations(new arraylist<constraintviolation<?>>(violations)); } model.validate(); } @inject loginpresenter(eventbus eventbus,myview view, myproxy proxy) { super(eventbus, view, proxy, revealtype.root); getview().setuihandlers(this); editordriver = getview().createeditordriver(); editordriver.edit(model); } public enum editormode { view, edit, create }; }
ui handler
package pl.korbeldaniel.cms.client.login; import com.gwtplatform.mvp.client.uihandlers; interface loginuihandlers extends uihandlers { void onloginbuttonclick(); }
validatorfactory
package pl.korbeldaniel.cms.client.login; import javax.validation.validator; import pl.korbeldaniel.cms.client.model.loginmodel; import com.google.gwt.core.client.gwt; import com.google.gwt.validation.client.abstractgwtvalidatorfactory; import com.google.gwt.validation.client.gwtvalidation; import com.google.gwt.validation.client.impl.abstractgwtvalidator; public final class samplevalidatorfactory extends abstractgwtvalidatorfactory { /** * validator marker validation sample project. classes , * groups listed in {@link gwtvalidation} annotation can validated. */ @gwtvalidation(loginmodel.class) public interface gwtvalidator extends validator { } @override public abstractgwtvalidator createvalidator() { return gwt.create(gwtvalidator.class); } }
view
package pl.korbeldaniel.cms.client.login; import gwt.material.design.client.ui.materialbutton; import gwt.material.design.client.ui.materialcheckbox; import gwt.material.design.client.ui.materialtextbox; import javax.inject.inject; import pl.korbeldaniel.cms.client.model.loginmodel; import com.google.gwt.core.client.gwt; import com.google.gwt.editor.client.simplebeaneditordriver; import com.google.gwt.event.dom.client.clickevent; import com.google.gwt.event.dom.client.clickhandler; import com.google.gwt.uibinder.client.uibinder; import com.google.gwt.uibinder.client.uifield; import com.google.gwt.user.client.ui.widget; import com.gwtplatform.mvp.client.viewwithuihandlers; public class loginview extends viewwithuihandlers<loginuihandlers> implements loginpresenter.myview { interface binder extends uibinder<widget, loginview> {} /** driver link proxy bean view. */ public interface editordriver extends simplebeaneditordriver<loginmodel, loginview> { } @uifield materialtextbox email; @uifield materialtextbox password; @uifield materialbutton loginbutton; @uifield materialcheckbox keepmeloggedincheckbox; @inject loginview(binder uibinder) { initwidget(uibinder.createandbindui(this)); addclickhandlertologinbutton(); } //@uihandler("loginbutton") private void onloginbuttonclick(clickevent e){ getuihandlers().onloginbuttonclick(); } private void addclickhandlertologinbutton() { loginbutton.addclickhandler(new clickhandler() { @override public void onclick(clickevent event) { onloginbuttonclick(event); } }); } @override public simplebeaneditordriver<loginmodel, ?> createeditordriver() { editordriver driver = gwt.create(editordriver.class); driver.initialize(this); return driver; } }
view binder
<!doctype ui:uibinder system "http://dl.google.com/gwt/dtd/xhtml.ent"> <ui:uibinder xmlns:ui="urn:ui:com.google.gwt.uibinder" xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:m="urn:import:gwt.material.design.client.ui" xmlns:e="urn:import:pl.korbeldaniel.cms.client.login"> <m:materialrow ui:field="loginwidget"> <m:materialcolumn grid="s12 m4 l4" offset="l4 m4" > <m:materialtitle title="login" description="please provide account credentials."/> <m:materialpanel padding="5" shadow="1" addstylenames="{style.panel}"> <m:materialpanel addstylenames="{style.fieldpanel}"> <!-- <m:materialimage url="http://b.vimeocdn.com/ps/339/488/3394886_300.jpg" type="circle" addstylenames="{style.imgprofile} z-depth-1"/> --> <m:materialtextbox ui:field="email" type="email" placeholder="email"/> <m:materialtextbox ui:field="password" type="password" placeholder="password"/> <m:materialrow addstylenames="{style.rowaction}"> <m:materialcolumn grid="s12 m12 l6"> <m:materialcheckbox ui:field="keepmeloggedincheckbox" text="keep me logged in"/> </m:materialcolumn> </m:materialrow> <m:materialbutton ui:field="loginbutton" waves="light" text="log in" width="100%"/> </m:materialpanel> </m:materialpanel> </m:materialcolumn> </m:materialrow> </ui:uibinder>
Comments
Post a Comment