package e4s.tutorial; import e4s.html.*; import e4s.html.input.extended.*; import e4s.servlet.E4ModuleImplementation; import e4s.translate.E4LabelApp; import e4s.translate.E4Label_Intf; /** * A form for entering values using different types of fields. * * {@tutorial Example_FormValidator} */ public class Example_FormValidator extends E4ModuleImplementation { public static E4Method start = null; public static E4Method submitted = null; private final static E4InputFieldName PARAM_NAME_TEST = new E4InputFieldName("TEST"); private final static String SESSION_OBJ_NAME_4_FORM = "TUTORIAL.FORM"; /** * Validator: do not accept A,E,I,O or U characters. */ private static class MyValidator implements AEP_ValidationObject_Intf { public MyValidator() { TRACE(); } public boolean isMandatory() { return false; } public void setFieldset(char mode, E4Fieldset fieldset) { } public E4Label_Intf validate(E4InputFieldName_Intf fieldname, E4Label_Intf label, String value) { final String BADCHARS[] = {"A","E","I","O","U"}; if (value != null) { TRACE(value); for( int i = 0; i < BADCHARS.length; i++ ) { int pos = value.toUpperCase().indexOf(BADCHARS[i]); if (pos >= 0) { TRACE("NOK"); // return an error message (will be translated) return new E4LabelApp("May not contain character # at position #",BADCHARS[i],Integer.toString(pos)); } } } TRACE("OK"); // o.k. return null; } } public void start( HTML html ) { BODY body = html.BODY(); // create a form FORM form = body.FORM(submitted); form.preventFromRemoving(); form.Message(E4Message.CAPTION,"Do not enter A,E,I,O or U"); form.P(); // store the form for possible re-display setSessionObject(SESSION_OBJ_NAME_4_FORM,form); // add a textfield to the form TEXTFIELD fTest = form.TEXTFIELD(PARAM_NAME_TEST,"Your Input Field",50); fTest.setValidator(new MyValidator()); // add a submit button form.P(); form.BUTTON_Submit("Execute"); } public void submitted( HTML html, E4CgiParams params ) throws Exception { BODY body = html.BODY(); FORM form = (FORM)getSessionObject(SESSION_OBJ_NAME_4_FORM,true); if (! form.validate(params,getServlet())) { body.Message(E4Message.ERROR,"There are some errors!"); body.P(); body.addElement(form); } else { String val = params.get(PARAM_NAME_TEST); body.Message(E4Message.INFO,new E4LabelApp("You have entered #. Thank you.",val)); removeSessionObject(SESSION_OBJ_NAME_4_FORM); body.A(start).SMALL().print("start again"); } } }