Skip to content

Commit

Permalink
Merge a7e60d3 into aa0ee6e
Browse files Browse the repository at this point in the history
  • Loading branch information
pierredup committed Oct 31, 2023
2 parents aa0ee6e + a7e60d3 commit a5c6545
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/ClientBundle/Form/Type/ContactDetailType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace SolidInvoice\ClientBundle\Form\Type;

use SolidInvoice\ClientBundle\Entity\ContactType as ContactTypeEntity;
use SolidInvoice\ClientBundle\Repository\ContactTypeRepository;
use SolidInvoice\CoreBundle\Form\DataTransformer\EntityUuidTransformer;
use Symfony\Component\Form\AbstractType;
Expand Down Expand Up @@ -53,6 +54,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'attr' => [
'class' => 'input-group-select-val',
],
'constraints' => [
new NotBlank(['groups' => 'not_blank_type', 'message' => 'The contact detail type cannot be empty']),
],
]
)
->addModelTransformer(new EntityUuidTransformer($this->contactTypeRepository->findAll()))
Expand All @@ -75,7 +79,13 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('validation_groups', function (FormInterface $form) {
// @codeCoverageIgnoreStart
$type = $form->get('type')->getData()->getName();
$contactDetailsType = $form->get('type')->getData();

if (! $contactDetailsType instanceof ContactTypeEntity) {
return ['Default', 'not_blank_type'];
}

$type = $contactDetailsType->getName();
$value = $form->get('value')->getData();

if (! empty($type) && empty($value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</a>
{% endfor %}
</div>
{{ form_widget(form.type) }}
{{ form_row(form.type) }}
</div>
{{ form_widget(form.value) }}
<span class="input-group-btn">
Expand All @@ -38,6 +38,7 @@
</div>

<div class="form-group has-error">
{{ form_errors(form.type) }}
{{ form_errors(form.value) }}
{{ form_errors(form) }}
</div>
5 changes: 3 additions & 2 deletions src/ClientBundle/Resources/views/Form/contacts.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@
{{ 'client.contact.details.extra'|trans }}
</h5>

{% set total = form.additionalContactDetails.children|length %}
{% set total = form.additionalContactDetails|length %}
<div class="prototype-widget client_contacts" data-counter="{{ total > 0 ? total : 0 }}">
{% for widget in form.additionalContactDetails.children %}
{% for widget in form.additionalContactDetails %}
{{ form_row(widget, {'child' : true}) }}
{% endfor %}

{{ additional_details|replace({'__contact_details_prototype__' : total})|raw }}
</div>

{{ form_errors(form.additionalContactDetails) }}
{{ form_errors(form) }}

{% if allow_delete %}
Expand Down
5 changes: 3 additions & 2 deletions src/CoreBundle/Form/DataTransformer/EntityUuidTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace SolidInvoice\CoreBundle\Form\DataTransformer;

use Ramsey\Uuid\UuidInterface;
use SolidInvoice\ClientBundle\Entity\ContactType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use function method_exists;
Expand Down Expand Up @@ -41,7 +42,7 @@ public function __construct(array $types)
*/
public function transform($value): ?UuidInterface
{
if (is_object($value) && method_exists($value, 'getId')) {
if ($value instanceof ContactType) {
return $value->getId();
}

Expand All @@ -57,7 +58,7 @@ public function transform($value): ?UuidInterface
*/
public function reverseTransform($value): ?object
{
if ('' === $value) {
if ('' === $value || null === $value) {
return null;
}

Expand Down

0 comments on commit a5c6545

Please sign in to comment.