validation gwt java - validacion de datos en gwt

VALIDATION.txt


validation gwt java

 ort com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FormErrorOrientation;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.validator.CustomValidator;
import com.smartgwt.client.widgets.form.validator.IsIntegerValidator;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.LayoutSpacer;

public class TestTextError extends Window{

    private DynamicForm form;
  
    private TextItem textItem1;
  
    private TextItem textItem2;

    private TextItem textItem3;

    private TextItem textItem4;
  
    private IButton saveButton;
  
    private IButton cancelButton;
  
    private IButton clearButton;
  
    public TestTextError() {
      
        createComponents();
        doLayout();
        defineValidators();
        defineCallbacks();
    }
  
    private void createComponents() {
        form = new DynamicForm();      
        form.setShowErrorText(true);
        form.setErrorOrientation(FormErrorOrientation.RIGHT);
        form.setRequiredTitleSuffix("*");

        textItem1 = new TextItem("textItem1");
        textItem1.setTitle("TextBox 1");
        textItem1.setWrapTitle(false);
        textItem1.setRequired(true);      
      
        textItem2 = new TextItem("textItem2"); 
        textItem2.setTitle("Widthout Spaces");
        textItem2.setWrapTitle(false);
        textItem2.setRequired(true);
      
        textItem3 = new TextItem("textItem3");
        textItem3.setTitle("Not only spaces");
        textItem3.setWrapTitle(false);
        textItem3.setRequired(true);
              
        textItem4 = new TextItem("textItem4");
        textItem4.setTitle("Only integer");
        textItem4.setWrapTitle(false);
        textItem4.setRequired(true);
      
        saveButton = new IButton("Salve");

        cancelButton = new IButton("Cancel");
      
        clearButton = new IButton("Clear");
              
    }
  
    private void doLayout() {
        setTitle("Test ErrorText");
        setAutoCenter(true);
        setIsModal(true);
        setCanDragResize(false);
        setAutoSize(true);
        setWidth(670);
        setAutoHeight();
        setShowResizer(true);
      
        form.setWidth100();
        form.setPadding(10);
      
        form.setNumCols(2);
        form.setColWidths(1, "*");
        form.setCellBorder(1);

        textItem1.setWidth(175);
        textItem2.setWidth(175);
        textItem3.setWidth(175);
        textItem4.setWidth(175);
      
        textItem1.setErrorMessageWidth(0);
        textItem2.setErrorMessageWidth(0);
        textItem3.setErrorMessageWidth(0);
        textItem4.setErrorMessageWidth(0);
      
        form.setFields(textItem1, textItem2, textItem3, textItem4);
      
        HLayout buttonsLayout = new HLayout();
        buttonsLayout.setWidth100();
        buttonsLayout.setAutoHeight();
        buttonsLayout.setDefaultLayoutAlign(Alignment.CENTER);
        buttonsLayout.setMargin(10);
        buttonsLayout.setMembersMargin(10);
      
        buttonsLayout.addMember(new LayoutSpacer());
        buttonsLayout.addMember(saveButton);
        buttonsLayout.addMember(cancelButton);
        buttonsLayout.addMember(clearButton);
        buttonsLayout.addMember(new LayoutSpacer());

        Label info = new Label("Look the space available beside the field. It's enought to not wrapp the errorText. Click Save to validate.");
        info.setWidth100();
        info.setAutoHeight();
        info.setMargin(10);
      
      
        addItem(form);
        addItem(info);
        addItem(buttonsLayout);
    }
  
    private void defineValidators() {
//        textItem1.setValidateOnChange(true);
//        textItem2.setValidateOnChange(true);
//        textItem3.setValidateOnChange(true);
//        textItem4.setValidateOnChange(true);
      
        CustomValidator notOnlySpaceValidator = new CustomValidator() {
            @Override
            protected boolean condition(Object value) {
                if(value==null) {
                    return true;
                }
                if(!(value instanceof String)) {
                    return false;
                }
                return ((String)value).trim().length()>0;
            }
        };
        notOnlySpaceValidator.setErrorMessage("This field can't have only spaces.");      
      
        CustomValidator noSpaceValidator = new CustomValidator() {
            @Override
            protected boolean condition(Object value) {
                if(value==null) {
                    return true;
                }
                if(!(value instanceof String)) {
                    return false;
                }
                return !((String)value).contains(" ") && !((String)value).contains("\t");
            }
        };
        noSpaceValidator.setErrorMessage("This field can't have spaces.");

        textItem2.setValidators(noSpaceValidator); textItem2.setValue("Text with spaces");
        textItem3.setValidators(notOnlySpaceValidator); textItem3.setValue("   ");
        textItem4.setValidators(new IsIntegerValidator()); textItem4.setValue("Not number text");
    }
  
    private void defineCallbacks() {
        saveButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
                if(form.validate()) {
                    SC.say("ok");
                }
            }
        });

        cancelButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
                TestTextError.this.destroy(); //TODO is it the best option?
            }});
      
        clearButton.addClickHandler(new ClickHandler(){
            public void onClick(ClickEvent event) {
                form.clearValues();
            }});
    }
}


validacion de datos en gwt

Comments

Popular posts from this blog

how to access the DOM gwt smartclient

smart gwt allow only numbers in textfield