Validation Problem

I have problem. I validate all user input using valuechage event it’s working perfectly. Submit button call another methods, Now my problem if user enter wrong data and press submit button (neglecting errors) those methods should not call and give some error message. Below show my code. Please help me how to solve this problem.


import com.vaadin.Application;
import com.vaadin.data.validator.EmailValidator;
import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;

/** 
 *
 * @author dinesh
 * @version 
 */
public class MyApplication extends Application implements ClickListener {

    TextField txtName;
    TextField txtEmailAdd;
    Button btnRegister;

    @Override
    public void init() {

        Window mainWindow = new Window("MyApplication");

        txtName = new TextField();
        txtName.setImmediate(true);
        txtName.setHeight("-1px");
        txtName.setWidth("-1px");
        txtName.setCaption("Name");
        txtName.setRequired(true);
        txtName.setRequiredError("Please Enter a Name");
        txtName.addValidator(new RegexpValidator("^\\w*$", "Must be letters and numbers"));
        txtName.setTabIndex(1);
        mainWindow.addComponent(txtName);


        txtEmailAdd = new TextField("Email");
        txtEmailAdd.setImmediate(true);
        txtEmailAdd.setHeight("-1px");
        txtEmailAdd.setWidth("250px");
        txtEmailAdd.setTabIndex(2);
        txtEmailAdd.addValidator(new EmailValidator("Enter correct Email"));
        mainWindow.addComponent(txtEmailAdd);

        btnRegister = new Button();
        btnRegister.setImmediate(true);
        btnRegister.setHeight("-1px");
        btnRegister.setWidth("-1px");
        btnRegister.setCaption("Register");
        btnRegister.setTabIndex(25);
        btnRegister.addListener((ClickListener) this);
        mainWindow.addComponent(btnRegister);
        setMainWindow(mainWindow);

    }

    public void buttonClick(ClickEvent event) {
        final Button source = event.getButton();
        if (source == btnRegister) {
            System.out.println("Can't Print");
        }
    }
}


I did it below show that code


public void buttonClick(ClickEvent event) {
        final Button source = event.getButton();
        if (source == btnRegister) {
            if (!txtName.isValid()) {
                System.out.println("Can't Print");
            } else {
                System.out.println("can print");
            }
        }
    }

If this wrong please correct me.

  1. I have another problem, If user press submit button without filling required textfiled. I want to display error mark top of the textbox. How can do it.

if (!txtCustomerNic.isValid()) {
          //    in here  should write validation required code( for the display error mark)
 }


  1. My form contain combo boxed I want display them as required field. In order to do that I used below code

        cmbTitle = new ComboBox();
        cmbTitle.setHeight("-1px");
        cmbTitle.setWidth("60px");
        cmbTitle.setCaption("Title");
        cmbTitle.addItem("Nadeera");
        cmbTitle.setNullSelectionAllowed(false);
        cmbTitle.setInputPrompt("Title");
        cmbTitle.setImmediate(true);
        cmbTitle.setRequired(true);
        cmbTitle.setRequiredError("Please Select a Title");
        mainWindow.addComponent(cmbTitle);

problem is if I load page first time, combo box field diplay error message “Please Select a Title” without changing value or pressing submit button.