2012-11-30 12 views
5

grailsフレームワークでは作業していますが、controller.Myアプリケーションは「シングル・ページ・アプリケーション」ではありません。ページをリロードしてサービスを登録しないでください。つまりRegistrationService Ajaxコールでだから私はデータバインディングのためのノックアウトを使用しています。データベースを使用して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:// localhostのような絶対パスを指定しないでください。 '、'現在のページ '/ api/registration ... 'との相対値で指定します。 – BuddhiP

答えて

1

サーバーは応答して、サーバー側のエラーを含める必要があります。

これらのエラーメッセージをviewmodalのプロパティにして、適切にスタイル化されたhtmlセグメントにバインドします。

また、あなたは同じアプリケーションからサービスを呼び出すときに、http://localhost...のような完全なパスをspeficyしていないあなたが書くべきGrailsのコントローラでは、あなたの現在のページへの相対/api/registration...

+0

私のビューページをチェックすると、id =" errorDiv "が作成されました。このIDを使用し、modalModal.jsでエラープロパティーを作成しました –

+0

エラーのチェックでフォームサーバー側を再レンダリングする方がよいでしょう。 ''と ' 'を参照してください。 –

+0

@JamesKleeh:返信用にはthanx。 しかし、私は、私のデータ値をデータベースに保存することができますが、レスポンスの場合はそのプロパティが機能していないことを知りたいということを知りたいと思っています。それは私が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> 
関連する問題