lucas.sanner54070
lucas.sanner54070

In my editing form there are specific cases where some fields should be hidden.

public function filterFields($fields, $context = null)
{
   if (condition) {
     $fields->my_field->hidden = true;
   }
}

The field disappears from the form as expected but the field is also removed from the source code.
As a result, when the form is saved, the field value is lost and a null value is applied instead.
How can I hide my fields without being removed from the source code ?

Last updated

mjauvin
mjauvin

Can you show your actual filter fields method so we can understand better?

lucas.sanner54070
lucas.sanner54070

Here it is:

public function filterFields($fields, $context = null)
{   
    if ($this->parent && $this->parent->attributes['status'] == 'unpublished') {
        $fields->status->hidden = true;
        $fields->parent->hidden = true;
    }   
    elseif ($this->parent && $this->parent->attributes['status'] == 'published' && $this->status == 'unpublished') {
        $fields->parent->hidden = true;
    }   
}
mjauvin
mjauvin

Maybe use the cssClass field option to hide it?

https://octobercms.com/docs/backend/forms#form-field-options

mjauvin
mjauvin

like:

$field->cssClass = "hidden";

Last updated

mjauvin
lucas.sanner54070
lucas.sanner54070

Already did. Same result...
Could you try it in one of your plugins, just to make sure ?

Last updated

mjauvin
mjauvin

Which method leads to the same result? Can you share the code?

lucas.sanner54070
lucas.sanner54070

In my fields.yaml

 status:
     label: Status
     type: dropdown
     span: left
     hidden: true
mjauvin
mjauvin

but that's not what I suggested above...

$field->cssClass = "hidden";
lucas.sanner54070
lucas.sanner54070

Sorry I skipped your post.
Yes it works. Actually in my case:

$fields->status->cssClass = 'hidden'; 

But I find the "hidden" attribute misleading. I was expecting it to modify the type of the field

<input  type="hidden"  name="Post[my_field]" value="hello" />

1-11 of 11