src/Form/Type/CarPricingType.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type;
  3. use App\Entity\Profile\CarPricing;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\DataMapperInterface;
  6. use Symfony\Component\Form\Exception;
  7. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  8. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  9. use Symfony\Component\Form\FormBuilderInterface;
  10. use Symfony\Component\Form\FormInterface;
  11. use Symfony\Component\OptionsResolver\OptionsResolver;
  12. use Symfony\Component\Validator\Constraints\GreaterThan;
  13. class CarPricingType extends AbstractType implements DataMapperInterface
  14. {
  15.     /**
  16.      * @inheritDoc
  17.      */
  18.     public function buildForm(FormBuilderInterface $builder, array $options): void
  19.     {
  20.         $builder->add('provided'CheckboxType::class, [
  21.             'required' => false,
  22.         ]);
  23.         $builder->setDataMapper($this);
  24.     }
  25.     /**
  26.      * @inheritDoc
  27.      */
  28.     public function configureOptions(OptionsResolver $resolver): void
  29.     {
  30.         $resolver->setDefaults([
  31.             'data_class' => CarPricing::class,
  32.             'empty_data' => null,
  33.         ]);
  34.     }
  35.     /**
  36.      * @inheritDoc
  37.      */
  38.     public function mapDataToForms($viewData$forms)
  39.     {
  40.         if (null === $viewData) {
  41.             return null;
  42.         }
  43.         if (!$viewData instanceof CarPricing) {
  44.             throw new Exception\UnexpectedTypeException($viewDataCarPricing::class);
  45.         }
  46.         /** @var FormInterface[] $forms */
  47.         $forms iterator_to_array($forms);
  48.         $forms['provided']->setData($viewData->isProvided());
  49.     }
  50.     /**
  51.      * @inheritDoc
  52.      */
  53.     public function mapFormsToData($forms, &$viewData): void
  54.     {
  55.         /** @var FormInterface[] $forms */
  56.         $forms iterator_to_array($forms);
  57.         $viewData = new CarPricing(
  58.             $forms['provided']->getData()
  59.         );
  60.     }
  61. }