diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 9a6a95e7dc3..52427f512b7 100755 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -823,31 +823,23 @@ public function inputs($fields = null, $blacklist = null, $options = array()) { * - `type` - Force the type of widget you want. e.g. `type => 'select'` * - `label` - Either a string label, or an array of options for the label. See FormHelper::label(). * - `div` - Either `false` to disable the div, or an array of options for the div. - * See HtmlHelper::div() for more options. * - `options` - For widgets that take options e.g. radio, select. * - `error` - Control the error message that is produced. Set to `false` to disable any kind of error reporting (field * error and error messages). * - `empty` - String or boolean to enable empty select box options. - * - `before` - Content to place before the label + input. - * - `after` - Content to place after the label + input. - * - `between` - Content to place between the label + input. - * - `format` - Format template for element order. Any element that is not in the array, will not be in the output. - * - Default input format order: array('before', 'label', 'between', 'input', 'after', 'error') - * - Default checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error') - * - Hidden input will not be formatted - * - Radio buttons cannot have the order of input and label elements controlled with these settings. * * @param string $fieldName This should be "Modelname.fieldname" * @param array $options Each type of input takes different options. * @return string Completed form widget. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#creating-form-elements */ - public function input($fieldName, $options = array()) { + public function input($fieldName, $options = []) { $options += [ 'type' => null, 'label' => null, 'error' => null, 'options' => null, + 'required' => null, 'templates' => [] ]; $options = $this->_parseOptions($fieldName, $options); @@ -856,6 +848,7 @@ public function input($fieldName, $options = array()) { $originalTemplates = $this->templates(); $this->templates($options['templates']); unset($options['templates']); + $label = $this->_getLabel($fieldName, $options); if ($options['type'] !== 'radio') { unset($options['label']); @@ -877,7 +870,7 @@ public function input($fieldName, $options = array()) { $result = $this->formatTemplate($template, [ 'content' => $result, 'error' => $error, - 'required' => null, + 'required' => $options['required'] ? ' required' : '', 'type' => $options['type'], ]); } @@ -1008,6 +1001,11 @@ protected function _optionsOptions($fieldName, $options) { */ protected function _magicOptions($fieldName, $options, $allowOverride) { $context = $this->_getContext(); + + if (!isset($options['required'])) { + $options['required'] = $context->isRequired($fieldName); + } + $type = $context->type($fieldName); $fieldDef = $context->attributes($fieldName); diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index 07ff41e6e2e..37f860b74da 100755 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -7299,92 +7299,53 @@ public function testHtml5InputException() { } /** - * Tests that the 'on' key validates as expected on create + * Tests that formhelper sets required attributes. * * @return void */ - public function testRequiredOnCreate() { - $this->markTestIncomplete('Need to revisit once models work again.'); - $this->Form->create('Contact'); - - $result = $this->Form->input('Contact.imrequiredonupdate'); - $expected = array( - 'div' => array('class' => 'input text'), - 'label' => array('for' => 'ContactImrequiredonupdate'), - 'Imrequiredonupdate', - '/label', - 'input' => array( - 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]', - 'id' => 'ContactImrequiredonupdate' - ), - '/div' - ); - $this->assertTags($result, $expected); + public function testRequiredAttribute() { + $this->article['required'] = [ + 'title' => true, + 'body' => false, + ]; + $this->Form->create($this->article); - $result = $this->Form->input('Contact.imrequiredoncreate'); + $result = $this->Form->input('title'); $expected = array( 'div' => array('class' => 'input text required'), - 'label' => array('for' => 'ContactImrequiredoncreate'), - 'Imrequiredoncreate', + 'label' => array('for' => 'title'), + 'Title', '/label', 'input' => array( - 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]', - 'id' => 'ContactImrequiredoncreate', - 'required' => 'required' + 'type' => 'text', + 'name' => 'title', + 'id' => 'title', + 'required' => 'required', ), '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->input('Contact.imrequiredonboth'); - $expected = array( - 'div' => array('class' => 'input text required'), - 'label' => array('for' => 'ContactImrequiredonboth'), - 'Imrequiredonboth', - '/label', - 'input' => array( - 'type' => 'text', 'name' => 'Contact[imrequiredonboth]', - 'id' => 'ContactImrequiredonboth', - 'required' => 'required' - ), - '/div' - ); - $this->assertTags($result, $expected); + $result = $this->Form->input('title', ['required' => false]); + $this->assertNotContains('required', $result); - $this->Form->inputDefaults(array('required' => false)); - $result = $this->Form->input('Contact.imrequired'); + $result = $this->Form->input('body'); $expected = array( 'div' => array('class' => 'input text'), - 'label' => array('for' => 'ContactImrequired'), - 'Imrequired', + 'label' => array('for' => 'body'), + 'Body', '/label', 'input' => array( - 'type' => 'text', 'name' => 'Contact[imrequired]', - 'id' => 'ContactImrequired' - ), - '/div' - ); - $this->assertTags($result, $expected); - - $result = $this->Form->input('Contact.imrequired', array('required' => false)); - $this->assertTags($result, $expected); - - $result = $this->Form->input('Contact.imrequired', array('required' => true)); - $expected = array( - 'div' => array('class' => 'input text required'), - 'label' => array('for' => 'ContactImrequired'), - 'Imrequired', - '/label', - 'input' => array( - 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]', - 'id' => 'ContactImrequired' + 'type' => 'text', + 'name' => 'body', + 'id' => 'body', ), '/div' ); $this->assertTags($result, $expected); - $result = $this->Form->input('Contact.imrequired', array('required' => null)); - $this->assertTags($result, $expected); + $result = $this->Form->input('body', ['required' => true]); + $this->assertContains('required', $result); } /**