Skip to content
Browse files

Added html5 required attribute for select tags too

  • Loading branch information...
1 parent 8135d40 commit e11d0f829a03ab32fe42c3e59e80923f1c9708b2 @ADmad ADmad committed
Showing with 54 additions and 0 deletions.
  1. +46 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +8 −0 lib/Cake/View/Helper/FormHelper.php
View
46 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -4191,6 +4191,32 @@ public function testSelect() {
'/select'
);
$this->assertTags($result, $expected);
+
+ $result = $this->Form->select('Contact.required_one', array('option A'));
+ $expected = array(
+ 'select' => array(
+ 'name' => 'data[Contact][required_one]',
+ 'id' => 'ContactRequiredOne',
+ 'required' => 'required'
+ ),
+ array('option' => array('value' => '')), '/option',
+ array('option' => array('value' => '0')), 'option A', '/option',
+ '/select'
+ );
+ $this->assertTags($result, $expected);
+
+ $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
+ $expected = array(
+ 'select' => array(
+ 'name' => 'data[Contact][required_one]',
+ 'id' => 'ContactRequiredOne',
+ 'disabled' => 'disabled'
+ ),
+ array('option' => array('value' => '', 'disabled' => 'disabled')), '/option',
+ array('option' => array('value' => '0', 'disabled' => 'disabled')), 'option A', '/option',
+ '/select'
+ );
+ $this->assertTags($result, $expected);
}
/**
@@ -4444,6 +4470,26 @@ public function testSelectMultiple() {
'/select'
);
$this->assertTags($result, $expected);
+
+ $result = $this->Form->select('Contact.required_one', array(
+ '1' => 'option A',
+ '2' => 'option B'
+ ), array('multiple' => true));
+ $expected = array(
+ 'input' => array(
+ 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
+ ),
+ 'select' => array(
+ 'name' => 'data[Contact][required_one][]',
+ 'id' => 'ContactRequiredOne',
+ 'required' => 'required',
+ 'multiple' => 'multiple'
+ ),
+ array('option' => array('value' => '1')), 'option A', '/option',
+ array('option' => array('value' => '2')), 'option B', '/option',
+ '/select'
+ );
+ $this->assertTags($result, $expected);
}
/**
View
8 lib/Cake/View/Helper/FormHelper.php
@@ -2008,6 +2008,14 @@ public function select($fieldName, $options = array(), $attributes = array()) {
$tag = 'selectstart';
}
+ if ($tag !== 'checkboxmultiplestart' &&
+ !isset($attributes['required']) &&
+ empty($attributes['disabled']) &&
+ $this->_introspectModel($this->model(), 'validates', $this->field())
+ ) {
+ $attributes['required'] = true;
+ }
+
if (!empty($tag) || isset($template)) {
$hasOptions = (count($options) > 0 || $showEmpty);
// Secure the field if there are options, or its a multi select.

0 comments on commit e11d0f8

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