How to make translatable field hidden programatically?
Created 6 years ago by vargvinterHello,
In my custom form handler I am generating slug automatically. Initially I've added slug field to skips array in FormBuilder.
Then in FormHandler I create slug fields programatically and If something goes wrong forms redirects back. Great so far. But I want to have slug field hidden. User should not see this field if validation fails and redirects him back. Below code does not hide slug field.
$field->setHidden(true);
works for NOT translatable field. How to make it work for my translatable slug field?
class FooHandler
{
public function handle(FieldFactory $factory, MyFormBuilder $builder)
{
foreach ($builder->getFormFields() as $field) {
$field = $factory->make([
'field' => 'slug',
'type' => 'anomaly.field_type.text',
'translatable' => true,
'locale' => $field->locale
]);
$field->setHidden(true);
$builder->addFormField($field);
$builder->setFormValue(
'slug_' . $field->locale,
($field->value) ? str_slug($field->value) : null
);
$builder->addFormError('slug_' . $field->locale, 'abc');
}
if ($builder->hasFormErrors()) {
return;
}
$builder->saveForm();
}
}
I know. The field I have added has anomaly.field_type.text
type and I have provided custom slug generation in the handler. I only have a problem with hiding this translatable text field when validation fails and form redirects a user back.
By the time you are hiding the field here - it's redirecting back and losing it's configuration.
I would use a handler for your fields - FooFormFields
and if $builder->formHasErrors()
(from previous submission) then add the hidden attribute to that field.
@ryanthompson Where should I put FooFormFields
? Inside Form
folder? Will Pyro automagically read this file?
Or:
protected $fields => FooFormFields::class;
?
Any snippet?
I have created class FooFormFields
in Form folder. Looks like Pyro loads it automagically.
Unfortunately hidden
attribute does not work for translatable field.
class FooFormFields
{
public function handle(ArchitectFormBuilder $builder)
{
$builder->setFields([
'*',
'slug' => [
'hidden' => true,
]
]);
}
}
Is this not working then in the wrapper view? Or not being hit?
Hmmm... ya I see it looks like it's not being set correctly on the field. Fixing...
Well.. we're closer on this. I pushed up an update yesterday that allows you to properly hide translated fields but.. I think we need something else cause the JS on page load causes the active locale field to display the hidden field (which is normal).
Still thinking about it we might need a different flag.
Excellent!
Slug doesn't support translates.