0

I have a system that I want to migrate to vaadin 8. I wanted to request for assistance on how to validate a TextField based on regular expressions. I have code that works for Vaadin 7 but not Vaadin 8 which could further explain what I am asking. A code sample like below would be highly appreciated.

TextField txtFirstname = new TextField(); 
txtFirstname.setInputPrompt("Enter the firstname e.g. John or Jane");
txtFirstname.addValidator(new RegexpValidator("^[a-zA-Z _\\-'\" ]{3,25}$", "Invalid FirstName"));
txtFirstname.addBlurListener(new BlurListener() {           
    @Override 
    public void blur(BlurEvent event) { 
        //txtFirstname.validate(); 
        if(txtFirstname.isValid()==false) 
        { 
            txtFirstname.setValidationVisible(true); 
            //txtFirstname.focus(); 
        } 
        else if(txtFirstname.isValid()==true)
            { 
                    txtFirstname.setValidationVisible(false); 
            } 
        } 
    }); 
rjdkolb
  • 10,377
  • 11
  • 69
  • 89
  • You may give details, do you have errors ? warnings ? other ? – azro Aug 25 '17 at 07:53
  • The above code works perfectly in Vaadin 7 but cannot work in Vaadin 8. The isValid() method does not exist in Vaadin 8.I basically wanted to know how to validate a textfield using regular expressions in Vaadin 8(I only know how to do it for Vaadin 7 as I indicated above) Thank you. – Allan-The-Great Aug 25 '17 at 08:01
  • Did you check out the [documentation](https://vaadin.com/docs/-/part/framework/components/components-fields.html#validating-field-values)? It should be sufficient to get started. If you have specific questions to that, we can surely help you. – Steffen Harbich Aug 25 '17 at 12:05

1 Answers1

0

I've work a bit on Vaadin, but I did not look around 'Validator' so I would propose an easier way :

TextField txtFirstname = new TextField(); 
txtFirstname.setInputPrompt("Enter the firstname e.g. John or Jane");
String pattern = "^[a-zA-Z _\\-'\" ]{3,25}$";

txtFirstname.addBlurListener(new BlurListener() {           
    @Override 
    public void blur(BlurEvent event) { 

        if(!txtFirstname.getValue().match(pattern)){  //<- basic string check
            txtFirstname.setValidationVisible(true); 
            //txtFirstname.focus(); 
        } 
        else if(txtFirstname.isValid()==true){ 
             txtFirstname.setValidationVisible(false); 
        } 
   } 
}); 

And use !booleanValue rather than booleanValue==false

azro
  • 53,056
  • 7
  • 34
  • 70
  • Never thought of that thanks. The .setValidationVisible and isValid() methods were depracated in Vaadin 8 but your code (matching the input to the regular expression) should work – Allan-The-Great Aug 25 '17 at 10:23
  • 2
    This is not the way Vaadin has intended for validation of form fields. The preferred way is to use a `Binder` to bind the field to its data. With the `Binder` you can add validators/converters as you did with Vaadin 7. See [docs](https://vaadin.com/docs/-/part/framework/components/components-fields.html#validating-field-values) – Steffen Harbich Aug 25 '17 at 12:03