Skip to content

Commit

Permalink
add validator #2168
Browse files Browse the repository at this point in the history
  • Loading branch information
sfinx13 committed Feb 27, 2024
1 parent 694fd45 commit cc99774
Show file tree
Hide file tree
Showing 17 changed files with 378 additions and 139 deletions.
20 changes: 12 additions & 8 deletions src/Controller/Back/SignalementEditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ public function editInformationsLogement(
'json'
);

$validationGroups = ['Default', 'EDIT'];
$validationGroups = ['Default'];
if ($signalement->getProfileDeclarant()) {
$validationGroups[] = $signalement->getProfileDeclarant()->value;
}
$errorMessage = FormHelper::getErrorsFromRequest($validator, $informationsLogementRequest, $validationGroups);

if (empty($errorMessage)) {
Expand Down Expand Up @@ -285,15 +288,11 @@ public function editCompositionLogement(
);

$validationGroups = ['Default'];
if ('autre' == $compositionLogementRequest->getType()) {
$validationGroups[] = 'TYPE_LOGEMENT_AUTRE';
}
if ($signalement->getProfileDeclarant()) {
$validationGroups[] = $signalement->getProfileDeclarant()->value;
}

$errorMessage = FormHelper::getErrorsFromRequest($validator, $compositionLogementRequest, $validationGroups);

if (empty($errorMessage)) {
$signalementManager->updateFromCompositionLogementRequest($signalement, $compositionLogementRequest);
$response = [
Expand Down Expand Up @@ -339,7 +338,10 @@ public function editSituationFoyer(
'json'
);

$validationGroups = ['Default', 'EDIT'];
$validationGroups = ['Default'];
if ($signalement->getProfileDeclarant()) {
$validationGroups[] = $signalement->getProfileDeclarant()->value;
}
$errorMessage = FormHelper::getErrorsFromRequest($validator, $situationFoyerRequest, $validationGroups);

if (empty($errorMessage)) {
Expand Down Expand Up @@ -386,8 +388,10 @@ public function editProcedureDemarches(
ProcedureDemarchesRequest::class,
'json'
);

$validationGroups = ['Default', 'EDIT'];
$validationGroups = ['Default'];
if ($signalement->getProfileDeclarant()) {
$validationGroups[] = $signalement->getProfileDeclarant()->value;
}
$errorMessage = FormHelper::getErrorsFromRequest($validator, $procedureDemarchesRequest, $validationGroups);

if (empty($errorMessage)) {
Expand Down
73 changes: 72 additions & 1 deletion src/Dto/Request/Signalement/CompositionLogementRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@

use App\Dto\Request\RequestInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

class CompositionLogementRequest implements RequestInterface
{
public function __construct(
#[Assert\NotBlank(message: 'Merci de définir le type de logement.')]
private readonly ?string $type = null,
#[Assert\NotBlank(message: 'Merci de préciser le type de logement autre.', groups: ['TYPE_LOGEMENT_AUTRE'])]
#[Assert\When(
expression: 'this.getType() == "autre"',
constraints: [
new Assert\NotBlank(message: 'Merci de préciser le type de logement autre.'),
],
)]
private readonly ?string $typeLogementNatureAutrePrecision = null,
#[Assert\NotBlank(message: 'Merci de selectioner pièce unique ou plusieurs pièces.')]
#[Assert\Choice(options: ['piece_unique', 'plusieurs_pieces'], message: 'Merci de selectioner pièce unique ou plusieurs pièces.')]
private readonly ?string $typeCompositionLogement = null,
#[Assert\NotBlank(message: 'Merci de saisir la superficie du logement.', groups: ['LOCATAIRE', 'BAILLEUR_OCCUPANT'])]
#[Assert\Positive(message: 'Merci de saisir une information numérique dans le champs de superficie.')]
Expand All @@ -22,21 +30,70 @@ public function __construct(
#[Assert\Positive(message: 'Merci de saisir une information numérique dans le champs nombre de pièces à vivre.')]
private readonly ?string $compositionLogementNbPieces = null,
private readonly ?string $nombreEtages = null,
#[Assert\When(
expression: 'this.getType() == "appartement"',
constraints: [
new Assert\NotBlank(message: 'Merci de préciser si le logement est au rez-de-chaussée.'),
],
)]
private readonly ?string $typeLogementRdc = null,
#[Assert\When(
expression: 'this.getType() == "appartement" && this.getTypeLogementRdc() == "non"',
constraints: [
new Assert\NotBlank(message: 'Merci de préciser si le logement est au dernier étage.'),
],
)]
private readonly ?string $typeLogementDernierEtage = null,
#[Assert\When(
expression: 'this.getType() == "appartement" && this.getTypeLogementDernierEtage() == "oui"',
constraints: [
new Assert\NotBlank(message: 'Merci de préciser si le logement est sous comble et sans fenêtre.'),
],
)]
private readonly ?string $typeLogementSousCombleSansFenetre = null,
#[Assert\When(
expression: 'this.getType() == "appartement" && this.getTypeLogementRdc() == "non" && this.getTypeLogementDernierEtage() == "non"',
constraints: [
new Assert\NotBlank(message: 'Merci de préciser si le logement est au sous-sol et sans fenêtre.'),
],
)]
private readonly ?string $typeLogementSousSolSansFenetre = null,
#[Assert\NotBlank(message: 'Merci de définir si une pièce fait plus de 9m².', groups: ['LOCATAIRE', 'BAILLEUR_OCCUPANT', 'BAILLEUR', 'TIERS_PARTICULIER'])]
private readonly ?string $typeLogementCommoditesPieceAVivre9m = null,
#[Assert\NotBlank(message: 'Merci de définir si il y a une cuisine.', groups: ['LOCATAIRE', 'BAILLEUR_OCCUPANT', 'BAILLEUR', 'TIERS_PARTICULIER', 'TIERS_PRO', 'SERVICE_SECOURS'])]
private readonly ?string $typeLogementCommoditesCuisine = null,
#[Assert\When(
expression: 'this.getTypeLogementCommoditesCuisine() == "non"',
constraints: [
new Assert\NotBlank(message: 'Merci de préciser s\'il y a une cuisine collective.'),
],
)]
private readonly ?string $typeLogementCommoditesCuisineCollective = null,
#[Assert\NotBlank(message: 'Merci de définir si il y a une salle de bain.', groups: ['LOCATAIRE', 'BAILLEUR_OCCUPANT', 'BAILLEUR', 'TIERS_PARTICULIER', 'TIERS_PRO', 'SERVICE_SECOURS'])]
private readonly ?string $typeLogementCommoditesSalleDeBain = null,
#[Assert\When(
expression: 'this.getTypeLogementCommoditesSalleDeBain() == "non"',
constraints: [
new Assert\NotBlank(message: 'Merci de préciser s\'il y a une salle de bain collective.'),
],
)]
private readonly ?string $typeLogementCommoditesSalleDeBainCollective = null,
#[Assert\NotBlank(message: 'Merci de définir si il y a des WC.', groups: ['LOCATAIRE', 'BAILLEUR_OCCUPANT', 'BAILLEUR', 'TIERS_PARTICULIER', 'TIERS_PRO', 'SERVICE_SECOURS'])]
private readonly ?string $typeLogementCommoditesWc = null,
#[Assert\When(
expression: 'this.getTypeLogementCommoditesWc() == "non"',
constraints: [
new Assert\NotBlank(message: 'Merci de préciser s\'il y a des wc collectifs.'),
],
)]
private readonly ?string $typeLogementCommoditesWcCollective = null,

