This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

hai1533
hai1533

How do you show a backend form field based on a boolean condition?

Daniel81
Daniel81

You might need a bit if Jquery to listen for an onChange event, then show/hide the relevant fields. There could be a simpler way, I'd be interested to know also if there is :)

Astucieux
Astucieux

Hi, in a plugin, there are three ways to do it quite easily :

First solution

Put a disabled condition in the models/your_object_model/fields.yml configuration file of your object's model, like :

# in models/your_object_model/fields.yml
your_field_name:
     label: Name of the field to test
     type: (type of your field)
     disabled: true

Second solution

In your model, create a function :

// in models/your_object_model
public function filterFields($fields, $context = null){
if ( *your condition* ) {
          $fields->your_field_name->hidden = true; // to hide the field (example 1)
          $fields->your_field_name->disabled = true; // or just to disable the field (example 2)
}

Be careful : this solution won't work with dates if there's a datepicker, because in that case the field is not a standard field : it's a widget, and it doesn't follow the same rules...

Third solution

Put a trigger on a field in the models/your_object_model/fields.yml configuration file of your object, for example :

# in models/your_object_model/fields.yml
your_field_name_to_control:
     label: Name of the field which behaviour you want to change
     type: textarea #(or any other type for your field)
     hidden: true
     trigger:
                action: show # (or other action)
                field: leader_field_control
                condition: checked # (or other condition)
leader_field_control:
     label: Activate the other Field ?
     type: switch # (or other type for this field)

Thanks to all for your feedbacks !! ;)

Last updated

chris10207
paulgrafx
paulgrafx

Based on the 'third solution' can this be used with radio buttons? as well as checkboxes

1-5 of 5

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.