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

SonataAdminBundle - Form validation not performed when data is changed on PRE_SUBMIT or POST_SET_DATA #2273

Closed
ethernal opened this issue Aug 6, 2014 · 3 comments

Comments

@ethernal
Copy link

ethernal commented Aug 6, 2014

I am trying to validate computed field that is referenced as title below. The field's content is built from parts of two other entities ([entity1]Document->getName and [entity2]DocumentRevision->getRevisionNmber).

Main issue is that although UniqeEntity constraint is used on the title field and modification is done on PRE_SUBMIT or on POST_SET_DATA (and validation should be performed on SUBMIT event SonataAdmin does not return form validation error, but tries to enter data to the database invoking DB layer error on Unique constraint.

$builder = $formMapper->getFormBuilder();
        $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
            $data = $event->getData();
            $form = $event->getForm();

            if (!$data) {
                return;
            }

            $data['title'] = $data['document'] . ' wyd. ' . $data['documentRevision'];
            $event->setData($data);
        }

        );

Second issue is that I can't/dont know how to get data of related entities (to generate the title field name) as using callbacks to entity cause errors (running method on non object).

For more details please refer to this topic on stack overflow: http://stackoverflow.com/questions/25135031/symfony-2-change-entitys-field-from-admin-while-keeping-validation-using-sona

Also this issue seems to be related to #1016 about EventSubscriber.

If there is a different way to keep fields validated and make them computed at runtime please let me know I'm very eager to know of a way (with details please).

@webdevilopers
Copy link
Contributor

Regarding entity data maybe this will help you to get the subject:
https://gist.github.com/webdevilopers/fef9e296e77bb879d138

Then you could use getters to get the desired data for instance:

protected function configureFormFields(FormMapper $formMapper)
{
    $subject = $this->getSubject();

    $formMapper->getFormBuilder()->addEventListener(FormEvents::PRE_SET_DATA,
        function (FormEvent $event) use ($subject) { 
            $document = $subject->getDocument();

            // ...
    });
}

@webdevilopers
Copy link
Contributor

Close @ethernal ?

@ethernal
Copy link
Author

It can be closed. I have used LifecycleCallbacks and for some reason it works :-). I am trying to do the same now for other Entity, but it's not working as expected.

PS. this should work on DB level not on Form level as I want this to functionality to work even when I'm not using form directly (or through a factory).

/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function setTitleFromDocumentName()
{
        $this->setTitle($this->getDocument()->getName()." rev. ". $this->getDocumentRevision()->getRevisionNumber());
}

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

No branches or pull requests

2 participants