2
votes

on a Validator component I need to evaluate which textfield is on error, I found a Isvalid() method but is no valid for my porpuse, beacuse I don't know which value is on error.

How can I know which constraint and component is no valid?,

The

boolean isValid(Component cmp) 

is no public.

Regards

1

1 Answers

1
votes

I'm not sure if you are using the right Validator since you provided no code. However, below code should help you get started:

Validate using a single validator:

import com.codename1.ui.validation.LengthConstraint;
import com.codename1.ui.validation.RegexConstraint;
import com.codename1.ui.validation.Validator;

.
.
.

Validator val = new Validator();
val.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
val.addConstraint(emailField, RegexConstraint.validEmail())
   .addConstraint(passwordField, new LengthConstraint(6));

if (val.isValid()) {
    System.out.println("All validated fields pass the constraints");
} else {
    // show dialog here
}

Validate using an individual validator:

import com.codename1.ui.validation.LengthConstraint;
import com.codename1.ui.validation.RegexConstraint;
import com.codename1.ui.validation.Validator;

.
.
.

Validator valEmail = new Validator();
valEmail.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
valEmail.addConstraint(emailField, RegexConstraint.validEmail());
if (valEmail.isValid()) {
    System.out.println("Email field passes the constraint");
} else {
    // show dialog here
}

Validator valPassword = new Validator();
valPassword.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
valPassword.addConstraint(passwordField, new LengthConstraint(6));

if (valPassword.isValid()) {
    System.out.println("Password field passes the constraint");
} else {
    // show dialog here
}

In addition to the above, I would recommend using TextComponent as it will show the error message seamlessly below each component, without the need for a dialog.

As a bonus, you could even disable the submit button until all the validated fields are valid:

import com.codename1.ui.validation.LengthConstraint;
import com.codename1.ui.validation.RegexConstraint;
import com.codename1.ui.validation.Validator;
import com.codename1.ui.*;

.
.
.

TextComponent email = new TextComponent().constraint(TextArea.EMAILADDR)
        .labelAndHint("Email").focusAnimation(false);
email.setName("email");

TextComponent password = new TextComponentPassword()
        .labelAndHint("Password").focusAnimation(false);
password.setName("password");

Button submit = new Button("Submit");

Validator val = new Validator();
val.setValidationFailureHighlightMode(Validator.HighlightMode.UIID);
val.addSubmitButtons(submit)
        .addConstraint(email, RegexConstraint.validEmail())
        .addConstraint(password, new LengthConstraint(6));