2012-11-30 1 views
5

Grails 프레임 워크에서 작동하지만 controller.My 응용 프로그램은 "단일 페이지 응용 프로그램"이 아닙니다. 서비스를 작성 했으므로 내 페이지를 다시로드하려고하지 않습니다. 즉 RegistrationService 아약스 전화로. 그래서 데이터 바인딩을위한 녹아웃을 사용하고 있습니다. 데이터베이스를 사용하는 postgresql입니다. 이메일 필드가있는보기 페이지가 있습니다. 중복 이메일 ID를 입력하고 저장 버튼을 클릭하면 유효성 검사 오류 (예 : "이미 이메일을 가져 왔습니다")가 표시 될 수 있지만 고유 ID를 작성하는 경우에도 동일한 유효성 검사 오류가 발생합니다.이 경우 modalModal.js 페이지에서 오류 상태가되기 때문에이 문제를 해결하는 방법을 알지 못합니다. 고유 ID를 입력하고 저장 버튼을 클릭하면 유효성 검사 오류가 표시되지 않습니다.보기 페이지에서 서버 쪽 유효성 검사 오류를 표시하는 방법

_newCostingpage.gsp (my view page) 

    <div id="light" class="white_content" style="color: black;"> 
    <form action="#/cart" method="post"> 
    <input type="hidden" name="url" value="${grailsApplication.config.serverURL}"/> 
    <p><label>first name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input name="firstName" 
      class="formElement" data-bind='value: firstName'/></label></p> 
    <p><label>last name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input name="lastName" 
       class="formElement" data-bind='value: lastName' /></label></p> 
    <p><label>organisation: <input name="organisation" class="formElement" data- 
       bind='value: organisation' /></label></p> 
<p><label>email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <input name="email" class="formElement" data-bind='value: email' /></label><label 
     id="errorDiv"></label></p> 
<p><label>password:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="password" name="password" 
     class="formElement" data-bind='value: password' /></label></p> 
<p><label style="margin-left: -37px;">confirm password:</label> <input 
     type="password" name="confirmPassword" class="formElement" data-bind='value: 
     confirmPassword' /></p> 
<p><input type="submit" value="register"/></p> 
</form> 
<a href="javascript:void(0)"onclick="document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none';clearBox();">Close</a> 
     </div> 
     <div id="fade" class="black_overlay"></div> 
    </div> 

Domain class :-\ 

    class Registration { 
String firstName 
String lastName 
String organization 
String email 
String password 
String confirmPassword 

static constraints = { 
    firstName(nullable:false) 
    lastName(nullable:true) 
    organization(blank:false) 
    email(nullable:false,email:true,unique:true) 
    password(blank:false) 
    confirmPassword(blank:false) 
       } 
      } 

modalModal.js

var ratPack = $.sammy(function() { 
this.post('#/cart', function() { 
    var firstname = this.params['firstName']; 
    var lastName = this.params['lastName']; 
    var organisation = this.params['organisation']; 
    var email = this.params['email']; 
    var password = this.params['password']; 
    var confirmPassword = this.params['confirmPassword']; 
    var baseurl = this.params['url']; 
    $.ajax({ 
     type : 'POST', 
     url :'http://localhost:9191/guido-rest- 
          resource/api/registration/'+firstname+'/'+lastName+'/'+email, 
     success : function() { 
      alert("success:"); 
     }, 
     error:function(){ 
      $('#errorDiv').text("Email has already been taken"); 
     } 
    }); 
    }); 
     }); 
     $(function() { 
     ratPack.run(); 
     }); 
     function clearBox(){ 
     $('.formElement').val(""); 
     } 


    RegistrationResource.groovy 

     package common.rest.resourcepl 
     import static org.grails.jaxrs.response.Responses.* 
     import javax.ws.rs.Consumes 
     import javax.ws.rs.GET 
     import javax.ws.rs.Produces 
     import javax.ws.rs.Path 
     import javax.ws.rs.PathParam 
     import javax.ws.rs.POST 
     import javax.ws.rs.core.Response 
     import common.servicepl.RegistrationService 
    @Path('/api/registration') 
     class RegistrationResource { 
    @GET 
    @Produces('text/plain') 
    String getRegistrationRepresentation() { 
    'Registration' 
    } 
    def registrationService; 
    @POST 
    @Path('/{firstname}/{lastName}/{email}') 
    Response create(@PathParam('firstname') String firstname, 
       @PathParam('lastName') String lastName, 
       @PathParam('email') String email) { 
       return 
     RegistrationService().createRegistration(firstname,lastName,email); 
    } 
    } 

    RegistrationService.groovy 

     package common.servicepl 
     import common.persistencepl.Registration 
     class RegistrationService { 
       def createRegistration(String firstName,String lastName,String email) { 
     println "Inside Registration Service" 
     def reg = new Registration(); 
     reg.firstName = firstName; 
     reg.lastName = lastName; 
     reg.password = "asdf"; 
     reg.confirmPassword = "asdf"; 
     reg.email = email; 
     reg.organization = "fasdf"; 
     if(reg.save([flush:true])){ 
      return true 
     } 
     else 
     { 
      return false 
     } 
      } 
     } 
+1

당신이 동일한 응용 프로그램에서 서비스를 호출, HTTP'처럼 전체 경로를 speficy하지 않습니다 : // localhost를 .. .', 현재 페이지의 상대적인 위치를 지정하십시오.'/ api/registration ...' – BuddhiP

답변

1

서버는 응답하여 서버 측 에러를 포함한다.

이러한 오류 메시지를 viewmodal의 속성으로 만들고 올바르게 스타일이 지정된 HTML 세그먼트에 바인딩합니다. 당신이 당신의 동일한 응용 프로그램에서 서비스를 호출 할 때 또한

는, http://localhost...처럼 전체 경로를 speficy를 작성해야 Grails의 컨트롤러에서 현재 페이지 /api/registration...

+0

내 뷰 페이지를 체크하면 id = "errorDiv"가 생성됩니다. 이 ID를 사용하고 modalModal.js에서 오류 속성을 만들었습니다. –

+0

오류 검사와 함께 양식 서버 측을 다시 렌더링하는 것이 좋습니다. ''와' '를 찾으십시오. –

+0

@JamesKleeh : 답장에는 고맙습니다. 하지만 요청을 통해 데이터베이스에 내 데이터 값을 저장할 수 있지만 응답의 경우 해당 속성이 작동하지 않는다는 것을 알고 싶습니다. 그게 왜 내가 modalModal.js의 오류 속성에 들어가는 지 알 수 있습니다. 그런데 왜 그런 일이 일어나고 왜 내가 "경고"를 "성공"으로 얻지 못하고 있습니까? –

0

상대를 지정하지 않습니다

if (!beanName.hasErrors() && beanName.validate()) { 
     // TODO 
    } else { 
     render status: Constants.SC_VALIDATION_FAILED, view: "viewName", model: [beanName: beanName] 
} 

와보기는 :

<div> 
<g:hasErrors bean="${beanName}"> 
    <div class="errors"> 
    <g:renderErrors bean="${beanName}" as="list"/> 
    </div> 
</g:hasErrors> 
</div> 
관련 문제