#[Assert\When(
expression: 'this.getTypeLogementCommoditesWc() == "oui" && this.getTypeLogementCommoditesCuisine() == "oui"',
constraints: [
new Assert\NotBlank(message: 'Merci de préciser s\'il y a des wc dans la cuisine.'),
],
)]
private readonly ?string $typeLogementCommoditesWcCuisine = null,
) {
}
Expand Down Expand Up @@ -135,4 +192,18 @@ public function getTypeLogementCommoditesWcCuisine(): ?string
{
return $this->typeLogementCommoditesWcCuisine;
}

#[Assert\Callback]
public function validate(ExecutionContextInterface $context, mixed $payload): void
{
if ($this->getTypeLogementDernierEtage() === $this->getTypeLogementRdc()) {
$context->buildViolation('Merci de bien préciser si le logement est au rez-de-chaussée ou dernier étage.')
->atPath('typeLogementRdc')
->addViolation();

$context->buildViolation('Merci de bien préciser si le logement est au rez-de-chaussée ou dernier étage.')
->atPath('typeLogementDernierEtage')
->addViolation();
}
}
}
11 changes: 9 additions & 2 deletions src/Dto/Request/Signalement/CoordonneesFoyerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ class CoordonneesFoyerRequest implements RequestInterface
{
public function __construct(
private readonly ?string $typeProprio = null,
#[Assert\When(
expression: 'this.getTypeProprio() == "ORGANISME_SOCIETE"',
constraints: [
new Assert\NotBlank(message: 'Merci de saisir un nom de structure.'),
],
)]
private readonly ?string $nomStructure = null,
#[Assert\NotBlank(message: 'Merci de selectionner une civilité.', groups: ['LOCATAIRE'])]
private readonly ?string $civilite = null,
#[Assert\NotBlank(message: 'Merci de saisir un nom.', groups: ['LOCATAIRE', 'BAILLEUR_OCCUPANT'])]
#[Assert\NotBlank(message: 'Merci de saisir un nom.', groups: ['LOCATAIRE', 'BAILLEUR_OCCUPANT', 'TIERS_PARTICULIER', 'TIERS_PRO', 'BAILLEUR'])]
#[Assert\Length(max: 50, maxMessage: 'Le nom ne doit pas dépasser {{ limit }} caractères.')]
private readonly ?string $nom = null,
#[Assert\NotBlank(message: 'Merci de saisir un prénom.', groups: ['LOCATAIRE', 'BAILLEUR_OCCUPANT'])]
#[Assert\NotBlank(message: 'Merci de saisir un prénom.', groups: ['LOCATAIRE', 'BAILLEUR_OCCUPANT', 'TIERS_PARTICULIER', 'TIERS_PRO', 'BAILLEUR'])]
#[Assert\Length(max: 50, maxMessage: 'Le prénom ne doit pas dépasser {{ limit }} caractères.')]
private readonly ?string $prenom = null,
#[Assert\NotBlank(message: 'Merci de saisir un email', groups: ['LOCATAIRE', 'BAILLEUR_OCCUPANT'])]
Expand Down
6 changes: 6 additions & 0 deletions src/Dto/Request/Signalement/CoordonneesTiersRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public function __construct(
#[AppAssert\TelephoneFormat]
private readonly ?string $telephone = null,
private readonly ?string $lien = null,
#[Assert\When(
expression: 'this.getLien() == "PRO" || this.getLien() == "SECOURS"',
constraints: [
new Assert\NotBlank(message: 'Merci de saisir un nom de structure.'),
],
)]
private readonly ?string $structure = null,
) {
}
Expand Down
10 changes: 8 additions & 2 deletions src/Dto/Request/Signalement/ProcedureDemarchesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@
class ProcedureDemarchesRequest implements RequestInterface
{
public function __construct(
#[Assert\NotBlank(['message' => 'Merci d\'indiquer si le bailleur a été averti', 'groups' => ['LOCATAIRE']])]
#[Assert\NotBlank(['message' => 'Merci d\'indiquer si le bailleur a été averti.', 'groups' => ['LOCATAIRE']])]
private readonly ?string $isProprioAverti = null,
#[Assert\NotBlank(['message' => 'Merci d\'indiquer si l\'assurance a été contactée', 'groups' => ['LOCATAIRE', 'BAILLEUR_OCCUPANT', 'BAILLEUR']])]
#[Assert\NotBlank(['message' => 'Merci d\'indiquer si l\'assurance a été contactée.', 'groups' => ['LOCATAIRE', 'BAILLEUR_OCCUPANT', 'BAILLEUR']])]
private readonly ?string $infoProcedureAssuranceContactee = null,
#[Assert\When(
expression: 'this.getInfoProcedureAssuranceContactee() == "oui"',
constraints: [
new Assert\NotBlank(message: 'Merci de noter la réponde de l\'assurance.'),
],
)]
private readonly ?string $infoProcedureReponseAssurance = null,
#[Assert\NotBlank(['message' => 'Merci d\'indiquer si l\'occupant souhaite garder son logement après travaux ', 'groups' => ['LOCATAIRE', 'BAILLEUR_OCCUPANT']])]
private readonly ?string $infoProcedureDepartApresTravaux = null,
Expand Down
12 changes: 12 additions & 0 deletions src/Dto/Request/Signalement/SituationFoyerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,23 @@ public function __construct(
#[Assert\NotBlank(['message' => 'Veuillez définir le champ allocataire', 'groups' => ['LOCATAIRE', 'BAILLEUR_OCCUPANT', 'BAILLEUR', 'TIERS_PARTICULIER', 'TIERS_PRO']])]
private readonly ?string $isAllocataire = null,
#[Assert\DateTime('Y-m-d')]
#[Assert\When(
expression: 'this.getIsAllocataire() == "oui" || this.getIsAllocataire() == "CAF" || this.getIsAllocataire() == "MSA"',
constraints: [
new Assert\NotBlank(message: 'Merci de préciser la date de naissance.'),
],
)]
private readonly ?string $dateNaissanceOccupant = null,
private readonly ?string $numAllocataire = null,
private readonly ?string $logementSocialMontantAllocation = null,
#[Assert\NotBlank(['message' => 'Veuillez définir le champ souhaite quitter le logement', 'groups' => ['LOCATAIRE', 'BAILLEUR_OCCUPANT', 'TIERS_PARTICULIER', 'TIERS_PRO']])]
private readonly ?string $travailleurSocialQuitteLogement = null,
#[Assert\When(
expression: 'this.getTravailleurSocialQuitteLogement() == "oui"',
constraints: [
new Assert\NotBlank(message: 'Merci de préciser s\'il y a un préavis de départ.'),
],
)]
private readonly ?string $travailleurSocialPreavisDepart = null,
#[Assert\NotBlank(['message' => 'Veuillez définir le champ accompagnement par un travailleur social', 'groups' => ['LOCATAIRE', 'BAILLEUR_OCCUPANT', 'TIERS_PARTICULIER', 'TIERS_PRO']])]
private readonly ?string $travailleurSocialAccompagnementDeclarant = null,
Expand Down
12 changes: 7 additions & 5 deletions src/Serializer/SignalementDraftRequestNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace App\Serializer;

