2

I'm working on hibernate-validator and validation-api library, I want to validate a field name with type is Integer, my issue cause when user input with String type. It work with null and empty. If you used to has this problem, could you show me the way to resolve it?

@Entity
@Table(name = "shops")
public class Shop {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    @Column(name = "employees_number")
    private Integer emplNumber;

    // getter and setter

}

@Component
public class ShopValidator implements Validator {

    @Override
    public boolean supports(Class<?> clazz) {
        return Shop.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        Shop shop = (Shop) target;
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "shop.name.empty");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emplNumber", "shop.emplNumber.empty");
        if (!NumberUtils.isNumeric(shop.getEmplNumber().toString())) {
            errors.rejectValue("emplNumber", "shop.emplNumber.invalid");
        }
    }

}

public class NumberUtils {

    public static boolean isNumeric(String isNum) {
        try {
            return isNum.matches("[1-9]+");
        } catch (NullPointerException e) {
            return false;
        }
    }
}

Here is picture I need to validate

1 Answer 1

2

Try @Digits and @NotBlank annotations

@Entity
@Table(name = "shops")
public class Shop {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    @Column(name = "employees_number")
    @NotBlank(message = "{shop.name.empty}")
    @Digits(integer=10, fraction=0, message = "{shop.emplNumber.invalid}")
    private String emplNumber;

    // getter and setter
}

http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-defineconstraints-spec

Sign up to request clarification or add additional context in comments.

4 Comments

Method of you work when changed type and annotated one by one field. But in my current code I used @InitBinder to validate a class. If I change type to String, these code done with current code and no need add more code anymore. The question is do you have any way to validate but still keeping Integer type? Because I want this type to map with type in db.
emplNumber is Integer so it cannot be String and you cannot pass String value from your form. You can change it to String and use @Type annotation. Or you can keep it as Integer but do not allow to enter anything but number values on your page side with some JS or <input type="number"> in HTML5
Maybe change type to String is best way and fit with current code. "JS or <input type="number"> in HTML5" -> I tried this method, but if user disable js or f12 to edit type attribute input then submit, it still cause issue. This is just a simple project, but in case customer assign for us a project and they just want to develop more, they don't want edit things existed. Or a tester they input into form what they want, it continue cause issue. Maybe I'm complicating :). Thanks for you help.
@Digits wouldn't validate against String

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.