Skip to content
This repository
Browse code

fixes disabled attribute for multiple checkboxes

  • Loading branch information...
commit a208eb6cb1e5489c722639fac0460e09d335191b 1 parent 22c1ac9
Mark authored July 23, 2012 markstory committed August 02, 2012
72  lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -2340,6 +2340,78 @@ public function testInputCheckboxesInLoop() {
2340 2340
 	}
2341 2341
 
2342 2342
 /**
  2343
+ * Test generating checkboxes with disabled elements.
  2344
+ *
  2345
+ * @return void
  2346
+ */
  2347
+	public function testInputCheckboxWithDisabledElements() {
  2348
+		$options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2349
+		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2350
+		
  2351
+		$expected = array(
  2352
+			array('div' => array('class' => 'input select')),
  2353
+			array('label' => array('for' => "ContactMultiple")),
  2354
+			'Multiple',
  2355
+			'/label',
  2356
+			array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2357
+			array('div' => array('class' => 'checkbox')),
  2358
+			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2359
+			array('label' => array('for' => "ContactMultiple1")),
  2360
+			'One',
  2361
+			'/label',
  2362
+			'/div',
  2363
+			array('div' => array('class' => 'checkbox')),
  2364
+			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2365
+			array('label' => array('for' => "ContactMultiple2")),
  2366
+			'Two',
  2367
+			'/label',
  2368
+			'/div',
  2369
+			array('div' => array('class' => 'checkbox')),
  2370
+			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2371
+			array('label' => array('for' => "ContactMultiple3")),
  2372
+			'Three',
  2373
+			'/label',
  2374
+			'/div',
  2375
+			'/div'
  2376
+		);
  2377
+		$this->assertTags($result, $expected);
  2378
+	
  2379
+		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2380
+		$this->assertTags($result, $expected);
  2381
+	
  2382
+		$disabled = array('2', 3);
  2383
+	
  2384
+		$expected = array(
  2385
+			array('div' => array('class' => 'input select')),
  2386
+			array('label' => array('for' => "ContactMultiple")),
  2387
+			'Multiple',
  2388
+			'/label',
  2389
+			array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2390
+			array('div' => array('class' => 'checkbox')),
  2391
+			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2392
+			array('label' => array('for' => "ContactMultiple1")),
  2393
+			'One',
  2394
+			'/label',
  2395
+			'/div',
  2396
+			array('div' => array('class' => 'checkbox')),
  2397
+			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2398
+			array('label' => array('for' => "ContactMultiple2")),
  2399
+			'Two',
  2400
+			'/label',
  2401
+			'/div',
  2402
+			array('div' => array('class' => 'checkbox')),
  2403
+			array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2404
+			array('label' => array('for' => "ContactMultiple3")),
  2405
+			'Three',
  2406
+			'/label',
  2407
+			'/div',
  2408
+			'/div'
  2409
+		);
  2410
+		$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2411
+		$this->assertTags($result, $expected);
  2412
+	}
  2413
+
  2414
+/**
2343 2415
  * test input name with leading integer, ensure attributes are generated correctly.
2344 2416
  *
2345 2417
  * @return void
16  lib/Cake/View/Helper/FormHelper.php
@@ -1801,7 +1801,8 @@ public function select($fieldName, $options = array(), $attributes = array()) {
1801 1801
 			'secure' => true,
1802 1802
 			'empty' => '',
1803 1803
 			'showParents' => false,
1804  
-			'hiddenField' => true
  1804
+			'hiddenField' => true,
  1805
+			'disabled' => false
1805 1806
 		);
1806 1807
 
1807 1808
 		$escapeOptions = $this->_extractOption('escape', $attributes);
@@ -1874,7 +1875,8 @@ public function select($fieldName, $options = array(), $attributes = array()) {
1874 1875
 				'name' => $attributes['name'],
1875 1876
 				'value' => $attributes['value'],
1876 1877
 				'class' => $attributes['class'],
1877  
-				'id' => $attributes['id']
  1878
+				'id' => $attributes['id'],
  1879
+				'disabled' => $attributes['disabled'],
1878 1880
 			)
1879 1881
 		));
1880 1882
 
@@ -2427,6 +2429,16 @@ protected function _selectOptions($elements = array(), $parents = array(), $show
2427 2429
 
2428 2430
 					if ($attributes['style'] === 'checkbox') {
2429 2431
 						$htmlOptions['value'] = $name;
  2432
+						
  2433
+						if (!empty($attributes['disabled'])) {
  2434
+							if (is_array($attributes['disabled'])) {
  2435
+								if (in_array($htmlOptions['value'], $attributes['disabled'])) {
  2436
+									$htmlOptions['disabled'] = 'disabled';
  2437
+								}
  2438
+							} else {
  2439
+								$htmlOptions['disabled'] = $attributes['disabled'] === true ? 'disabled' : $attributes['disabled'];
  2440
+							}
  2441
+						}
2430 2442
 
2431 2443
 						$tagName = $attributes['id'] . Inflector::camelize(Inflector::slug($name));
2432 2444
 						$htmlOptions['id'] = $tagName;

0 notes on commit a208eb6

Please sign in to comment.
Something went wrong with that request. Please try again.