From f9cad4fd01c0ea9b6a92c6008cb1922e5e18b61d Mon Sep 17 00:00:00 2001 From: Arthur ZHU Date: Mon, 7 Feb 2022 17:06:00 +0100 Subject: [PATCH] correctif formulaire --- app/entreprises/forms.py | 78 ++++++++++++++++--------- app/templates/entreprises/_contact.html | 4 ++ 2 files changed, 53 insertions(+), 29 deletions(-) diff --git a/app/entreprises/forms.py b/app/entreprises/forms.py index 59cd85929..b644e2bc2 100644 --- a/app/entreprises/forms.py +++ b/app/entreprises/forms.py @@ -85,21 +85,29 @@ class EntrepriseCreationForm(FlaskForm): "Prénom du contact", validators=[DataRequired(message=CHAMP_REQUIS)], ) - telephone = StringField( - "Téléphone du contact", - validators=[DataRequired(message=CHAMP_REQUIS)], - ) + telephone = StringField("Téléphone du contact", validators=[Optional()]) mail = EmailField( "Mail du contact", - validators=[ - DataRequired(message=CHAMP_REQUIS), - Email(message="Adresse e-mail invalide"), - ], + validators=[Optional(), Email(message="Adresse e-mail invalide")], ) - poste = StringField("Poste du contact", validators=[]) - service = StringField("Service du contact", validators=[]) + poste = StringField("Poste du contact", validators=[Optional()]) + service = StringField("Service du contact", validators=[Optional()]) submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) + def validate(self): + rv = FlaskForm.validate(self) + if not rv: + return False + + if not self.telephone.data and not self.mail.data: + self.telephone.errors.append( + "Saisir un moyen de contact (mail ou téléphone)" + ) + self.mail.errors.append("Saisir un moyen de contact (mail ou téléphone)") + return False + + return True + def validate_siret(self, siret): siret = siret.data.strip() if re.match("^\d{14}$", siret) == None: @@ -198,18 +206,13 @@ class ContactCreationForm(FlaskForm): hidden_entreprise_id = HiddenField() nom = StringField("Nom", validators=[DataRequired(message=CHAMP_REQUIS)]) prenom = StringField("Prénom", validators=[DataRequired(message=CHAMP_REQUIS)]) - telephone = StringField( - "Téléphone", validators=[DataRequired(message=CHAMP_REQUIS)] - ) + telephone = StringField("Téléphone", validators=[Optional()]) mail = EmailField( "Mail", - validators=[ - DataRequired(message=CHAMP_REQUIS), - Email(message="Adresse e-mail invalide"), - ], + validators=[Optional(), Email(message="Adresse e-mail invalide")], ) - poste = StringField("Poste", validators=[]) - service = StringField("Service", validators=[]) + poste = StringField("Poste", validators=[Optional()]) + service = StringField("Service", validators=[Optional()]) submit = SubmitField("Envoyer", render_kw={"style": "margin-bottom: 10px;"}) def validate(self): @@ -226,7 +229,15 @@ class ContactCreationForm(FlaskForm): if contact is not None: self.nom.errors.append("Ce contact existe déjà (même nom et prénom)") self.prenom.errors.append("") - return False + + if not self.telephone.data and not self.mail.data: + self.telephone.errors.append( + "Saisir un moyen de contact (mail ou téléphone)" + ) + self.mail.errors.append( + "Saisir un moyen de contact (mail ou téléphone)" + ) + return False return True @@ -234,20 +245,29 @@ class ContactCreationForm(FlaskForm): class ContactModificationForm(FlaskForm): nom = StringField("Nom", validators=[DataRequired(message=CHAMP_REQUIS)]) prenom = StringField("Prénom", validators=[DataRequired(message=CHAMP_REQUIS)]) - telephone = StringField( - "Téléphone", validators=[DataRequired(message=CHAMP_REQUIS)] - ) + telephone = StringField("Téléphone", validators=[Optional()]) mail = EmailField( "Mail", - validators=[ - DataRequired(message=CHAMP_REQUIS), - Email(message="Adresse e-mail invalide"), - ], + validators=[Optional(), Email(message="Adresse e-mail invalide")], ) - poste = StringField("Poste", validators=[]) - service = StringField("Service", validators=[]) + poste = StringField("Poste", validators=[Optional()]) + service = StringField("Service", validators=[Optional()]) submit = SubmitField("Modifier", render_kw={"style": "margin-bottom: 10px;"}) + def validate(self): + rv = FlaskForm.validate(self) + if not rv: + return False + + if not self.telephone.data and not self.mail.data: + self.telephone.errors.append( + "Saisir un moyen de contact (mail ou téléphone)" + ) + self.mail.errors.append("Saisir un moyen de contact (mail ou téléphone)") + return False + + return True + class HistoriqueCreationForm(FlaskForm): etudiant = StringField( diff --git a/app/templates/entreprises/_contact.html b/app/templates/entreprises/_contact.html index 50fe4185c..e2f61aa9c 100644 --- a/app/templates/entreprises/_contact.html +++ b/app/templates/entreprises/_contact.html @@ -3,8 +3,12 @@
Nom : {{ contact.nom }}
Prénom : {{ contact.prenom }}
+ {% if contact.telephone %} Téléphone : {{ contact.telephone }}
+ {% endif %} + {% if contact.mail %} Mail : {{ contact.mail }}
+ {% endif %} {% if contact.poste %} Poste : {{ contact.poste }}
{% endif %}