From 111527b53baf2699ae57dc518ea69b37dc8fbdf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Thu, 5 Oct 2017 21:36:27 +0000 Subject: [PATCH] Gestion des hidden fields --- .../templatetags/bootstrap_form_typeahead.py | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/machines/templatetags/bootstrap_form_typeahead.py b/machines/templatetags/bootstrap_form_typeahead.py index 9c1363df..9b38b528 100644 --- a/machines/templatetags/bootstrap_form_typeahead.py +++ b/machines/templatetags/bootstrap_form_typeahead.py @@ -62,20 +62,23 @@ def bootstrap_form_typeahead(django_form, typeahead_fields, *args, **kwargs): t_fields = typeahead_fields.split(',') exclude = kwargs.get('exclude', None) exclude = exclude.split(',') if exclude else [] + hidden = [h.name for h in django_form.hidden_fields()] form = '' for f_name, f_value in django_form.fields.items() : if not f_name in exclude : if f_name in t_fields : - form += render_tag( - 'div', - attrs = {'class': 'form-group'}, - content = label_tag( f_name, f_value ) + - input_tag( f_name, f_value ) + - hidden_tag( f_name ) + - typeahead_full_script( f_name, f_value ) - ) - + if not f_name in hidden : + form += render_tag( + 'div', + attrs = {'class': 'form-group'}, + content = label_tag( f_name, f_value ) + + input_tag( f_name, f_value ) + + hidden_tag( f_name ) + + typeahead_full_script( f_name, f_value ) + ) + else: + form += hidden_tag( f_name ) else: form += render_field( f_value.get_bound_field(django_form, f_name), @@ -89,14 +92,15 @@ def bootstrap_form_typeahead(django_form, typeahead_fields, *args, **kwargs): def input_id( f_name ): return 'typeahead_input_'+f_name -def select_id( f_name ): +def hidden_id( f_name ): return 'typeahead_select_'+f_name def hidden_tag( f_name ): return render_tag( 'input', attrs={ - 'id': select_id(f_name), + 'id': hidden_id(f_name), + 'maxlength': 255, 'name': f_name, 'type': 'hidden', 'value': '' @@ -173,7 +177,7 @@ def typeahead_datasets( f_name ) : def typeahead_updater( f_name ): return 'function(evt, item) { ' \ - '$("#'+select_id(f_name)+'").val( item.key ); ' \ + '$("#'+hidden_id(f_name)+'").val( item.key ); ' \ 'return item; ' \ '}'