Merge lp:~openerp-dev/openobject-client-web/listgrid-validation into lp:openobject-client-web/trunk

Proposed by Vaibhav Darji
Status: Merged
Merged at revision: 3256
Proposed branch: lp:~openerp-dev/openobject-client-web/listgrid-validation
Merge into: lp:openobject-client-web/trunk
Diff against target: 208 lines (+85/-40) (has conflicts)
2 files modified
addons/openerp/static/javascript/listgrid.js (+71/-23)
addons/openerp/widgets/templates/listgrid.mako (+14/-17)
Text conflict in addons/openerp/widgets/templates/listgrid.mako
To merge this branch: bzr merge lp:~openerp-dev/openobject-client-web/listgrid-validation
Reviewer Review Type Date Requested Status
Navrang Oza (community) Approve
Xavier (Open ERP) (community) Approve
Review via email: mp+34733@code.launchpad.net

Description of the change

implemented validation for inline editable list and o2m.

To post a comment you must log in.
3216. By Vaibhav Darji

[FIX] Clean up.

3217. By Xavier (Open ERP)

[FIX] discovery of @record-bearing row

Revision history for this message
Xavier (Open ERP) (xmo-deactivatedaccount) wrote :

* Should validate all fields at once, currently only validates one field at a time

* If validation failed, should set focus on first failed field so user doesn't have to re-select it by mouse in order to fix

* Fields fixed don't get back to "blue" color after next validation:
    - Create new analytic journal item
    - Put gibberish in Analytic Account field
    - Try to save, "General Account" field becomes red
    - Pick value for general account
    - Try to save again, Analytic Journal is now red but General Account still is.
    - You can fill all incorrect fields one after the other, they never get back to normal (blue)

* In listgridValidation, there are many paths which result in exactly the same behavior. Probably this could be improved by fixing some code (for instance I think `record_id == 'undefined' || typeof record_id == 'undefined'` could be replaced buy `record_id == undefined` no?) merging tests and things like that to reduce the total number of tests (and the complexity of the code).

* I am not sure about storing all those informations (list changed status and current id) as global variables. Probably we could add it a an attribute on the list itself for storing the current id.

Revision history for this message
Xavier (Open ERP) (xmo-deactivatedaccount) :
review: Needs Fixing
3218. By Vaibhav Darji

[FIX] Fixed validation criteria.

3219. By Vaibhav Darji

[IMP] Imporved validation.

3220. By Vaibhav Darji

[FIX] Fix Validation for require fields.

Revision history for this message
Xavier (Open ERP) (xmo-deactivatedaccount) wrote :

Looks good to me, if noz is ok with the branch you can merge it.

review: Approve
3221. By Xavier (Open ERP)

[FIX] small style fixes (missing semicolons, undefined test, ...)

