From a1d4e126ad5cbb7da39efdd5ac460231b13c998c Mon Sep 17 00:00:00 2001
From: Carlos Vega <carlos.vega@.uni.lu>
Date: Wed, 26 Sep 2018 16:56:11 +0200
Subject: [PATCH] updated solution for issue #222

---
 smash/web/forms/worker_form.py       |  9 ++++++++-
 smash/web/templates/doctors/add.html | 10 ++++++++--
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/smash/web/forms/worker_form.py b/smash/web/forms/worker_form.py
index bcbb40ca..8835f916 100644
--- a/smash/web/forms/worker_form.py
+++ b/smash/web/forms/worker_form.py
@@ -33,8 +33,15 @@ class WorkerForm(ModelForm):
                 initial_role = roles[0].role
 
         choices = role_choices_by_worker_type(worker_type)
+
         if worker_type in [WORKER_VOUCHER_PARTNER, WORKER_HEALTH_PARTNER]:
-            self.fields['role'] = forms.ChoiceField(label='Role', choices=choices, widget=forms.HiddenInput(), required=False)
+            '''
+            Since ChoiceField expects tuples (id, label), for a *custom* HiddenInput field we need to create a charfield with TextInput widget
+            providing the value of the *id*. hidden_form_field CSS class is defined in templates/doctors/add.html and has display:none property.
+            '''
+            if initial_role is None:
+                initial_role = choices[0][0] # get 1st element of 1st tuple (('VOUCHER_PARTNER', 'Voucher Partner'),)
+            self.fields['role'] = forms.CharField(label='Role', widget=forms.TextInput(attrs={'class': 'hidden_form_field'}), disabled=True)
         else:
             self.fields['role'] = forms.ChoiceField(label='Role', choices=choices)
         
diff --git a/smash/web/templates/doctors/add.html b/smash/web/templates/doctors/add.html
index ad00fa25..d3fe6a0f 100644
--- a/smash/web/templates/doctors/add.html
+++ b/smash/web/templates/doctors/add.html
@@ -5,6 +5,11 @@
 {% block styles %}
     {{ block.super }}
     <link rel="stylesheet" href="{% static 'AdminLTE/plugins/awesomplete/awesomplete.css' %}"/>
+    <style type="text/css">
+        .hidden_form_field{
+            display: none;
+        }
+    </style>
 {% endblock styles %}
 
 {% block ui_active_tab %}'workers'{% endblock ui_active_tab %}
@@ -27,12 +32,13 @@
 
                 <div class="box-body">
                     <div class="col-sm-6">
-                        {% for field in form.visible_fields %}
-                            <div class="form-group  {% if field.errors %}has-error{% endif %}">
+                        {% for field in form %}
+                            <div class="form-group  {% if field.errors %}has-error{% endif %} {{ field.field.widget.attrs.class }}">
                                 <label for="{# TODO #}" class="col-sm-4 control-label">
                                     {{ field.label }}
                                 </label>
 
+                                {{field.field.widget.attrs.class}}
                                 <div class="col-sm-8">
                                     {{ field|add_class:'form-control' }}
                                 </div>
-- 
GitLab