CakePHP disable some switches?

I have a simple form with some switches. I want to disable some of the switches, is this possible?

$sixMonths = true; $twelveMonths = true; $twentyfourMonths = false; echo $this->McForm->create('Wizard', array ('url'=>'/wizard/create')); $options = array('24' => '24 months','12' => '12 months', '6' => '6 months'); $attributes = array('legend' =>false, 'default' => '6'); echo $this->McForm->radio('period', $options, $attributes); echo $this->McForm->submit('Save'); echo $this->McForm->end(); 

So, in this case, I would turn off the first switch and turn on the other two.

I know I could do it with jQuery, but I would rather do it without using it, is this possible? Any ideas?

Thanks!

+6
source share
2 answers

This is not possible if you do not call the radio function for each option separately and add 'disabled' => 'disabled' to the $attributes array for the ones you want to disable. Here a solution is possible:

 // Options $options = array('24' => '24 months','12' => '12 months', '6' => '6 months'); // Disabled options $disabled_options = array('12'); // Default attributes (these may need to be adjusted) $attributes = array('legend' => false, 'default' => '6'); // Loop through all of the options foreach ( $options as $key => $value ) { // Output the radio button. // The field name is now "period.n" which will result in "data[Model][period][n]" where "n" is the number of months. // The options is an array contain only the current $key and $value. // The 'disabled' => 'disabled' is added to the attributes if the key is found in the $disabled_options array. echo $this->McForm->radio('period.' . $key, array($key => $value), ( in_array($key, $disabled_options) ? $attributes + array('disabled' => 'disabled') : $attributes )); } 
0
source

You can add the disabled array to $attributes , which contains the values โ€‹โ€‹of the radio stations:

 $attributes = [ 'legend' => false, 'default' => '6', 'disabled' => ['6', '24'] ]; 
+5
source

Source: https://habr.com/ru/post/891515/


All Articles