A
A
Alexander2021-10-27 19:39:27
symfony
Alexander, 2021-10-27 19:39:27

How to save/get the value between form submission attempts?

Symfony project in which the Sylius plugin is used
There is a standard entity Customer, the creation of which needs to be redefined depending on what type of this customer
If isCompany - additional fields are added The

resource controller of the vendor /admin/customers/new is
used I added a qveri parameter and try to process this matter in CustomerProfileTypeExtension
Something like /admin/customers/new?isCompany=1

In the form builder I add an event listener and use the isCompany value to manage the form

The problem is that when I submit the form, it redirects to the standard resource url /admin/customers/new
And my get parameter is lost and I can't properly validate the form
Nagovnokodil such a method that checks

private function resolveCustomerType(FormEvent $event): ?bool {
        $customerType = $_GET['isCompany'] ?? null;
        $referer = $_SERVER['HTTP_REFERER'] ?? null;

        if ($customerType !== null) {
            return (bool) $_GET['isCompany'];
        }

        if ($referer !== null) {
            $query = parse_url($referer, PHP_URL_QUERY);
            if ($query) {
                return (bool) explode('=', $query)[1];
            }
        }
         // $form->get('isCompany')->getData() === NULL
        // $event->getData()->getIsCompany() === NULL
        // здесь никак не получается достать раннее установленное значение isCompany
        return null;
    }


As long as the referrer is available, I can get the value,
but in case the form contained errors, after resubmitting , I can’t get the value I need, because the submission took place from the /admin/customers/new route and the referrer is no longer available.

To summarize, it turns out such nonsense:
1) An empty form opens /admin/customers/new?isCompany=1
2) After submitting a redirect to /admin/customers/new - but isCompany can still be obtained from the referrer
3) After resubmission, the isCompany value is no longer available

There is an option to override controller
sylius_admin_customer_create.company:
    path: /admin/customers/new/company
    methods: [GET, POST]
    defaults:
        _controller: sylius.controller.customer:createAction
        _sylius:
            section: admin
            redirect: sylius_admin_customer_index
            template: "@SyliusAdmin/Crud/create.html.twig"
            form: App\Form\Type\CustomerCompanyType
            vars:
                templates:
                    form: "@SyliusAdmin/Customer/_form.company.html.twig"

But in this case, a similar picture is obtained
. The form opens normally, after I try to send it, the redirect to /admin/customers/new occurs again and I cannot process errors

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Derepko, 2021-10-28
@uDenX

https://symfony.com/doc/current/forms.html#process...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question