Revision history for this message
Navrang Oza (noz-tiny) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'addons/openerp/static/javascript/listgrid.js'
2--- addons/openerp/static/javascript/listgrid.js 2010-09-11 08:05:20 +0000
3+++ addons/openerp/static/javascript/listgrid.js 2010-09-13 07:22:46 +0000
4@@ -28,7 +28,7 @@
5 ////////////////////////////////////////////////////////////////////////////////
6
7 var ListView = function(name) {
8- var elem = jQuery('#'+name).get();
9+ var elem = jQuery('[id="'+name+'"]').get(0);
10
11 if (elem.__listview) {
12 return elem.__listview;
13@@ -62,7 +62,7 @@
14 this.m2m = jQuery('[id*="'+ name + '_set' + '"]');
15 this.default_get_ctx = jQuery('[id*="' + prefix + '_terp_default_get_ctx' + '"]').get() ? jQuery('[id*="' + prefix + '_terp_default_get_ctx' + '"]').val() : null;
16 // save the reference
17- jQuery('[id*="'+name+'"]:first').__listview = this;
18+ jQuery('[id="'+name+'"]').get(0).__listview = this;
19
20 this.sort_order = null;
21 this.sort_key = null;
22@@ -535,7 +535,7 @@
23 this.reload(edit_inline, null, default_get_ctx);
24 },
25
26- save: function(id) {
27+ save: function(id, prev_id) {
28
29 if (openobject.http.AJAX_COUNT > 0) {
30 return callLater(1, bind(this.save, this), id);
31@@ -568,34 +568,48 @@
32 args['_terp_parent/model'] = openobject.dom.get(parent_field + '_terp_model').value;
33 args['_terp_parent/context'] = openobject.dom.get(parent_field + '_terp_context').value;
34 args['_terp_source'] = this.name;
35-
36+
37+
38 var self = this;
39 var req = openobject.http.postJSON('/openerp/listgrid/save', args);
40-
41+
42+ var $current_record = jQuery('table[id="'+this.name+'_grid'+'"]').find('tr.grid-row[record="'+id+'"]');
43 req.addCallback(function(obj) {
44 if (obj.error) {
45- if (obj.error_field) {
46- var fld = openobject.dom.get('_terp_listfields/' + obj.error_field);
47- if (fld && getNodeAttribute(fld, 'kind') == 'many2one') {
48- fld = openobject.dom.get(fld.id + '_text');
49- }
50- if (!obj.error_field.value){
51- jQuery(fld).addClass('errorfield');
52- result = false;
53- } else if(jQuery(fld).hasClass('errorfield')) {
54- jQuery(fld).removeClass('errorfield');
55- }
56- if (!result){
57- alert(obj.error);
58- fld.focus();
59- fld.select();
60- }
61+ var $focus_field;
62+ for (var k in data) {
63+
64+ var $req_field = $current_record.find('td [id="'+'_terp_listfields/'+k+'"].requiredfield');
65+ if(!$req_field.length)
66+ continue;
67+ if($req_field.attr('kind') == 'many2one') {
68+ $req_field = $current_record.find('td [id="'+'_terp_listfields/'+k+'_text'+'"]');
69+ }
70+
71+ var req_value = $req_field.val();
72+ if(req_value == '') {
73+ $req_field.addClass('errorfield');
74+ if(!$focus_field) {
75+ $focus_field = $req_field;
76+ }
77+ } else if($req_field.hasClass('errorfield')) {
78+ $req_field.removeClass('errorfield');
79+ }
80+
81+ }
82+ if($focus_field) {
83+ $focus_field.focus();
84 }
85 } else {
86 openobject.dom.get(prefix + '_terp_id').value = obj.id;
87 openobject.dom.get(prefix + '_terp_ids').value = obj.ids;
88-
89- self.reload(id > 0 ? null : -1, prefix ? 1 : 0);
90+
91+ if(prev_id !== undefined) {
92+ self.reload(prev_id , prefix ? 1 : 0);
93+ } else {
94+ self.reload(id > 0 ? null : -1, prefix ? 1 : 0);
95+ }
96+
97 }
98 });
99 },
100@@ -834,3 +848,37 @@
101 _terp_view_mode : this.view_mode}),{width: 700, height: 500});
102 }
103 });
104+
105+function validateList(_list) {
106+ var $list = jQuery('[id="' + _list + '"]').removeAttr('current_id');
107+ var $check = jQuery('table.grid[id="'+_list+'_grid'+'"] tr.grid-row td:not(.selector)').find('input, select');
108+ $check.change(function() {
109+ $list.attr(
110+ 'current_id',
111+ parseInt(jQuery(this).closest('tr.grid-row').attr('record'), 10) || -1);
112+ });
113+}
114+
115+function listgridValidation(_list, o2m, record_id) {
116+ o2m = parseInt(o2m, 0);
117+ var current_id = jQuery('[id="' + _list + '"]').attr('current_id');
118+ // not(null | undefined)
119+ if(current_id != null) {
120+ if(confirm('This record has been modified \n Do you really want to save it?')) {
121+ new ListView(_list).save(current_id, record_id);
122+ }
123+ } else{
124+ if(o2m) {
125+ var detail = jQuery('table.one2many[id$="' + _list + '"]').attr('detail');
126+ if(record_id == undefined || record_id == -1) {
127+ new One2Many(_list, detail).create();
128+ } else {
129+ new ListView(_list).edit(record_id);
130+ }
131+ } else if(record_id == -1) {
132+ new ListView(_list).create();
133+ } else {
134+ new ListView(_list).edit(record_id);
135+ }
136+ }
137+}
138
139=== modified file 'addons/openerp/widgets/templates/listgrid.mako'
140--- addons/openerp/widgets/templates/listgrid.mako 2010-09-10 05:57:57 +0000
141+++ addons/openerp/widgets/templates/listgrid.mako 2010-09-13 07:22:46 +0000
142@@ -122,7 +122,7 @@
143 </button>
144 % elif o2m:
145 <button title="${_('Create new record.')}" id="${name}_btn_"
146- onclick="new One2Many('${name}', jQuery('table.one2many[id$=${name}]').attr('detail')).create(); return false;">
147+ onclick="listgridValidation('${name}', '${o2m or 0}', -1); return false;">
148 ${_('new')}
149 </button>
150 % else:
151@@ -130,23 +130,8 @@
152 <button id="${name}_new" title="${_('Create new record.')}">${_('new')}</button>
153 % if editors:
154 <script type="text/javascript">
155- var is_list_changed = false;
156- var current_id = -1;
157- jQuery(document).ready(function() {
158- var $check = jQuery("table.grid[id='${name}_grid'] tr.grid-row td:not(.selector)").find('input, select');
159- $check.change(function() {
160- is_list_changed = true;
161- current_id = jQuery(this).closest('tr').attr('record') || -1;
162- });
163- });
164 jQuery('[id=${name}_new]').click(function() {
165- if(is_list_changed) {
166- if (confirm('This record has been modified \n Do you really want to save it?')) {
167- new ListView('${name}').save(current_id)
168- }
169- } else {
170- new ListView('${name}').create();
171- }
172+ listgridValidation('${name}', '${o2m or 0}', -1)
173 return false;
174 });
175 </script>
176@@ -160,6 +145,13 @@
177 % endif
178 % endif
179 % endif
180+ % if not m2m and not dashboard and editors:
181+ <script type="text/javascript">
182+ jQuery(document).ready(function() {
183+ validateList('${name}')
184+ });
185+ </script>
186+ % endif
187 </td>
188 % endif
189 <td class="pager-cell-button" style="display: none;">
190@@ -326,6 +318,7 @@
191 jQuery('table[id=${name}_grid] tr.grid-row').each(function(index, row) {
192 jQuery(row).click(function(event) {
193 if (!jQuery(event.target).is(':input, img, option, td.m2o_coplition')) {
194+<<<<<<< TREE
195 var record_id = jQuery(row).attr('record');
196 if (record_id > 0) {
197 new ListView('${name}').edit(record_id);
198@@ -346,6 +339,10 @@
199 new One2Many('${name}', jQuery('table.one2many[id$=${name}]').attr('detail')).create();
200 }
201 }
202+=======
203+ var record_id = parseInt(jQuery(row).attr('record'), 10) || -1;
204+ listgridValidation('${name}','${o2m or 0}', record_id);
205+>>>>>>> MERGE-SOURCE
206 }
207 });
208 });