How to make translatable field hidden programatically?
Created 1 year ago by vargvinter


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



                'slug_' . $field->locale,
                ($field->value) ? str_slug($field->value) : null

            $builder->addFormError('slug_' . $field->locale, 'abc');

        if ($builder->hasFormErrors()) {

piterden  —  1 year ago

Slug doesn't support translates.

vargvinter  —  1 year ago

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.

ryanthompson  —  1 year ago

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.

vargvinter  —  1 year ago

@ryanthompson Where should I put FooFormFields? Inside Form folder? Will Pyro automagically read this file?


protected $fields => FooFormFields::class;


Any snippet?

vargvinter  —  1 year ago

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)
            'slug' => [
                'hidden' => true,
ryanthompson  —  1 year ago
ryanthompson  —  1 year ago

Hmmm... ya I see it looks like it's not being set correctly on the field. Fixing...

ryanthompson  —  1 year ago

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.

vargvinter  —  1 year ago

Right now I set classs hidden for a given field.


Works for me.

ryanthompson  —  1 year ago