How to repopulate form with translatable fields on validation error/exception?
Created 6 years ago by vargvinterHello,
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'),
...
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?
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'),
]);
}
}
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'
]
];
@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
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
Thank you @ryanthompson master of pyro. 😄 I am just learning this CMS and I think I will have more questions.
@vargvinter my pleasure! Glad to have you here 😊
@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