Results 1 to 4 of 4

Thread: Validating Ajax Request

  1. #1
    Join Date
    Dec 2007
    Posts
    13

    Default Validating Ajax Request

    Hi,

    I have an application that serves up DTO's to javascript clients (using DWR) and over spring remoting (using http). This works well for the moment but I have having a few issues validating the DTO's. Is it possible to use the spring validation module inconjunction with apache commons validate POJO's that do not extend SimpleFormController i.e. have no http form request lifecycle.

    Thanks,

    TK.

  2. #2
    Join Date
    Dec 2007
    Posts
    13

    Default Re: A little further information

    Hi,

    Just re-reading my initial post and as nobody has replied I thought I would supply a little more information.

    Example DTO:

    @DataTransferObject
    public class DTOEmployeeDetails extends DTOBase implements IDTOEmployeeDetails {

    private String logonId;

    public DTOEmployeeDetails ( ) {
    }

    public String getLogonId() {
    return logonId;
    }


    public void setLogonId(String logonId) {
    this.logonId = logonId;
    }
    }


    At the moment I can implement the spring validator interface and validate these attributes.
    public abstract class DefaultValidator implements org.springframework.validation.Validator {

    public void validateLogonId(String logonId, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "logonId", LOGON_ID_REQUIRED);
    }
    }


    But I would prefer to get the benefit of the commons apache validators for email vaildation etc and to put these validation rules in my validation.xml file is this possible. These DTO's are not managed by spring MVC but are managed by the spring IOC.

    Any opinions appreciated.

  3. #3
    Join Date
    Dec 2007
    Posts
    13

    Default Re: Solution

    Hi,

    I eventually got this working. The solution was to use the spring validation module (which wraps the commons validation stuff). I then can control the behaviour of my validator beans via spring and externalise my validation rules in xml. I also get the to use the available validation routines from the spring validation module. I have included some code below to demonstrate:

    public class DTOValidator implements ApplicationContextAware {

    private DefaultBeanValidator beanValidator = null;

    private ApplicationContext applicationContext = null;


    public HashMap validate(Object obj) {

    Errors errors = new BindException(obj, obj.getClass().getName());

    beanValidator.validate(obj, errors);

    return getMessages(errors);
    }
    }


    This method is called for any save methods using a method inceptor. Based on the field errors we look in the message properties and build hashmap of attributeNames plus error message.


    private HashMap getMessages(Errors errors) {

    List<FieldError> fieldErrrors = errors.getFieldErrors();

    HashMap<String, String> messages = new HashMap();

    String msg = null;

    for (FieldError fieldError: fieldErrrors) {
    String fieldName = fieldError.getField();
    msg = getApplicationContext().getMessage
    (
    errors.getFieldError(fieldName).getDefaultMessage( ), new Object[]
    {
    getMessage((MessageSourceResolvable)errors.getFiel dError(fieldName).getArguments()[0])
    },
    Locale.getDefault()
    );
    messages.put(fieldName, msg);
    }

    return messages;
    }



    VALIDATOR.XML
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE form-validation PUBLIC
    "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN"
    "http://jakarta.apache.org/commons/dtds/validator_1_1.dtd">

    <form-validation>

    <formset>

    <form name="DTOBasicUserDetails">
    <field property="hrId" depends="required">
    <arg0 key="basic.hrId" />
    </field>
    <field property="firstname" depends="required">
    <arg0 key="basic.first.name" />
    </field>
    <field property="lastname" depends="required">
    <arg0 key="basic.last.name" />
    </field>
    <field property="title" depends="required">
    <arg0 key="basic.title" />
    </field>
    </form>

    </formset>

    </form-validation>


    VALIDATOR-RULES.XML (Too Big) - hope this might help some people. As the docs for doing this stuff without any mvc are not very helpfull.

  4. #4

    Default Need help on AJAX+Spring validation framework

    Hi,
    I am new to springs.I need to implement ajax+springs validation framework (using the benefit of the commons apache validators).

    I am able to implement custom validation using ajax.

    but i need to implement AJAX for validation in springs using apache validators.

    can u give me some sample scenario.

Posting Permissions

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