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

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

问题描述

我正在使用extjs 4& rails 3.
我想要在extjs 表单上的唯一性的验证。
我想在 Rails模型上进行验证,以获得唯一性,所以我在rails栏目中进行了以下操作:

  validates_uniqueness_of:search_key,:message => 找到重复的值

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

  store.add(values); 

如果验证失败,则记录不会插入数据库。
现在我想弹出一个警告框,指示验证失败时条目是重复的,以便用户可以编辑字段。
我如何在控制器和控制器之间进行通信用于验证的extjs表单
还将rails回调在其中是有用的?

解决方案

这是如何将从服务器返回的消息传递给extjs应用程式,我认为您可以根据自己的需要进行修改,或者让我知道如果我需要澄清任何内容:



Extjs






在模型中,添加:

  proxy:{
type:'rest',
url:'/ manifest-items',
reader:{
type:'json',//我们期望服务器给我们一个JSON字符串作为响应
root:'rows',
totalProperty:'total',
messageProperty:'message',
}
} ,

Rails控制器






然后,在错误或成功的两种情况下,返回的响应应为:

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

返回Extjs






在extjs中,可以访问以下消息:



important :注意获取错误消息和成功案例之间的差异。

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


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);

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?

解决方案

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


in model, add:

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 Controller


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

Back to 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天全站免登陆