vargvinter - 10 months ago

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'),
...

Answer

ryanthompson - 10 months ago

@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 - 10 months 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 - 10 months 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 - 10 months 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 - 10 months 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 - 10 months ago

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

ryanthompson - 10 months ago

@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 - 10 months 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 - 10 months ago

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

ryanthompson - 10 months ago

@vargvinter my pleasure! Glad to have you here ^_^