Grails 和 AJAX:Grails 表单验证使用 ModalBox 或 YUI2 Dialog 工作吗?
几个 Grails 应用程序(例如我正在编写的应用程序)需要 /user 视图和 /admin 视图,这些视图是“仪表板”,基本上用户或管理员可能会在成功登录之后登陆该页面,并且所有数据表和选项卡都在那里,因此他们几乎不需要离开该页面,从而提供更令人满意的用户体验,就像 Gmail 或 Mint 的用户已经习惯了一样。
为了允许从主/用户仪表板进行搜索和表单发布,我一直在使用 modalbox 插件 v0.4 grails install modalbox
。 Modalbox 显然正在管理 GET/POST 本身,不幸的是,它丢失了典型约束块为您提供的 99% 的验证。
现在通过替换 g:submitButton 来解决这个问题,在提交中添加一些基本的 JavaScript,如图所示。 (当然,这是一个糟糕的解决方法。)
<input type="button" name="create" class="save" onclick="if (!(document.getElementById('name').value === '' || document.getElementById('summary').value === '')) { document.forms[0].submit(); }" value="Create" />
但必须有更好的方法!有人告诉我使用 g:remoteForm,但还没有看到足够完整的示例来使用 Modalbox 。也许人们正在使用一个新窗口,它会像 Gmail 的撰写窗口一样自动关闭几秒钟?
这可能是一个非常常见的场景,对于 SiteMesh 模板来说已经成熟,或者至少是一个“渲染模板:”,甚至是像 modalBox:createLink 这样的 GSP 标签。
来源可在 Gtown 项目空间中的此处获取。
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.
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
. Modalbox obviously is managing the GET/POST itself and unfortunately is losing 99% of the validations that the typical constraints block gives you.
Working around this for now by replacing g:submitButton, putting some basic JavaScript in the submit as shown. (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. Maybe folks are using a new window which would automatically close a couple of seconds like Gmail's compose window?
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.
Source available here in Gtown project space.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Modalbox 上的示例已将验证错误隐藏在灯箱中:
所以我认为如果你想使用 Grails 验证,你必须自己控制表单。在这种情况下,灯箱插件将只负责绘制灯箱,您将提供所有表单内容。
我使用 Boxy(另一个灯箱插件,其工作方式应该与您的 Modalbox 类似),下面是我如何在灯箱中使用 Grails 验证的示例。当我创建灯箱时,我使用“shell”操作来填充它。 shell 操作渲染一个模板,模板内部有一个。第一次绘制搜索表单。调用验证操作并使用验证操作的结果重新绘制灯箱的内容。结果可能是成功页面或验证错误页面。
命令对象(在本示例中为 SearchCommand)的使用仅用于演示。
控制器:
_lightboxFrame.gsp:
_lightboxContents.gsp:
如果您希望更多地控制搜索成功或失败时执行的操作,例如在失败时呈现验证错误或在成功时关闭灯箱,您可能需要编写你自己的 JavaScript 回调。
与此问题有些相关,并且可能对您有帮助的是 Grails 远程约束插件。我已经有一段时间没有尝试过它了,但是您应该能够使用它来异步重绘页面的某些部分,并处理 Grails 生成的验证错误。
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. 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. 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. <g:formRemote> calls a validation action and redraws the contents of the lightbox with the results of the validation action. 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.
Controller:
_lightboxFrame.gsp:
_lightboxContents.gsp:
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.
Somewhat related to this problem, and it may be helpful to you, is the Grails Remote Constraints plugin. 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.