Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required fields in collection #1671

Closed
phpPhil opened this issue Jun 11, 2017 · 10 comments
Closed

Required fields in collection #1671

phpPhil opened this issue Jun 11, 2017 · 10 comments

Comments

@phpPhil
Copy link

phpPhil commented Jun 11, 2017

I'm using the latest version of Symfony and Easyadmin.
In my form I have a collection with an inline form to add another domain object "GameAward". I'm defining a Symfony form to display this form.

image

The problem is that while the main form displays red asterisks to highlight required fields, this is NOT the case for the inline form fields and there is no validation happening when submitting the form!

My config:

            form:
                fields:
                   [...]
                    - { property: 'gameAwards', type: 'collection', type_options: { entry_type: 'MyVendor\MyBundle\Form\GameAwardType', by_reference: false }  }

My Form Type:

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', TextType::class, array(
            'required' => true
        ))
            ->add('description')
            ->add('type', EnumGameAwardTypeType::class)
            ->add('condition')
            ->add('ScoreBonus');
    }

I'm aware that required: true is the default of Symfony form fields, and in fact all of these fields are required and defined as nullable=false in the Doctrine Entity. However, to rule out that the issue is causes by the default behaviour as you can see I also explicitly specified it for the name field.

Is this behaviour a known limitation, a defect or am I doing something wrong?

@laurent-bientz
Copy link
Contributor

I've the same problem, I think is a known limitation, if someone has an idea to fix it?

@amustill
Copy link

amustill commented Aug 9, 2017

By design Symfony only validates the top level form. To validate an embedded form you must manually tell Symfony to do so by adding the Valid constraint to the mapped property:

use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\Valid()
 */
private $gameAwards;

And then add constraints inside your GameAward entity:

use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Assert\NotBlank()
 */
private $name;

@laurent-bientz
Copy link
Contributor

laurent-bientz commented Oct 12, 2017

@phpPhil See #1124 to solve it.

@amustill it's not related to validation, even with assert, there is no "red star" in front for required fiels in nested collection

@javiereguiluz
Copy link
Collaborator

Is this issue still relevant in the latest versions of this bundle? I have simple collections and they display the asterisk for required fields ... but I can't test it with complex collections. Thanks!

@laurent-bientz
Copy link
Contributor

Hi @javiereguiluz

Nop, still the same.

The complete case is:

  • An Entity Page with a oneToMany (Assert\Valid) to Entity Block:
/**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\OneToMany(targetEntity="PageBlock", mappedBy="page", cascade={"merge", "persist"}, fetch="EAGER")
     * @ORM\OrderBy({"createdAt" = "ASC"})
     * @Assert\Valid()
     */
    private $blocks;
  • Asserts on Block Entity fields (and non-nullable field):
/**
     * @var string
     * @ORM\Column(name="content", type="text")
     * @Assert\NotBlank(message="Vous devez renseigner le contenu.")
     */
    private $content;
  • On Page settings in yml, a simple collection with a custom FormType:
- { property: 'blocks', label: 'Blocks de contenu', type: 'collection', type_options: { entry_type: 'AppBundle\Form\Type\PageBlockType', by_reference: false } }
  • The custom FormType (event with trying to force 'required': true):
/**
 * Class PageBlockType.
 *
 * used by easyadminbundle custom bo
 */
class PageBlockType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(
                'content',
                CKEditorType::class,
                array(
                    'label' => false,
                    'required' => true,
                )
            )
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundle\Entity\PageBlock',
        ));
    }
}
  • Result:
    No red star and no HTML5 validation on nested collection (hopefully back-end validation is ok):
    ez_collection

As @HeahDude said in #1124, the only way to hack it is to add the correct classes for the label directly in the FormType:

$builder
            ->add(
                'content',
                CKEditorType::class,
                array(
                    'label' => false,
                    'required' => true,
                    'label_attr' => ['class' => 'required label-required']
                )
            )
        ;

Hope it can helps.

@grachevko
Copy link
Contributor

This solve this issue, but i still don't know the source of this problem.

form:
    fields:
        - { property: '...', type: 'collection', type_options: { required: true } }

@javiereguiluz
Copy link
Collaborator

I'm closing this issue because we're starting a new phase in the history of this bundle (see #2059). We've moved it into a new GitHub organization and we need to start from scratch: no past issues, no pending pull requests, etc.

I understand if you are angry or disappointed by this, but we really need to "reset" everything in order to reignite the development of this bundle.

@ebuildy
Copy link

ebuildy commented Apr 18, 2019

As you can see in Symfony code source (Form.php):

    /**
     * {@inheritdoc}
     */
    public function isRequired()
    {
        if (null === $this->parent || $this->parent->isRequired()) {
            return $this->config->getRequired();
        }

        return false;
    }

This means you must set required to true like this:

- { property: rules, type: 'collection', type_options: {required: true, entry_type: 'App\Form\OfferRuleFormType', by_reference: false}  }

@Arianne97
Copy link

Hello ! I'm using easyadmin 3 and i have a same project than this one. But I can't embed my forms. please can you show me how you did it ?
symfony

@trikess
Copy link
Contributor

trikess commented Jul 27, 2021

Example

    use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
   
   
    $property = CollectionField::new('property')
            ->allowAdd()
            ->allowDelete()
            ->setEntryIsComplex(true)
            ->setEntryType(PropertyType::class)
            ->setFormTypeOptions([
                'by_reference' => false,
            ]);

PropertyType is a custom class to generate subform

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants