formulaire dynamique envoi offre

This commit is contained in:
Arthur ZHU 2022-04-05 23:01:38 +02:00
parent 9af424d356
commit 8d64bf3b87
5 changed files with 58 additions and 31 deletions

View File

@ -40,6 +40,7 @@ from wtforms import (
SelectMultipleField,
DateField,
BooleanField,
FieldList,
)
from wtforms.validators import ValidationError, DataRequired, Email, Optional
from wtforms.widgets import ListWidget, CheckboxInput
@ -424,19 +425,13 @@ class StageApprentissageModificationForm(FlaskForm):
class EnvoiOffreForm(FlaskForm):
responsable_1 = _build_string_field(
"Responsable de formation (*)",
render_kw={"placeholder": "Tapez le nom du responsable de formation"},
)
responsable_2 = _build_string_field(
"Responsable de formation",
required=False,
render_kw={"placeholder": "Tapez le nom du responsable de formation"},
)
responsable_3 = _build_string_field(
"Responsable de formation",
required=False,
render_kw={"placeholder": "Tapez le nom du responsable de formation"},
responsables = FieldList(
_build_string_field(
"Responsable (*)",
render_kw={"placeholder": "Tapez le nom du responsable de formation"},
),
min_entries=1,
max_entries=5,
)
submit = SubmitField("Envoyer", render_kw=SUBMIT_MARGE)
@ -445,12 +440,9 @@ class EnvoiOffreForm(FlaskForm):
if not FlaskForm.validate(self):
validate = False
for field in self:
if (
field.name in ["responsable_1", "responsable_2", "responsable_3"]
and field.data.strip()
):
responsable_data = field.data.upper().strip()
for entry in self.responsables.entries:
if entry.data:
responsable_data = entry.data.upper().strip()
stm = text(
"SELECT id, UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')')) FROM \"user\" WHERE UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')'))=:responsable_data"
)
@ -460,7 +452,7 @@ class EnvoiOffreForm(FlaskForm):
.first()
)
if responsable is None:
field.errors.append("Champ incorrect (selectionnez dans la liste)")
entry.errors.append("Champ incorrect (selectionnez dans la liste)")
validate = False
return validate

View File

@ -987,12 +987,9 @@ def envoyer_offre(id):
)
form = EnvoiOffreForm()
if form.validate_on_submit():
for field in form:
if (
field.name in ["responsable_1", "responsable_2", "responsable_3"]
and field.data
):
responsable_data = field.data.upper().strip()
for responsable in form.responsables.entries:
if responsable.data.strip():
responsable_data = responsable.data.upper().strip()
stm = text(
"SELECT id, UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')')) FROM \"user\" WHERE UPPER(CONCAT(nom, ' ', prenom, ' ', '(', user_name, ')'))=:responsable_data"
)

View File

@ -11,7 +11,9 @@
{% block app_content %}
<div class="container" style="margin-bottom: 10px;">
<h1>Liste des contacts</h1>
{% if current_user.has_permission(current_user.Permission.RelationsEntreprisesChange, None) %}
<a class="btn btn-primary" style="margin-bottom:10px;" href="{{ url_for('entreprises.add_contact', id=entreprise_id) }}">Ajouter contact</a>
{% endif %}
<table id="table-contacts">
<thead>
<tr>

View File

@ -16,7 +16,20 @@
<p>
(*) champs requis
</p>
{{ wtf.quick_form(form, novalidate=True) }}
<form method="POST" action="" novalidate>
{{ form.hidden_tag() }}
{{ form.responsables.label }}<br>
{% for subfield in form.responsables %}
{% if subfield.errors %}
{% for error in subfield.errors %}
<p class="help-block">{{ error }}</p>
{% endfor %}
{% endif %}
{% endfor %}
{{ form.responsables }}
<button class="btn btn-default" id="add-responsable-field">Ajouter un responsable</button>
<input class="btn btn-default" type="submit" value="Envoyer">
</form>
</div>
</div>
@ -30,9 +43,32 @@
minchars: 2,
timeout: 60000
};
var as_responsables_1 = new bsn.AutoSuggest('responsable_1', responsables_options);
var as_responsables_2 = new bsn.AutoSuggest('responsable_2', responsables_options);
var as_responsables_3 = new bsn.AutoSuggest('responsable_3', responsables_options);
let allResponsablesFieldWrapper = document.getElementById('responsables');
let allResponsablesField = allResponsablesFieldWrapper.getElementsByTagName('input');
for(let i = 0; i < allResponsablesField.length; i++) {
new bsn.AutoSuggest(allResponsablesField[i].id, responsables_options);
document.getElementById(allResponsablesField[i].id).classList.add("form-control");
}
let addResponsableFieldBtn = document.getElementById('add-responsable-field');
addResponsableFieldBtn.addEventListener('click', function(e){
e.preventDefault();
let allResponsablesFieldWrapper = document.getElementById('responsables');
let allResponsablesField = allResponsablesFieldWrapper.getElementsByTagName('input');
if(allResponsablesField.length > 4) {
return;
}
let responsableInputIds = []
for(let i = 0; i < allResponsablesField.length; i++) {
responsableInputIds.push(parseInt(allResponsablesField[i].name.split('-')[1]));
}
let newFieldName = `responsables-${Math.max(...responsableInputIds) + 1}`;
allResponsablesFieldWrapper.insertAdjacentHTML('beforeend',`
<li><label for="${newFieldName}">Responsable</label> <input class="form-control" id="${newFieldName}" name="${newFieldName}" type="text" value="" placeholder="Tapez le nom du responsable de formation"></li>
`);
var as_r = new bsn.AutoSuggest(newFieldName, responsables_options);
});
}
</script>
{% endblock %}

View File

@ -43,8 +43,8 @@
<a class="btn btn-danger" href="{{ url_for('entreprises.delete_entreprise', id=entreprise.id) }}">Supprimer</a>
<a class="btn btn-primary" href="{{ url_for('entreprises.add_offre', id=entreprise.id) }}">Ajouter offre</a>
<a class="btn btn-primary" href="{{ url_for('entreprises.add_correspondant', id=entreprise.id) }}">Ajouter correspondant</a>
<a class="btn btn-primary" href="{{ url_for('entreprises.add_contact', id=entreprise.id) }}">Ajouter contact</a>
{% endif %}
<a class="btn btn-primary" href="{{ url_for('entreprises.contacts', id=entreprise.id) }}">Liste contacts</a>
<a class="btn btn-primary" href="{{ url_for('entreprises.offres_expirees', id=entreprise.id) }}">Voir les offres expirées</a>
<div>