Bean validation for Integer field










2















I am trying to have constraint validation on the field, which must reject if the input is not an integer.



public class ClientTO 
private Integer phone_num; //



I tried:



1) @Digits - It does not validate if input is integer or not as you could see the type mismatch exception is still thrown.



2) My custom validation - which seems to not work on Integer fields



Error:



Exception in thread "main" javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Integer.


My custom validation class:



public class X_CVAImpl implements ConstraintValidator<X_CustomValidatorAnnotation,String> 
public boolean isValid(String value, ConstraintValidatorContext context)
// TODO Auto-generated method stub
boolean val_d;
if (value.matches("[0-9]+") && value.length() ==10)
val_d=true;
else
val_d=false;

return val_d;




Any help please?










share|improve this question



















  • 2





    Seems that you declared a validator for String fields, but you annotated an Integer field with it. Beside that, why didn't you just apply the @Pattern constraint on a string field?

    – ernest_k
    Nov 15 '18 at 14:11











  • I am trying to construct entity on ClientTO to put it into MVC model, I did not want use string field as I wanted the entity properties of proper type so there wouldn't be any issues when put it into MVC model. trying to work on validator for Integer as you suggested.

    – sql_dummy
    Nov 15 '18 at 14:37






  • 2





    Using an integer field simply means that type checking would fail before bean validation if the wrong data is used. If you have to use Integer, then the only constraint you need is perhaps @NotNull, and whatever you use for serialization will take care of making sure that the data type is valid.

    – ernest_k
    Nov 15 '18 at 14:44











  • got it, thanks. Could you put it into answer.

    – sql_dummy
    Nov 15 '18 at 15:40















2















I am trying to have constraint validation on the field, which must reject if the input is not an integer.



public class ClientTO 
private Integer phone_num; //



I tried:



1) @Digits - It does not validate if input is integer or not as you could see the type mismatch exception is still thrown.



2) My custom validation - which seems to not work on Integer fields



Error:



Exception in thread "main" javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Integer.


My custom validation class:



public class X_CVAImpl implements ConstraintValidator<X_CustomValidatorAnnotation,String> 
public boolean isValid(String value, ConstraintValidatorContext context)
// TODO Auto-generated method stub
boolean val_d;
if (value.matches("[0-9]+") && value.length() ==10)
val_d=true;
else
val_d=false;

return val_d;




Any help please?










share|improve this question



















  • 2





    Seems that you declared a validator for String fields, but you annotated an Integer field with it. Beside that, why didn't you just apply the @Pattern constraint on a string field?

    – ernest_k
    Nov 15 '18 at 14:11











  • I am trying to construct entity on ClientTO to put it into MVC model, I did not want use string field as I wanted the entity properties of proper type so there wouldn't be any issues when put it into MVC model. trying to work on validator for Integer as you suggested.

    – sql_dummy
    Nov 15 '18 at 14:37






  • 2





    Using an integer field simply means that type checking would fail before bean validation if the wrong data is used. If you have to use Integer, then the only constraint you need is perhaps @NotNull, and whatever you use for serialization will take care of making sure that the data type is valid.

    – ernest_k
    Nov 15 '18 at 14:44











  • got it, thanks. Could you put it into answer.

    – sql_dummy
    Nov 15 '18 at 15:40













2












2








2








I am trying to have constraint validation on the field, which must reject if the input is not an integer.



public class ClientTO 
private Integer phone_num; //



I tried:



1) @Digits - It does not validate if input is integer or not as you could see the type mismatch exception is still thrown.



2) My custom validation - which seems to not work on Integer fields



Error:



Exception in thread "main" javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Integer.


My custom validation class:



public class X_CVAImpl implements ConstraintValidator<X_CustomValidatorAnnotation,String> 
public boolean isValid(String value, ConstraintValidatorContext context)
// TODO Auto-generated method stub
boolean val_d;
if (value.matches("[0-9]+") && value.length() ==10)
val_d=true;
else
val_d=false;

return val_d;




Any help please?










share|improve this question
















I am trying to have constraint validation on the field, which must reject if the input is not an integer.



public class ClientTO 
private Integer phone_num; //



I tried:



1) @Digits - It does not validate if input is integer or not as you could see the type mismatch exception is still thrown.



2) My custom validation - which seems to not work on Integer fields



Error:



Exception in thread "main" javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.lang.Integer.


My custom validation class:



public class X_CVAImpl implements ConstraintValidator<X_CustomValidatorAnnotation,String> 
public boolean isValid(String value, ConstraintValidatorContext context)
// TODO Auto-generated method stub
boolean val_d;
if (value.matches("[0-9]+") && value.length() ==10)
val_d=true;
else
val_d=false;

return val_d;




Any help please?







java bean-validation hibernate-validator






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:09









Bsquare ℬℬ

3,654101635




3,654101635










asked Nov 15 '18 at 14:09









sql_dummysql_dummy

243213




