简体   繁体   中英

Sonata Admin strange form behaviour

I've started a game using Symfony 4.x and I decided to use traits to avoid code duplicate.

namespace App\Traits;

use App\Entity\Brand;

/**
 * Trait ToolTrait
 * @package App\Traits
 */
trait ToolTrait
{
    /**
     * @var null|int
     */
    private $id;

    /**
     * @var null|Brand
     */
    private $brand;

    /**
     * @var null|string
     */
    private $name;

    /**
     * @var null|string
     */
    private $image;

    /**
     * @var float
     */
    private $moneyPrice = 0.00;

    /**
     * @var int
     */
    private $gemsPrice = 0;

    /**
     * @var null|float
     */
    private $maintenance = 0.00;

    /**
     * @var boolean
     */
    private $limited = false;

    /**
     * @var int
     */
    private $leftSales = 0;

    /**
     * @var int
     */
    private $totalSales = 0;

    /**
     * @var bool
     */
    private $enabled = true;

    /**
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * @param int|null $id
     *
     * @return ToolTrait
     */
    public function setId(?int $id): self
    {
        $this->id = $id;

        return $this;
    }

    /**
     * @return Brand|null
     */
    public function getBrand(): ?Brand
    {
        return $this->brand;
    }

    /**
     * @param Brand|null $brand
     *
     * @return ToolTrait
     */
    public function setBrand(?Brand $brand): self
    {
        $this->brand = $brand;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getName(): ?string
    {
        return $this->name;
    }

    /**
     * @param null|string $name
     *
     * @return ToolTrait
     */
    public function setName(?string $name): self
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return null|string
     */
    public function getImage(): ?string
    {
        return $this->image;
    }

    /**
     * @param null|string $image
     *
     * @return ToolTrait
     */
    public function setImage(?string $image): self
    {
        $this->image = $image;

        return $this;
    }

    /**
     * @return float|null
     */
    public function getMoneyPrice(): ?float
    {
        return $this->moneyPrice;
    }

    /**
     * @param float|null $moneyPrice
     *
     * @return ToolTrait
     */
    public function setMoneyPrice(?float $moneyPrice): self
    {
        $this->moneyPrice = $moneyPrice;

        return $this;
    }

    /**
     * @return int|null
     */
    public function getGemsPrice(): ?int
    {
        return $this->gemsPrice;
    }

    /**
     * @param int|null $gemsPrice
     *
     * @return ToolTrait
     */
    public function setGemsPrice(?int $gemsPrice): self
    {
        $this->gemsPrice = $gemsPrice;

        return $this;
    }

    /**
     * @return float|null
     */
    public function getMaintenance(): ?float
    {
        return $this->maintenance;
    }

    /**
     * @param float|null $maintenance
     *
     * @return ToolTrait
     */
    public function setMaintenance(?float $maintenance): self
    {
        $this->maintenance = $maintenance;

        return $this;
    }

    /**
     * @return bool
     */
    public function isLimited(): bool
    {
        return $this->limited;
    }

    /**
     * @param bool $limited
     *
     * @return ToolTrait
     */
    public function setLimited(bool $limited): self
    {
        $this->limited = $limited;

        return $this;
    }

    /**
     * @return int
     */
    public function getLeftSales(): int
    {
        return $this->leftSales;
    }

    /**
     * @param int $leftSales
     *
     * @return ToolTrait
     */
    public function setLeftSales(int $leftSales): self
    {
        $this->leftSales = $leftSales;

        return $this;
    }

    /**
     * @return int
     */
    public function getTotalSales(): int
    {
        return $this->totalSales;
    }

    /**
     * @param int $totalSales
     *
     * @return ToolTrait
     */
    public function setTotalSales(int $totalSales): self
    {
        $this->totalSales = $totalSales;

        return $this;
    }

    /**
     * @return bool
     */
    public function isEnabled(): bool
    {
        return $this->enabled;
    }

    /**
     * @param bool $enabled
     *
     * @return ToolTrait
     */
    public function setEnabled(bool $enabled): self
    {
        $this->enabled = $enabled;

        return $this;
    }

    /**
     * @return null|string
     */
    public function __toString()
    {
        return $this->name ?? '-';
    }
}

This is my trait . I didn't add Doctrine mapping here. For this, I created one file for each of my classes and I set the mapping there ( XML ). Mapping is good because there's no error and I checked it manually using HeidiSQL .

形成 验证器

The problem is when I submit the form. Some of my properties are considered part of the entity, some not. It's strange because I don't have any children and the entity only implements this trait. Any idea?

What's really strange is that 'fileTmp' property is loaded from another trait, but this it hasn't mapping (only used for loading images).

I must say that for those fields I have a custom field type implemented based on Symfony documentation. If I remove that, everything's normal.

<?php

namespace App\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
 * Class HorsePowerType
 * @package App\Form\Type
 */
class HorsePowerType extends AbstractType
{
    /** {@inheritdoc} */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        $view->vars['icon'] = 'HP';
    }

    /** {@inheritdoc} */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'error_bubbling' => false
        ]);
    }

    /** {@inheritdoc} */
    public function getBlockPrefix()
    {
        return 'horse_power';
    }
}

And the template:

{% block horse_power_widget %}
    {% spaceless %}
        <div class="input-group">
            {{ block('form_widget_simple') }}
            <span class="input-group-addon">{{- icon|raw -}}</span>
        </div>
    {% endspaceless %}
{% endblock %}

PS: The only extra fields that are in this entity are: horsePower, fuelTank, maxSpeed, maxCapacity.

我通过在字段类型中添加compound => falseconfigureOptions来解决。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM