BindingResult not working in Spring 3.2.8 using JSR-303 BEAN VALIDATION

Nunyet de Can Calçada

I am working in Validation, Data Binding, and Type Conversion in Spring 3.2.8...

I have this controller

@Controller
public class SaveAccountController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/accounts/saveaccount.do", method = RequestMethod.POST)
private String saveAccount  (   @Valid @ModelAttribute("dataAccountCommand") final DataAccountCommand dataAccountCommand, 
                                BindingResult result) {

        return "registerAccountView";

    }               
}

this java object:

public class DataAccountCommand {

    @Valid  
    User userBean;

    public User getUserBean() {
        return userBean;
    }

    public void setUserBean(User userBean) {
        this.userBean = userBean;
    }

    @Override
    public String toString() {
        return "DataAccountCommand [userBean=" + userBean + "]";
    }       
}

and this other one:

  @SuppressWarnings("serial")
    @Entity
    @Table(name = "T_USER", uniqueConstraints = { @UniqueConstraint(columnNames = "LOGIN"), @UniqueConstraint(columnNames = "EMAIL") })
    @SequenceGenerator(name = "seqUSER", sequenceName = "SEQ_USER")
    public class User implements java.io.Serializable {

        @Id
        @Column(name = "ID", unique = true, nullable = false, precision = 38, scale = 0)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seqUSER")
        private Long id;

        @Column(name = "ROLE", nullable = false, precision = 38, scale = 0)
        @Enumerated(EnumType.ORDINAL)
        protected UserRole userRole;

        @ManyToOne(fetch = FetchType.EAGER) 
        @JoinColumn(name = "COMPANY")
        private Company company;

        @Column(name = "POSITION")
        @NotNull
        @Size(min=10)
        private String position;
    ....
}

and this in the applicationContext.xml

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

and here my servlet:

<!-- Maps incoming URLs to classnames -->

<bean name="controllerHandler" class="springext.web.servlet.mvc.support.ControllerClassNameHandlerMapping">        
    <property name="basePackage" value="fr.telecom.controller" />
    <property name="interceptors">
        <list>
            <!-- Checks a specific request parameter if the locale is changed. -->
            <ref bean="localeChangeInterceptor"/>
            <ref bean="sessionInterceptor"/>
        </list>
    </property>
</bean>

<!-- Action URL mappings to controllers. -->
<bean id="handlerMapping"
    class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
    <property name="interceptors">
        <list>
            <!-- Checks a specific request parameter if the locale is changed. -->
            <ref bean="localeChangeInterceptor"/>
            <ref bean="sessionInterceptor"/>
        </list>
    </property>
</bean>

in the controller position is empty, but result.hasErrors() is false !!!!!

I've tried also using another approach:

@Controller
public class SaveAccountController {

    @Autowired
    private UserService userService;

    @Autowired
    LocalValidatorFactoryBean validator;


    @RequestMapping(value = "/accounts/saveaccount.do", method = RequestMethod.POST)
    private String saveAccount  (   @Valid @ModelAttribute("dataAccountCommand") final DataAccountCommand dataAccountCommand, 
                                    Errors errors) {


        validator.validate(dataAccountCommand, errors);
}

}

But then I got another strange error:

java.lang.AbstractMethodError: org.hibernate.ejb.HibernatePersistence.getProviderUtil()Ljavax/persistence/spi/ProviderUtil; 
Nikolay Rusev

I didn't see where you validate your @ModelAttribute("dataAccountCommand"). Add your validation logic somewhere.

Please see their reference documentation. You have two options:

The first one is with custom validator:

@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result) {

    new PetValidator().validate(pet, result);
    if (result.hasErrors()) {
        return "petForm";
    }

    // ...

}

the second one is with @Valid Annotation:

@RequestMapping(value="/owners/{ownerId}/pets/{petId}/edit", method = RequestMethod.POST)
public String processSubmit(@Valid @ModelAttribute("pet") Pet pet, BindingResult result) {

    if (result.hasErrors()) {
        return "petForm";
    }

    // ...
}