243213







  • 2





    Seems that you declared a validator for String fields, but you annotated an Integer field with it. Beside that, why didn't you just apply the @Pattern constraint on a string field?

    – ernest_k
    Nov 15 '18 at 14:11











  • I am trying to construct entity on ClientTO to put it into MVC model, I did not want use string field as I wanted the entity properties of proper type so there wouldn't be any issues when put it into MVC model. trying to work on validator for Integer as you suggested.

    – sql_dummy
    Nov 15 '18 at 14:37






  • 2





    Using an integer field simply means that type checking would fail before bean validation if the wrong data is used. If you have to use Integer, then the only constraint you need is perhaps @NotNull, and whatever you use for serialization will take care of making sure that the data type is valid.

    – ernest_k
    Nov 15 '18 at 14:44











  • got it, thanks. Could you put it into answer.

    – sql_dummy
    Nov 15 '18 at 15:40












  • 2





    Seems that you declared a validator for String fields, but you annotated an Integer field with it. Beside that, why didn't you just apply the @Pattern constraint on a string field?

    – ernest_k
    Nov 15 '18 at 14:11











  • I am trying to construct entity on ClientTO to put it into MVC model, I did not want use string field as I wanted the entity properties of proper type so there wouldn't be any issues when put it into MVC model. trying to work on validator for Integer as you suggested.

    – sql_dummy
    Nov 15 '18 at 14:37






  • 2





    Using an integer field simply means that type checking would fail before bean validation if the wrong data is used. If you have to use Integer, then the only constraint you need is perhaps @NotNull, and whatever you use for serialization will take care of making sure that the data type is valid.

    – ernest_k
    Nov 15 '18 at 14:44











  • got it, thanks. Could you put it into answer.

    – sql_dummy
    Nov 15 '18 at 15:40







2




2





Seems that you declared a validator for String fields, but you annotated an Integer field with it. Beside that, why didn't you just apply the @Pattern constraint on a string field?

– ernest_k
Nov 15 '18 at 14:11





Seems that you declared a validator for String fields, but you annotated an Integer field with it. Beside that, why didn't you just apply the @Pattern constraint on a string field?

– ernest_k
Nov 15 '18 at 14:11













I am trying to construct entity on ClientTO to put it into MVC model, I did not want use string field as I wanted the entity properties of proper type so there wouldn't be any issues when put it into MVC model. trying to work on validator for Integer as you suggested.

– sql_dummy
Nov 15 '18 at 14:37





I am trying to construct entity on ClientTO to put it into MVC model, I did not want use string field as I wanted the entity properties of proper type so there wouldn't be any issues when put it into MVC model. trying to work on validator for Integer as you suggested.

– sql_dummy
Nov 15 '18 at 14:37




2




2





Using an integer field simply means that type checking would fail before bean validation if the wrong data is used. If you have to use Integer, then the only constraint you need is perhaps @NotNull, and whatever you use for serialization will take care of making sure that the data type is valid.

– ernest_k
Nov 15 '18 at 14:44





Using an integer field simply means that type checking would fail before bean validation if the wrong data is used. If you have to use Integer, then the only constraint you need is perhaps @NotNull, and whatever you use for serialization will take care of making sure that the data type is valid.

– ernest_k
Nov 15 '18 at 14:44













got it, thanks. Could you put it into answer.

– sql_dummy
Nov 15 '18 at 15:40





got it, thanks. Could you put it into answer.

– sql_dummy
Nov 15 '18 at 15:40












1 Answer
1






active

oldest

votes


















2














@Digits will only work for primitive types.
So if you change your entity to the following, your validation should kick in.



public class ClientTO 
@Digits(integer=10, fraction=0)
private int phone_num; // Constraint: phone_num can only be 10 digits long or less
...



Having that said, I believe that you should use a string to validate a phone number.



public class ClientTO 
@Size(min=10, max=10)
@Pattern(regexp="(^[0-9]10)")
private String phone_num; // Constraint: phone_num would match 10 digits number
...






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321296%2fbean-validation-for-integer-field%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    @Digits will only work for primitive types.
    So if you change your entity to the following, your validation should kick in.



    public class ClientTO 
    @Digits(integer=10, fraction=0)
    private int phone_num; // Constraint: phone_num can only be 10 digits long or less
    ...



    Having that said, I believe that you should use a string to validate a phone number.



    public class ClientTO 
    @Size(min=10, max=10)
    @Pattern(regexp="(^[0-9]10)")
    private String phone_num; // Constraint: phone_num would match 10 digits number
    ...






    share|improve this answer



























      2














      @Digits will only work for primitive types.
      So if you change your entity to the following, your validation should kick in.



      public class ClientTO 
      @Digits(integer=10, fraction=0)
      private int phone_num; // Constraint: phone_num can only be 10 digits long or less
      ...



      Having that said, I believe that you should use a string to validate a phone number.



      public class ClientTO 
      @Size(min=10, max=10)
      @Pattern(regexp="(^[0-9]10)")
      private String phone_num; // Constraint: phone_num would match 10 digits number
      ...






      share|improve this answer

























        2












        2








        2







        @Digits will only work for primitive types.
        So if you change your entity to the following, your validation should kick in.



        public class ClientTO 
        @Digits(integer=10, fraction=0)
        private int phone_num; // Constraint: phone_num can only be 10 digits long or less
        ...



        Having that said, I believe that you should use a string to validate a phone number.



        public class ClientTO 
        @Size(min=10, max=10)
        @Pattern(regexp="(^[0-9]10)")
        private String phone_num; // Constraint: phone_num would match 10 digits number
        ...






        share|improve this answer













        @Digits will only work for primitive types.
        So if you change your entity to the following, your validation should kick in.



        public class ClientTO 
        @Digits(integer=10, fraction=0)
        private int phone_num; // Constraint: phone_num can only be 10 digits long or less
        ...



        Having that said, I believe that you should use a string to validate a phone number.



        public class ClientTO 
        @Size(min=10, max=10)
        @Pattern(regexp="(^[0-9]10)")
        private String phone_num; // Constraint: phone_num would match 10 digits number
        ...







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 15:02









        dirbackedirbacke

        1,1891118




        1,1891118





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53321296%2fbean-validation-for-integer-field%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto