简体   繁体   English

Grails和AJAX:使用ModalBox或YUI2对话框可以进行Grails表单验证吗?

[英]Grails and AJAX: Grails form validation working using ModalBox or YUI2 Dialog?

Several Grails applications, like the one I'm writing, require a /user view and /admin view that are 'dashboards', basically the user or admin lands on that page, possibly after a successful login and all the datatables and tabs are there, so they hardly ever need to navigate off that page, providing a more satisfying users experience, like users of Gmail or Mint have become accustomed. 几个Grails应用程序(例如我正在编写的应用程序)都需要一个/ user视图和/ admin视图,它们是“仪表板”,基本上,该用户或admin可以登陆该页面,可能是在成功登录之后,所有的数据表和选项卡都在那里,因此他们几乎不需要导航到该页面,即可提供更令人满意的用户体验,例如Gmail或Mint用户已经习惯了。

In order to allow searches and form posts from the main /user dashboard, I've been using the modalbox plug-in v0.4 grails install modalbox . 为了允许从主/ user仪表板进行搜索和形成帖子,我一直在使用modalbox插件v0.4 grails install modalbox Modalbox obviously is managing the GET/POST itself and unfortunately is losing 99% of the validations that the typical constraints block gives you. Modalbox显然是在管理GET / POST本身,不幸的是,它丢失了典型约束块提供给您的99%的验证。

Working around this for now by replacing g:submitButton, putting some basic JavaScript in the submit as shown. 现在通过替换g:submitButton来解决此问题,如图所示将一些基本JavaScript放入提交中。 (This is a terrible workaround, of course.) (当然,这是一个糟糕的解决方法。)

<input type="button" name="create" class="save" onclick="if (!(document.getElementById('name').value === '' || document.getElementById('summary').value === '')) { document.forms[0].submit(); }" value="Create" />

But there must be a better way! 但是必须有更好的方法! I've been told to use g:remoteForm, but have not seen a complete enough example to work w/ Modalbox. 我被告知要使用g:remoteForm,但是还没有看到一个足够完整的示例可以使用Modalbox。 Maybe folks are using a new window which would automatically close a couple of seconds like Gmail's compose window? 也许人们正在使用一个新窗口,它会像Gmail的撰写窗口一样自动关闭几秒钟?

This is likely a scenario so common as to be ripe for a SiteMesh template, or at the very least a 'render template:' or even a GSP tag like the modalBox:createLink. 对于SiteMesh模板,或者至少是“渲染模板:”甚至是GSP标签(如modalBox:createLink),这种情况很常见,以至于已经成熟。

Source available here in Gtown project space. 提供源代码这里在Gtown项目空间。

The example on Modalbox has the validation error already hidden in the lightbox: <p style="display:none">Invalid Email!</p> so I think if you want to use Grails validation, you will have to take control over the form yourself. Modalbox上的示例的灯箱中已经隐藏了验证错误:<p style =“ display:none”>无效的电子邮件!</ p>,因此,我认为如果您要使用Grails验证,则必须控制形成自己。 In this case the lightbox plugin will only be responsible for drawing the lightbox, and you will provide all the form contents. 在这种情况下,灯箱插件仅负责绘制灯箱,您将提供所有表单内容。

I use Boxy (another lightbox plugin which should work similarly to your Modalbox) and here's an example of how I use Grails validation within the lightbox. 我使用Boxy(另一个灯箱插件,其功能应与Modalbox相似),这是一个示例,说明了如何在灯箱中使用Grails验证。 When I create the lightbox I use a "shell" action to fill it. 创建灯箱时,我会使用“外壳”操作来填充它。 The shell action renders a template, and inside the template is a <g:include> which draws the search form the first time. shell操作将呈现一个模板,并且在模板内部是一个<g:include>,它会第一次绘制搜索表单。 <g:formRemote> calls a validation action and redraws the contents of the lightbox with the results of the validation action. <g:formRemote>调用验证动作,并使用验证动作的结果重绘灯箱的内容。 The results could be either be a success page or a validation error page. 结果可能是成功页面或验证错误页面。

The use of a command object, in this example SearchCommand, is just for demonstration. 命令对象(在本示例中为SearchCommand)的使用仅用于演示。

Controller: 控制器:

def launchLightbox = {
    render template: 'lightboxFrame'
}

def lightboxContents = { SearchCommand cmd ->

    // if the search failed, re-draw the search form with validation errors
    if (cmd.hasErrors()) {
        return [cmd: cmd]
    } 

    // the search succeeded. Show the results within the lightbox
    else {
        render template: 'searchResults', model: [results: cmd.results]
    }
}

_lightboxFrame.gsp: _lightboxFrame.gsp:

<g:formRemote name="searchLightbox" 
        url="[action: 'lightboxContents']" update="lightboxContentsDiv">
    <div id="lightboxContentsDiv">
        <g:include view="/yourController/_lightboxContents.gsp"/>
    </div>
</g:formRemote>

_lightboxContents.gsp: _lightboxContents.gsp:

<g:renderErrors bean="${cmd}"/>
<p>Enter your search:</p>
<g:textField name="search" value="${cmd?.search}"/>
<g:submitButton name="submitButton" value="Submit"/>

If you want more control over what to do when the search succeeds or fails, for example to either render the validation errors on failure or close the lightbox on success, you'll probably need to write your own javascript callback. 如果您想更好地控制搜索成功或失败时的操作,例如在失败时呈现验证错误或在成功时关闭灯箱,则可能需要编写自己的JavaScript回调。

Somewhat related to this problem, and it may be helpful to you, is the Grails Remote Constraints plugin . 与此问题相关的某种Grails远程约束插件 ,它可能对您有所帮助。 I haven't tried it in a while, but you should be able to use it to redraw portions of your page asynchronously with validation errors generated by Grails. 我已经有一段时间没有尝试过了,但是您应该可以使用它来重绘Grails生成的验证错误,从而异步重绘页面的某些部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM