Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9136143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:54:52+00:00 2026-06-17T08:54:52+00:00

When I try to do a bind in my class, it throws an exception,

  • 0

When I try to do a “bind” in my class, it throws an exception, but how can I show an error on the form?

Controller:

@InitBinder
public final void binder(WebDataBinder binder) {        
    binder.registerCustomEditor(Telefone.class, new PropertyEditorSupport(){

        @Override
        public void setAsText(String value){
            if(null != value){
                try{
                    setValue(Telefone.fromString(value));
                } catch (IllegalArgumentException e) {
                    // what to do here ??
                }
            }
        }

    });

Phone:

public static Telefone fromString(String s) {
    checkNotNull(s);
    String digits = s.replaceAll("\\D", "");
    checkArgument(digits.matches("1\\d{2}|1\\d{4}|0300\\d{8}|0800\\d{7,8}|\\d{8,13}"));
    return new Telefone(digits);
}

chekArgument is from Google Preconditions

When phone is not valid, an IllegalArgumentException is thrown .. but how to put it in a BindingResult

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:54:53+00:00Added an answer on June 17, 2026 at 8:54 am

    I am assuming you are using Java 5 so you can’t use @Valid (no JSR303). If that is the case than the only option is to use BindingResult.

    Here is what you can do:

    @Controller
    public class MyController {
    
        @RequestMapping(method = RequestMethod.POST, value = "myPage.html")
        public void myHandler(MyForm myForm, BindingResult result, Model model) {
            result.reject("field1", "error message 1");
        }
    }
    

    My jsp:

    <form:form commandName="myForm" method="post">
    <label>Field 1 : </label>
    <form:input path="field1" />
    <form:errors path="field1" />
    
    <input type="submit" value="Post" />
    </form:form>
    

    To associate error with specific form, you can use:

    result.rejectValue("field1", "messageCode", "Default error message");
    

    Also, BindingResult.reject() associates an error message with the form as a whole. So choose which one works for you. Hope that helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am try to bind the dropdowmlist using jquery. But is showing some error.
Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
I'm trying to bind a function with a non-existent class. I will try to
I try to bind a bean to a form snippet from the form: [#ftl/]
I try to bind an IPv6 server socket in Java 1.6 on Windows 7,
try: html = urlopen('http://glbse.com/api/asset/' + asset.name) except: print 'error while updating the price of
try: recursive_function() except RuntimeError e: # is this a max. recursion depth exceeded exception?
I have the following class Person: public class Person { public string Name {
I defined a local Service: public class ComService extends Service implements IComService { private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.