Announcement Announcement Module
Collapse
No announcement yet.
validation and json Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • validation and json

    Hi

    how can i return a ModelAndView Object to a json call and still let spring fill the form error tags?
    Code:
    	$("#participant_dialog").dialog({
    			modal: true,
    			buttons: {
    				// define the save category handler
    				"<spring:message code="btn.save"/>": function() {	
    					var obj = $.serializeJSON($("form#participant_form"));		
    					$.getJSON("<c:url value='/participant/create.html' />", obj, function(mav) {
    						if(mav.modelMap.error){
    							var data = mav.modelMap.error;
    							alert(print_r(data));
    							//add error messages in errors span
    							 ?????
    greez noise
    Last edited by noisebreath; Jun 27th, 2011, 02:59 PM.

  • #2
    ive added the errors to the mav object

    Code:
    	@RequestMapping(value = "/create", method = RequestMethod.GET)
    	public @ResponseBody ModelAndView createEvent(@ModelAttribute("participant") @Valid Participant participant, BindingResult result, HttpServletResponse response){
    		ModelAndView mav = new ModelAndView();
    		validator.validate(participant, result);  
            if (result.hasErrors()) { 
            	mav.addObject("errors", result.getAllErrors());
            	mav.setViewName("participant_form");
            	return mav;
    //        	response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    //        	return null;
            } 
    		// calculate password sha-1 hash 
    		participant.getAccount().setPassword(passwordEncoder.encodePassword(participant.getAccount().getPassword(), null));		
    		// save the participant
    		Session session = HibernateUtil.getInstance().getCurrentSession();
    		session.save(participant);
    		mav.addObject(participant);
    		return mav;
    //		return participant;
    	}
    now i can get the errors and distinguish whether everything is oki or not:
    Code:
    $("#participant_dialog").dialog({
    			modal: true,
    			buttons: {
    				// define the save category handler
    				"<spring:message code="btn.save"/>": function() {	
    					var obj = $.serializeJSON($("form#participant_form"));		
    					$.getJSON("<c:url value='/participant/create.html' />", obj, function(mav) {
    						if(mav.modelMap.errors){
    							alert("error");
    						}else{
    							var values = new Array();
    							values[0] = mav.participant.title;
    							values[1] = mav.participant.firstName;
    							values[2] = mav.participant.lastName;
    							values[3] = mav.participant.email;
    							values[4] = mav.participant.phone;
    							var rtn = table.fnAddData(values);
    					        var trAdded = table.fnGetNodes(rtn);
    					        $(trAdded).attr("id", mav.participant.id);
    					        $(trAdded).click(clickRow);			
    						}
    					});				
    					$(this).dialog("close");					
    				},
    				// define the cancel dialog handler
    				"<spring:message code="btn.cancel"/>": function() {
    					$(this).dialog("close");
    				}
    			}		
    		});
    But i still dont now how to fill the error tags of the former form...

    anybody?

    Comment


    • #3
      have simplified the question

      Comment

      Working...
      X