How to repopulate form with translatable fields on validation error/exception?
Created 6 years ago by vargvinter

Hello,

New day, new challenges with Pyro. 😄

Check out this code:

    /**
     * Create a new entry.
     *
     * @param TranslationFormBuilder $form
     * @param MessageBag $message
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function create(TranslationFormBuilder $form, MessageBag $message)
    {
        try {

            return $form->render();

        } catch (QueryException $e) {
            $message->error('lizard.module.translations::validation.unique_translation');

            return redirect()->back()->with([
                'key' => request()->get('key'),
                'value' => ???
                'category' => request()->get('category'),
            ]);
        }
    }

I have unique key on key and category fields in DB. If QueryException (code 1062) is thrown I want to go back and repopulate form fields. The problem is with translatable field (called value). How to repopulate this field?

The following code does not work.


...
'value_pl' => request()->get('value_pl'),
'value_en' => request()->get('value_en'),
...
ryanthompson  —  6 years ago Best Answer

@vargvinter ya the form repopulates itself automatically when validation fails so you should just use validators like you came to.

However if you ever need to repopulate the form manually for any reason you could try the following command: https://github.com/anomalylabs/streams-platform/blob/1.3/src/Ui/Form/Command/RepopulateFields.php

piterden  —  6 years ago

Well, I don't get what you want, but don't use try/catch in the controller.

Then, you don't need to inject MessageBag, cause it already injected. Check this out vendor/anomaly/streams-platform/src/Http/Controller/BaseController.php#L129.

What you mean TranslationFormBuilder? What is its purpose?

piterden  —  6 years ago

Also code could be highlighted with the same way, like on GitHub.

    /**
     * Create a new entry.
     *
     * @param TranslationFormBuilder $form
     * @param MessageBag $message
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function create(TranslationFormBuilder $form, MessageBag $message)
    {
        try {

            return $form->render();

        } catch (QueryException $e) {
            $message->error('lizard.module.translations::validation.unique_translation');

            return redirect()->back()->with([
                'key' => request()->get('key'),
                'value' => ???
                'category' => request()->get('category'),
            ]);
        }
    }
piterden  —  6 years ago

Also, the create() method is only for render a form page. It shouldn't store something in DB!!! You would definitely need to check name attributes of a form HTML elements in your browser.

vargvinter  —  6 years ago

Nevermind, I got serious brain freeze. I have created custom validation rule and now all is working fine. Controller is now clean as it was originally without try catch.

Thanks for a tip with MessageBag.

Bonus question: May I reference variables in FormBuilder in the $rules array? For example I want id of the edited entry.

    /**
     * Additional validation rules.
     *
     * @var array|string
     */
    protected $rules = [
        'key' => [
            'unique_translation:category'
        ]
    ];
piterden  —  6 years ago

vendor/anomaly/streams-platform/docs/en/04.ui/05.forms.md

ryanthompson  —  6 years ago Best Answer

@vargvinter ya the form repopulates itself automatically when validation fails so you should just use validators like you came to.

However if you ever need to repopulate the form manually for any reason you could try the following command: https://github.com/anomalylabs/streams-platform/blob/1.3/src/Ui/Form/Command/RepopulateFields.php

ryanthompson  —  6 years ago

To use variables in the rules property you will need to use a rules handler (same as any other property handler). FooBarFormRules for example with a handle command that accepts the FooBarFormBuilder $builder and anything else you need to inject. Similar to a fields handler for example: https://github.com/anomalylabs/streams-platform/blob/1.3/src/Stream/Form/StreamFormFields.php

vargvinter  —  6 years ago

Thank you @ryanthompson master of pyro. 😄 I am just learning this CMS and I think I will have more questions.

ryanthompson  —  6 years ago

@vargvinter my pleasure! Glad to have you here 😊