use App\Dto\Request\Signalement\AdresseOccupantRequest;
use App\Dto\Request\Signalement\CompositionLogementRequest;
use App\Dto\Request\RequestInterface;
use App\Dto\Request\Signalement\SignalementDraftRequest;
use App\Entity\Model\TypeCompositionLogement;
use App\Entity\SignalementDraft;
Expand Down Expand Up @@ -48,7 +47,11 @@ public function denormalize($data, $type, $format = null, array $context = []):

$transformedData[SignalementDraftRequest::FILE_UPLOAD_KEY][$keyUpdated] = $data[$key];
} else {
$transformedData[$key] = !empty($value) ? $value : null;
if ('isProprioAverti' === $key) {
$transformedData[$key] = $value;
} else {
$transformedData[$key] = !empty($value) ? $value : null;
}
}
}

Expand Down Expand Up @@ -89,8 +92,7 @@ public function getSupportedTypes(?string $format): array
return [
SignalementDraftRequest::class => true,
TypeCompositionLogement::class => true,
CompositionLogementRequest::class => true,
AdresseOccupantRequest::class => true,
RequestInterface::class => true,
];
}
}
25 changes: 25 additions & 0 deletions src/Twig/AppExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace App\Twig;

use App\Entity\Enum\OccupantLink;
use App\Entity\Enum\ProfileDeclarant;
use App\Entity\Enum\QualificationStatus;
use App\Service\Esabora\EsaboraPartnerTypeSubscription;
use App\Service\Files\ImageBase64Encoder;
use App\Service\Notification\NotificationCounter;
use App\Service\Signalement\Qualification\QualificationStatusService;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\When;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
Expand Down Expand Up @@ -56,6 +59,28 @@ public function getFunctions(): array
new TwigFunction('can_see_nde_qualification', [QualificationStatusService::class, 'canSeenNDEQualification']),
new TwigFunction('can_see_nde_edit_zone', [QualificationStatusService::class, 'canSeenNDEEditZone']),
new TwigFunction('can_edit_esabora_credentials', [EsaboraPartnerTypeSubscription::class, 'isSubscribed']),
new TwigFunction('show_label_facultatif', [$this, 'showLabelAsFacultatif']),
];
}

