Hibernate Validator @CPF that can be null

Asked

Viewed 3,134 times

3

I’m using the Hibernate Validator with the annotation @CPF. It is validating beauty, the problem is that my system can have the CPF field without value (null) and Hibernate does not accept it. I put the field with @Collumn (nullable = true) but it has no effect on Hibernate.

  • Probably not because of version, but what version of hibernate-validator are you using? The CPFValidator returns true when the value is null, not empty, then it is strange not to be validating when it is null. Include your code and error (stacktrace) to facilitate in helping.

  • I was able to fix it. The problem that JSF was passing an empty string and not a null value. Then I put it on the web.xml: <context-param> <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name> <param-value>true</param-value> </context-param>

  • Cool guy, include an answer saying that the problem was how the value came to you, ie empty and not null, can help other people ;)

2 answers

1

The problem was that JSF was passing an empty string and not a value null. So I put it on the web.xml:

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-n‌​ame>
    <param-value>true</param-value>
</context-param>

0

I decided as follows:

@CPF
    @Column(nullable = true)
    private String cpf;

public void setCpf(String cpf) {
        if(cpf != null && cpf.length() == 0) {
            this.cpf = null;
        }
        else {
            this.cpf = cpf;
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.