如何显示在视图页面的服务器端验证错误 [英] How to display a server side validation error on view page

查看:124
本文介绍了如何显示在视图页面的服务器端验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我工作的Grails框架,但没有使用controller.My应用是单页申请。我不希望我重新载入页面,所以我写了服务即RegistrationService使用Ajax调用。所以我用淘汰赛数据binding.Database使用是PostgreSQL的。 我有一个观点页面,我遇到一个电子邮件字段。当我进入一个重复的电子邮件ID,然后点击保存按钮,然后我可以看到一个验证错误,即电子邮件已经采取但是即使我正在写一个唯一的ID,然后还我得到相同的验证error.This是因为它在modalModal.js页面错误状态去,所以我没有收到如何解决这一问题的疑难问题。只是希望当我输入唯一的ID,然后点击保存按钮,然后它不应该给予验证错误。

  _newCostingpage.gsp(我的看法页)

    < D​​IV ID =轻级=white_content的风格=颜色:黑色;>
    <形式的行动=#/车的方法=邮报>
    <输入类型=隐藏名称=URL值=$ {grailsApplication.config.serverURL}/>
    < P><标签>第一个名字:放大器; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP; <输入名称=名字
             类=formElement数据绑定='值:名字/>< /标签>< / P>
    < P><标签>的名字:&功放; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP; <输入名称=姓
              类=formElement数据绑定='值:名字/>< /标签>< / P>
    < P><标签>组织:其中,输入名称=组织级=formElement数据 - 
              绑定='值:组织/>< /标签>< / P>
<p><label>email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      &LT;输入名称=电子邮件级=formElement数据绑定='值:电子邮件/&GT;&LT; /标签&gt;&LT;标签
         ID =errorDiv&GT;&LT; /标签&gt;&LT; / P&GT;
&LT; P&GT;&LT;标签&gt;密码:放大器; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP;&安培; NBSP; &LT;输入类型=密码NAME =密码
         类=formElement数据绑定=价值:密码/&GT;&LT; /标签&gt;&LT; / P&GT;
 &LT; P&GT;&LT;标签样式=保证金左:-37px;&GT;确认密码:LT; /标签&gt; &LT;输入
        TYPE =密码NAME =confirmPassword级=formElement数据绑定='值:
        confirmPassword'/&GT;&所述; / P&GT;
 &LT; P&GT;&LT;输入类型=提交值=注册/&GT;&LT; / P&GT;
&LT; /形式GT;
 &LT;一href="javascript:void(0)"onclick="document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none';clearBox();">Close</a>
        &LT; / DIV&GT;
        &LT; D​​IV ID =变脸级=black_overlay&GT;&LT; / DIV&GT;
    &LT; / DIV&GT;

域类: -  \

  类注册{
字符串的firstName
字符串的lastName
字符串组织
字符串email
字符串密码
字符串confirmPassword

静态约束= {
    的firstName(可空:假)
    lastName的(可空:真)
    组织(空白:假)
    电子邮件(可空:假的,电子邮件:真正的,独一无二的:真)
    密码(空白:假)
    confirmPassword(空白:假)
               }
            }
 

modalModal.js

  VAR ratPack = $ .sammy(函数(){
this.post('#/车',函数(){
    VAR名字= this.params ['的firstName'];
    VAR的lastName = this.params ['lastName的'];
    VAR组织= this.params ['组织'];
    VAR电子邮件= this.params [电子邮件];
    VAR密码= this.params ['密码'];
    VAR confirmPassword = this.params ['confirmPassword'];
    VAR baseURL时= this.params ['URL'];
    $阿贾克斯({
        键入:POST,
        网址:HTTP://本地主机:9191 /圭多 -  REST-
                          资源/ API /注册/+姓名+/+的lastName +/+电子邮件,
        成功:函数(){
            警报(成功);
        },
        错误:函数(){
           $('#errorDiv)文本(电子邮件已经采取了);
        }
    });
    });
      });
        $(函数(){
       ratPack.run();
        });
        功能clearBox(){
       $('。formElement')VAL()。
       }


   RegistrationResource.groovy

         包common.rest.resourcepl
         引入静态org.grails.jaxrs.response.Responses。*
         进口javax.ws.rs.Consumes
         进口javax.ws.rs.GET
         进口javax.ws.rs.Produces
         进口javax.ws.rs.Path
       进口javax.ws.rs.PathParam
       进口javax.ws.rs.POST
      进口javax.ws.rs.core.Response
      进口common.servicepl.RegistrationService
     @Path(/ API /注册)
       类RegistrationResource {
   @得到
   @Produces(text / plain的)
   字符串getRegistrationRe presentation(){
    '注册'
    }
   DEF registrationService;
    @POST
    @Path(/ {姓} / {lastName的} / {电子邮件})
    响应创建(@PathParam('姓')字符串名字,
                @PathParam('的lastName')字符串姓氏,
                @PathParam(电子邮件)字符串电子邮件){
                返回
        。RegistrationService()createRegistration(名字,姓氏,电子邮件);
  }
     }

     RegistrationService.groovy

         包common.servicepl
         进口common.persistencepl.Registration
         类RegistrationService {
              高清createRegistration(字符串名字,字符串姓氏,字符串电子邮件){
        的println内部登记服务
        高清章=新注册();
        reg.firstName =名字;
        reg.lastName = lastName的;
        reg.password =ASDF;
        reg.confirmPassword =ASDF;
        reg.email =电子邮件;
        reg.organization =fasdf;
        如果(reg.save([齐平:真])){
           返回true
        }
        其他
        {
          返回false
        }
          }
         }
 

解决方案

服务器应该在响应中包含服务器端错误。

请这些错误消息的viewmodal的属性,并绑定了正确的风格HTML代码段。

此外,当您从同一个应用程序调用服务,不speficy的完整路径,如的http://本地主机... ,指定其相对于您目前的页面 / API /注册...

As i'm working on grails framework but not using controller.My application is "Single Page Application".I dont want to reload my page so I have written service i.e RegistrationService with Ajax calling. So i'm using knockout for data-binding.Database using is postgresql . I have a view page where i'm having an email field. When i'm entering a duplicate email id and clicking save button then i can see a validation error i.e "Email has already been taken" but even if i'm writing a unique id then also i'm getting the same validation error.This is because its going in error condition in modalModal.js page, So I'm not getting how to fix this problem.I just want when i enter unique id and click save button then it should not given validation error.

_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
        }
          }
         }

解决方案

Server should include server side errors in the response.

Make those error messages a property of the viewmodal, and bind that to properly styled html segment.

Also, when you call a service from your same application, do not speficy the full path like http://localhost..., Specify it relative to your current page /api/registration...

这篇关于如何显示在视图页面的服务器端验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