Added html5 required attribute for select tags too

This commit is contained in:
ADmad 2013-04-16 00:02:04 +05:30
parent 8135d405a3
commit e11d0f829a
2 changed files with 54 additions and 0 deletions

View file

@ -4191,6 +4191,32 @@ class FormHelperTest extends CakeTestCase {
'/select' '/select'
); );
$this->assertTags($result, $expected); $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 @@ class FormHelperTest extends CakeTestCase {
'/select' '/select'
); );
$this->assertTags($result, $expected); $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 file

@ -2008,6 +2008,14 @@ class FormHelper extends AppHelper {
$tag = 'selectstart'; $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)) { if (!empty($tag) || isset($template)) {
$hasOptions = (count($options) > 0 || $showEmpty); $hasOptions = (count($options) > 0 || $showEmpty);
// Secure the field if there are options, or its a multi select. // Secure the field if there are options, or its a multi select.