diff --git a/src/Bundle/ChillMainBundle/Resources/views/layout.html.twig b/src/Bundle/ChillMainBundle/Resources/views/layout.html.twig index c11422a1b..f59e0aaff 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/layout.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/layout.html.twig @@ -36,33 +36,35 @@ {# Flash messages ! #} {% if app.session.flashbag.keys()|length > 0 %} -
diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonController.php b/src/Bundle/ChillPersonBundle/Controller/PersonController.php index ca5de4ef0..a4a29216c 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonController.php @@ -252,7 +252,7 @@ final class PersonController extends AbstractController $this->lastPostDataReset(); $address = $form->get('address')->getData(); - $addressForm = $form->get('addressForm')->getData(); + $addressForm = (bool) $form->get('addressForm')->getData(); if (null !== $address && $addressForm) { $household = new Household(); @@ -271,7 +271,7 @@ final class PersonController extends AbstractController if ($form->get('createHousehold')->isClicked()) { return $this->redirectToRoute('chill_person_household_members_editor', [ 'persons' => [$person->getId()], - 'household' => $household->getId() + 'household' => $household->getId(), ]); } } diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php b/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php index e738528c9..2de526ee6 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php @@ -35,7 +35,7 @@ class HouseholdMember /** * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) * @Serializer\Groups({"read", "docgen:read"}) - * @Assert\GreaterThan( + * @Assert\GreaterThanOrEqual( * propertyPath="startDate", * message="household_membership.The end date must be after start date", * groups={"household_memberships"} @@ -202,7 +202,7 @@ class HouseholdMember public function setPosition(?Position $position): self { - if ($this->position instanceof Position) { + if ($this->position instanceof Position && $this->position !== $position) { throw new LogicException('The position is already set. You cannot change ' . 'a position of a membership'); } diff --git a/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php b/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php index 0d854b681..124db26eb 100644 --- a/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php +++ b/src/Bundle/ChillPersonBundle/Form/CreationPersonType.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Form; +use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Form\Event\CustomizeFormEvent; use Chill\MainBundle\Form\Type\ChillDateType; use Chill\MainBundle\Form\Type\ChillPhoneNumberType; @@ -30,6 +31,8 @@ use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; +use Symfony\Component\Validator\Constraints\Callback; +use Symfony\Component\Validator\Context\ExecutionContextInterface; final class CreationPersonType extends AbstractType { @@ -84,10 +87,12 @@ final class CreationPersonType extends AbstractType 'label' => 'Create a household and add an address', 'required' => false, 'mapped' => false, + 'help' => 'A new household will be created. The person will be member of this household.', ]) ->add('address', PickAddressType::class, [ 'required' => false, 'mapped' => false, + 'label' => false, ]); if ($this->askCenters) { @@ -114,6 +119,9 @@ final class CreationPersonType extends AbstractType { $resolver->setDefaults([ 'data_class' => Person::class, + 'constraints' => [ + new Callback([$this, 'validateCheckedAddress']), + ], ]); } @@ -124,4 +132,18 @@ final class CreationPersonType extends AbstractType { return self::NAME; } + + public function validateCheckedAddress($data, ExecutionContextInterface $context, $payload): void + { + /** @var bool $addressFrom */ + $addressFrom = $context->getObject()->get('addressForm')->getData(); + /** @var ?Address $address */ + $address = $context->getObject()->get('address')->getData(); + + if ($addressFrom && null === $address) { + $context->buildViolation('person_creation.If you want to create an household, an address is required') + ->atPath('addressForm') + ->addViolation(); + } + } } diff --git a/src/Bundle/ChillPersonBundle/Resources/views/Person/create.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/Person/create.html.twig index e3631f119..03bece4d5 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/Person/create.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/Person/create.html.twig @@ -111,7 +111,6 @@ {{ form_row(form.addressForm) }}{{ 'A new household will be created. The person will be member of this household.'|trans }}
{{ form_row(form.address) }}