Skip to content

Commit

Permalink
fix: fixed driving licenses (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasstislav authored Mar 25, 2024
1 parent 0b929f2 commit a23c0ba
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/Entity/EuropeanCVDrivingLicense.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class EuropeanCVDrivingLicense
'comment' => 'Distance traveled in kilometers'
],
)]
private ?string $distanceTraveled = null;
private ?int $distanceTraveled = null;

#[ORM\Column(type: 'boolean')]
private ?bool $activeDriver = null;
Expand Down Expand Up @@ -69,12 +69,12 @@ public function setDrivingLicense(?DrivingLicenseEnum $drivingLicense): self
return $this;
}

public function getDistanceTraveled(): ?string
public function getDistanceTraveled(): ?int
{
return $this->distanceTraveled;
}

public function setDistanceTraveled(?string $distanceTraveled): self
public function setDistanceTraveled(?int $distanceTraveled): self
{
$this->distanceTraveled = $distanceTraveled;

Expand Down
16 changes: 7 additions & 9 deletions src/Form/Parts/EuropeanCVPartDrivingLicenseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Trexima\EuropeanCvBundle\Entity\EuropeanCV;
use Trexima\EuropeanCvBundle\Form\Type\DrivingLicenseType;
Expand All @@ -22,13 +24,6 @@ class EuropeanCVPartDrivingLicenseType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$entity = $builder->getData();

$drivingLicenses = [];
foreach ($entity->getDrivingLicenses() as $drivingLicense) {
$drivingLicenses[] = $drivingLicense->getDrivingLicense()->value;
}

$builder
->add('drivingLicenseOwner', CheckboxType::class, [
'required' => false,
Expand All @@ -53,11 +48,14 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
]
],
'required' => false,
'hidden' => empty($entity?->getDrivingLicenseOwner()),
'existing_licenses' => $drivingLicenses,
]);
}

public function finishView(FormView $view, FormInterface $form, array $options)
{
$view->children['drivingLicenses']->vars['hidden'] = !$form->getData()?->getDrivingLicenseOwner();
}

/**
* {@inheritdoc}
*/
Expand Down
2 changes: 0 additions & 2 deletions src/Form/Type/DrivingLicenseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'attr' => [
'class' => 'form-inline'
],
'existing_licenses' => $options['existing_licenses']
], $options['entry_options'])
);
}
Expand Down Expand Up @@ -122,7 +121,6 @@ public function configureOptions(OptionsResolver $resolver)
'entry_options' => [
'required' => false,
],
'existing_licenses' => []
]);
}

Expand Down
15 changes: 12 additions & 3 deletions src/Form/Type/EuropeanCVDrivingLicenseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Trexima\EuropeanCvBundle\Entity\Enum\DrivingLicenseEnum;
use Trexima\EuropeanCvBundle\Entity\EuropeanCVDrivingLicense;

Expand Down Expand Up @@ -44,12 +45,13 @@ public function buildForm(FormBuilderInterface $builder, array $options)
'required' => false, 'attr' => [
'placeholder' => t('trexima_european_cv.form_label.driving_license_distance_traveled_placeholder', [], 'trexima_european_cv')
],
'hidden' => !in_array($drivingLicense->value, $options['existing_licenses'])
'constraints' => [
new Assert\Range(min: 0, max: 2 ** 32 - 1),
],
])
->add('activeDriver', CheckboxType::class, [
'label' => t('trexima_european_cv.form_label.driving_license_active_driver_label', [], 'trexima_european_cv'),
'required' => false,
'hidden' => !in_array($drivingLicense->value, $options['existing_licenses'])
])
;

Expand Down Expand Up @@ -82,6 +84,14 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars['driving_license'] = $options['driving_license'];
}

public function finishView(FormView $view, FormInterface $form, array $options)
{
$drivingLicense = $form->getData()?->getDrivingLicense();

$view->children['distanceTraveled']->vars['hidden'] = !$drivingLicense;
$view->children['activeDriver']->vars['hidden'] = !$drivingLicense;
}

/**
* {@inheritdoc}
*/
Expand All @@ -90,7 +100,6 @@ public function configureOptions(OptionsResolver $resolver)
parent::configureOptions($resolver);
$resolver->setDefaults([
'data_class' => EuropeanCVDrivingLicense::class,
'existing_licenses' => []
]);

$resolver->setRequired([
Expand Down

0 comments on commit a23c0ba

Please sign in to comment.