extjs4 rails 3 模型验证的唯一性 [英] extjs4 rails 3 model validation for uniqueness

查看:16
本文介绍了extjs4 rails 3 模型验证的唯一性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 extjs 4 &导轨 3.我想验证 extjs 表单 上字段的唯一性.我想对 Rails 模型 的唯一性进行验证,所以我已经对我的 Rails 模型进行了以下操作:

i m working with extjs 4 & rails 3. I want to have validation for uniqueness of field that is on extjs form. I want to have validation on Rails model for uniqueness, so i have done follwing on my rails model :

validates_uniqueness_of :search_key, :message => "Duplicate value found"

我将表单的新值插入到商店中,如下所示:

I am inserting new values of form into store as follows :

store.add(values);

如果验证失败,记录不会被插入到数据库中.现在我想弹出一个警告框,指示验证失败时该条目是重复的,以便用户可以编辑字段.我怎样才能在 Rails 控制器和控制器之间进行这种通信?用于验证的 extjs 表单?Rails 回调在其中也有用吗?

If validation fails, the record does not get inserted into database. Now I want to pop an alert box indicating that the entry is duplicate when validation fails, so that user can edit field. How can i make this communication betwn rails controller & extjs form for validation? Also would rails callback will be useful in it?

推荐答案

这里是如何将从服务器返回的消息传递到 extjs 应用程序,我假设您可以修改它以满足您的需要,或者让我知道是否需要澄清任何东西:

Here is how to pass messages returned from server to extjs app, I assume you can modify this to suit your need, or let me know if I need to clarify anything:

Extjs

在模型中,添加:

proxy: {
  type: 'rest',
  url: '/manifest-items',
  reader: {
    type: 'json', // We expect the server to give us a JSON string as a response
    root: 'rows',
    totalProperty: 'total',
    messageProperty: 'message',
  }
},

Rails 控制器

那么,无论是错误还是成功,返回的响应都应该是:

Then, in both cases of error or success, returned response should be:

render :json => {:success => false, :message => "some error", :rows => [something], :total => x}.to_json

回到 Extjs

在extjs中,可以按如下方式访问消息:

In extjs, the message can be accessed as follows:

重要:注意在失败和成功案例中获取错误消息之间的区别.

important: note the difference between getting error message in failure and success cases.

manifestItem.save({
  success: function(records, operation) {
    Ext.Msg.alert(operation.resultSet.message);
  },
  failure: function(records, operation) {
    Ext.Msg.alert(operation.error);
  }
});

这篇关于extjs4 rails 3 模型验证的唯一性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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