public function showLabelAsFacultatif(string $class, string $field, ?ProfileDeclarant $profileDeclarant): string
{
if (null === $profileDeclarant) {
return '';
}

if ('nomStructure' === $field) {
$reflector = new \ReflectionClass($class);
$attributes = $reflector->getProperty($field)->getAttributes(When::class);
$constraints = $attributes[0]->getArguments()['constraints'] ?? [];

return \count($constraints) > 0 ? '(facultatif)' : '';
}

$reflector = new \ReflectionClass($class);
$attributes = $reflector->getProperty($field)->getAttributes(NotBlank::class);
$groups = $attributes[0]->getArguments()['groups'] ?? [];

return !empty($groups) && !\in_array($profileDeclarant->value, $groups) ? '(facultatif)' : '';
}
}
2 changes: 0 additions & 2 deletions src/Validator/TelephoneFormatValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ public function validate(mixed $value, Constraint $constraint): void
if (!$isPossible) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $value)
->atPath('telephone')
->addViolation();
}
} catch (NumberParseException $e) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $value)
->atPath('telephone')
->addViolation();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<input type="hidden" name="geolocLng" value="{% if signalement.geoloc.lng is defined %}{{ signalement.geoloc.lng }}{% endif %}">
</div>
<div class="fr-col-12 fr-col-md-6">
<h2 class="fr-h5">Complément d'adresse</h2>
<h2 class="fr-h5">Complément d'adresse (facultatif)</h2>

<div class="fr-input-group">
<label class="fr-label" for="etage">Etage
Expand Down
Loading

0 comments on commit cc99774

Please sign in to comment.