Leave only modelAttribute and BindingResult as input arguments for your method. EDIT:

   @RequestMapping(value = "/accounts/saveaccount.do", method = RequestMethod.POST)
    private String saveAccount  (   @Valid @ModelAttribute("dataAccountCommand") final DataAccountCommand dataAccountCommand, 
                                    BindingResult result) {

        return "registerAccountView";

    }  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring JSR-303 bean validation not working, empty bindingresult

From Dev

Spring + JSF2 + JSR 303 bean validation

From Dev

bean validation (JSR 303) for primefaces not working

From Dev

Can JSR 303 Bean Validation be used with Spring Data Rest?

From Dev

kotlin data class + bean validation jsr 303

From Dev

JSR-303 Bean Validation Collection Values

From Dev

Nested validation groups, Spring, JSR 303

From Dev

Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

From Dev

Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

From Dev

JSR 303 bean validation unit testing with Mockito and Autowiring

From Dev

Wildfly: ExceptionMapper not triggered with RestEasy JSR-303 Bean Validation

From Dev

Exception while binidng form to bean using Spring MVC, JSR 303 Annotation

From Dev

Inline validation using JSR-303 in GWT, part 2: A good implementation?

From Dev

Spring MVC: use different JSR-303 validators on the same bean?

From Dev

Spring MVC: use different JSR-303 validators on the same bean?

From Java

bean validation not working with kotlin (JSR 380)

From Dev

bean validation not working with kotlin (JSR 380)

From Dev

Spring mvc controller jsr 303 basic list validation

From Dev

Spring mvc controller jsr 303 basic list validation

From Dev

spring boot jsr 303 validation with nested array path fails

From Dev

How to break coupling between bean and validator with Bean Validation (JSR303)?

From Dev

JSR-303 / Spring MVC - validate conditionally using groups

From Dev

Validating a List of beans using Bean Validation 2.0 (JSR-308) and Spring 5

From Dev

Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?

From Dev

Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?

From Dev

BindingResult isn't detecting JSR:303 annotation based errors

From Dev

BindingResult isn't detecting JSR:303 annotation based errors

From Dev

Spring webflux bean validation not working

From Dev

JSR - 349 bean validation for Spring @RestController with Spring Boot

Related Related

  1. 1

    Spring JSR-303 bean validation not working, empty bindingresult

  2. 2

    Spring + JSF2 + JSR 303 bean validation

  3. 3

    bean validation (JSR 303) for primefaces not working

  4. 4

    Can JSR 303 Bean Validation be used with Spring Data Rest?

  5. 5

    kotlin data class + bean validation jsr 303

  6. 6

    JSR-303 Bean Validation Collection Values

  7. 7

    Nested validation groups, Spring, JSR 303

  8. 8

    Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

  9. 9

    Implementing custom validation logic for a spring boot endpoint using a combination of JSR-303 and Spring's Validator

  10. 10

    JSR 303 bean validation unit testing with Mockito and Autowiring

  11. 11

    Wildfly: ExceptionMapper not triggered with RestEasy JSR-303 Bean Validation

  12. 12

    Exception while binidng form to bean using Spring MVC, JSR 303 Annotation

  13. 13

    Inline validation using JSR-303 in GWT, part 2: A good implementation?

  14. 14

    Spring MVC: use different JSR-303 validators on the same bean?

  15. 15

    Spring MVC: use different JSR-303 validators on the same bean?

  16. 16

    bean validation not working with kotlin (JSR 380)

  17. 17

    bean validation not working with kotlin (JSR 380)

  18. 18

    Spring mvc controller jsr 303 basic list validation

  19. 19

    Spring mvc controller jsr 303 basic list validation

  20. 20

    spring boot jsr 303 validation with nested array path fails

  21. 21

    How to break coupling between bean and validator with Bean Validation (JSR303)?

  22. 22

    JSR-303 / Spring MVC - validate conditionally using groups

  23. 23

    Validating a List of beans using Bean Validation 2.0 (JSR-308) and Spring 5

  24. 24

    Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?

  25. 25

    Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?

  26. 26

    BindingResult isn't detecting JSR:303 annotation based errors

  27. 27

    BindingResult isn't detecting JSR:303 annotation based errors

  28. 28

    Spring webflux bean validation not working

  29. 29

    JSR - 349 bean validation for Spring @RestController with Spring Boot

HotTag

Archive