Results 1 to 4 of 4

Thread: How create a unique validation control for SWF and Spring MVC?

  1. #1
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,806

    Question How create a unique validation control for SWF and Spring MVC?

    Dear Members

    I am worrking on a project, well using among many frameworks Spring MVC and Spring Web Flow, powerful sub projects of course

    I did realize the particular situation about the validation object model control for each subproject, for example for Spring MVC

    I used to work with

    Code:
    @Component("entidadvalidator")
    public class EntidadValidator implements Validator{
    
           public boolean supports(Class clazz){
    		return Entidad.class.isAssignableFrom(clazz);
    	}
    
    	public void validate(Object target, Errors errors){
    		
    		Entidad entidad = (Entidad) target;
    		
    		if(entidad.getTipoEntidad().equals(MyConstansUtils.ALUMNO)){
    			for(String property : entidadPropertiesSiAlumno )
    				ValidationUtils.rejectIfEmptyOrWhitespace(errors, property, "required");	
    		}		
    ....
    and for such view jsp I can see the error using

    Code:
    <form:errors path="*" class="error" />
    or

    Code:
    <tr>
    	<td><spring:message code="razonEntidad" text="No deberia aparecer" /></td>
    	<td><form:input path="razonEntidad" size="50" /></td>
    	<td><form:errors path="razonEntidad" class="error" /></td>
    </tr>
    Now For Spring Web Flow I have to use something like

    Code:
    @Component
    public class BookingValidator {
        public void validateEnterBookingDetails(Booking booking, ValidationContext context) {
            MessageContext messages = context.getMessageContext();
            if (booking.getCheckinDate().before(today())) {
                messages.addMessage(new MessageBuilder().error().source("checkinDate").
                    defaultText("Check in date must be a future date").build());
            } else if (!booking.getCheckinDate().before(booking.getCheckoutDate())) {
                messages.addMessage(new MessageBuilder().error().source("checkoutDate").
                    defaultText("Check out date must be later than check in date").build());
            }
        }
    }
    The second bold part seems more verbose

    After to search in the Forum I can see all the error messages

    Code:
    <c:forEach items="${flowRequestContext.messageContext.allMessages}" var="message">
          ${message.text}
    </c:forEach>
    Here arise some questions
    1) Exists a unique way to write only once a validation control and be reused for these two sub projects?

    2)
    If this is for Spring MVC (error for a respective property or field)
    Code:
    <td>
    <form:errors path="razonEntidad" class="error" /></td>
    </tr>
    What is the equivalent for Spring Web Flow?

    Thanks in advanced
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You should be able to use the same form element (form:errors) with spring webflow.

    Next to that you can also use the same Validator for Spring MVC as for Spring WebFlow (a validation method can also have an Errors object instead of a MessageContext or ValidationContext) that way you can use the Errors object to register errors.

    The Spring Web Flow stuff is more powerful because each (error)message can also have a level (error, info, warn) this is based on the validation stuff in JSF.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    Quote Originally Posted by Marten Deinum View Post
    you can also use the same Validator for Spring MVC as for Spring WebFlow (a validation method can also have an Errors object instead of a MessageContext or ValidationContext) that way you can use the Errors object to register errors.
    Correct. To associate a Validator with a specific model, declare the validator as a Spring bean under the name: modelName + "Validator". Or alternatively the model can contain a validate(Errors) method, which internally can delegate to a Spring MVC Validator: validator.validate(this, errros);

    Last but not least both Spring MVC and Spring Web Flow can be configured with a LocalValidatorFactoryBean.

  4. #4
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,806

    Default

    Hello Marten and Rossen

    Thank you

    Marten
    You should be able to use the same form element (form:errors) with spring webflow.
    Has sense, but since SWF do not explicitly explain this, I thought other approach would be neccessary

    Next to that you can also use the same Validator for Spring MVC as for Spring WebFlow (a validation method can also have an Errors object instead of a MessageContext or ValidationContext) that way you can use the Errors object to register errors.
    Now have sense, then it is the key


    Rossen

    Correct. To associate a Validator with a specific model, declare the validator as a Spring bean under the name: modelName + "Validator". Or alternatively the model can contain a validate(Errors) method
    Yes, I prefer the first approach, to avoid mix concerns in the second approach, I already have created my validator for Spring MVC, my confusion was why other approach for SWF

    Code:
    which internally can delegate to a Spring MVC Validator: 
    validator.validate(this, errros);
    I think I need recicle this call to my Action classes for SWF (like the @Controller for MVC)

    Code:
    Last but not least both Spring MVC and Spring Web Flow can be configured with a 
    LocalValidatorFactoryBean
    Yes, but it is related with JSR-303 Bean Validation according with SWF documentation, or could be work for the classic interface Validator?

    I will do later my experiments and then share some code

    Best Regards!
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •