improves create a person with address

* validation: must have an address when 'create a form' is checked;
* minor improvements
This commit is contained in:
Julien Fastré 2022-05-06 11:43:05 +02:00
parent 64432bb08a
commit 6abbf9bf21
6 changed files with 56 additions and 30 deletions

View File

@ -36,33 +36,35 @@
{# Flash messages ! #}
{% if app.session.flashbag.keys()|length > 0 %}
<div class="col-8 mb-5 flash_message">
<div class="row justify-content-center">
<div class="col-10 mb-5 flash_message">
{% for flashMessage in app.session.flashbag.get('success') %}
<div class="alert alert-success flash_message">
<span>{{ flashMessage|raw }}</span>
</div>
{% endfor %}
{% for flashMessage in app.session.flashbag.get('error') %}
<div class="alert alert-danger flash_message">
<span>{{ flashMessage|raw }}</span>
</div>
{% endfor %}
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="alert alert-warning flash_message">
<span>{{ flashMessage|raw }}</span>
</div>
{% endfor %}
{% for flashMessage in app.session.flashbag.get('success') %}
<div class="alert alert-success flash_message">
<span>{{ flashMessage|raw }}</span>
</div>
{% endfor %}
{% for flashMessage in app.session.flashbag.get('error') %}
<div class="alert alert-danger flash_message">
<span>{{ flashMessage|raw }}</span>
</div>
{% endfor %}
{% for flashMessage in app.session.flashbag.get('notice') %}
<div class="alert alert-warning flash_message">
<span>{{ flashMessage|raw }}</span>
</div>
{% endfor %}
</div>
</div>
{% endif %}
{% block content %}
<div class="col-8 main_search">
<h2>{{ 'Search'|trans }}</h2>
<form action="{{ path('chill_main_search') }}" method="get">
<input class="form-control form-control-lg" name="q" type="search" placeholder="{{ 'Search persons, ...'|trans }}" />
<center>
@ -75,11 +77,11 @@
</center>
</form>
</div>
{# DISABLED {{ chill_widget('homepage', {} ) }} #}
{% include '@ChillMain/Homepage/index.html.twig' %}
{% endblock %}
</div>

View File

@ -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(),
]);
}
}

View File

@ -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');
}

View File

@ -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();
}
}
}

View File

@ -111,7 +111,6 @@
{{ form_row(form.addressForm) }}
</div>
<div id=address>
<p>{{ 'A new household will be created. The person will be member of this household.'|trans }}</p>
{{ form_row(form.address) }}
</div>
@ -148,4 +147,4 @@
{% block css %}
{{ encore_entry_link_tags('mod_input_address') }}
{% endblock %}
{% endblock %}

View File

@ -65,4 +65,7 @@ The person where the course is located must be associated to the course. Change
#relationship
relationship:
duplicate: Une relation de filiation existe déjà entre ces 2 personnes
duplicate: Une relation de filiation existe déjà entre ces 2 personnes
person_creation:
If you want to create an household, an address is required: Pour la création d'un ménage, une adresse est requise