[英]How to do a server-side Ajax validation in a modal box
I currently have an app that is using a twitter bootstrap modal box. 我目前有一个使用Twitter Bootstrap模式框的应用程序。 The div that the modal is displaying is getting loaded with JQuery from /users/new action. 模态显示的div从/ users / new操作中被JQuery加载。 Everything works great if the user enters valid data in the modal box submitted but if the data sent to params does not pass validation the controller redirects back to the new action but this time outside of the modal box and the page that user was previously on is gone, which sort of defeats the purpose of having the modal box to begin with. 如果用户在提交的模式框中输入有效数据,则一切工作都很好,但是如果发送给params的数据未通过验证,则控制器将重定向回新操作,但是这次不在模式框中,并且用户之前所在的页面是消失了,这破坏了使用模态盒的目的。
I have looked at Live-Validation and other javascript based client-side validations. 我看过Live-Validation和其他基于JavaScript的客户端验证。 These are an option but I would much prefer being able to use my existing validations in the users model for consistency's sake. 这些是一个选项,但是为了保持一致性,我更希望能够在用户模型中使用现有的验证。 I there a good way to make the modal box persist while the form is being submitted and to make the controller reload the page within the modal with the error_messages? 我有一个很好的方法可以使模式框在提交表单时保持不变,并让控制器使用error_messages重新加载模式内的页面?
Thanks 谢谢
Are the error_messages loaded into the modal using javascript? 是否使用javascript将error_messages加载到模式中? If so, you could have the page check to see if there are error_messages on load, then pop open the modal box if there are any. 如果是这样,您可以让页面检查以查看是否加载了error_messages,然后如果有的话弹出模式框。 This won't save any input from the modal, but it will pop it open. 这不会保存模态中的任何输入,但是会弹出它。
Alternatively, you could implement some light client side validation on top of your current model. 或者,您可以在当前模型的基础上实施一些轻客户端验证。 However, how light it would be is dependent on how picky your model is regarding its input. 但是,灯光的强度取决于模型对输入的挑剔程度。 If it's very specific what it will accept/reject, it would be much harder to verify consistency, and having something get through client-side validation only to bounce back and say it was invalid isn't always pretty. 如果要接受/拒绝的内容非常具体,则要验证一致性会困难得多,并且让某些内容通过客户端验证只是反弹然后说无效是不总是很漂亮的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.