Skip to content

Commit 450d3c1

Browse files
committed
Unskip required attribute tests for input().
Add missing required attribute/classname conventions and remove lengthy tests.
1 parent 99be72f commit 450d3c1

File tree

2 files changed

+33
-74
lines changed

2 files changed

+33
-74
lines changed

src/View/Helper/FormHelper.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -823,31 +823,23 @@ public function inputs($fields = null, $blacklist = null, $options = array()) {
823823
* - `type` - Force the type of widget you want. e.g. `type => 'select'`
824824
* - `label` - Either a string label, or an array of options for the label. See FormHelper::label().
825825
* - `div` - Either `false` to disable the div, or an array of options for the div.
826-
* See HtmlHelper::div() for more options.
827826
* - `options` - For widgets that take options e.g. radio, select.
828827
* - `error` - Control the error message that is produced. Set to `false` to disable any kind of error reporting (field
829828
* error and error messages).
830829
* - `empty` - String or boolean to enable empty select box options.
831-
* - `before` - Content to place before the label + input.
832-
* - `after` - Content to place after the label + input.
833-
* - `between` - Content to place between the label + input.
834-
* - `format` - Format template for element order. Any element that is not in the array, will not be in the output.
835-
* - Default input format order: array('before', 'label', 'between', 'input', 'after', 'error')
836-
* - Default checkbox format order: array('before', 'input', 'between', 'label', 'after', 'error')
837-
* - Hidden input will not be formatted
838-
* - Radio buttons cannot have the order of input and label elements controlled with these settings.
839830
*
840831
* @param string $fieldName This should be "Modelname.fieldname"
841832
* @param array $options Each type of input takes different options.
842833
* @return string Completed form widget.
843834
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#creating-form-elements
844835
*/
845-
public function input($fieldName, $options = array()) {
836+
public function input($fieldName, $options = []) {
846837
$options += [
847838
'type' => null,
848839
'label' => null,
849840
'error' => null,
850841
'options' => null,
842+
'required' => null,
851843
'templates' => []
852844
];
853845
$options = $this->_parseOptions($fieldName, $options);
@@ -856,6 +848,7 @@ public function input($fieldName, $options = array()) {
856848
$originalTemplates = $this->templates();
857849
$this->templates($options['templates']);
858850
unset($options['templates']);
851+
859852
$label = $this->_getLabel($fieldName, $options);
860853
if ($options['type'] !== 'radio') {
861854
unset($options['label']);
@@ -877,7 +870,7 @@ public function input($fieldName, $options = array()) {
877870
$result = $this->formatTemplate($template, [
878871
'content' => $result,
879872
'error' => $error,
880-
'required' => null,
873+
'required' => $options['required'] ? ' required' : '',
881874
'type' => $options['type'],
882875
]);
883876
}
@@ -1008,6 +1001,11 @@ protected function _optionsOptions($fieldName, $options) {
10081001
*/
10091002
protected function _magicOptions($fieldName, $options, $allowOverride) {
10101003
$context = $this->_getContext();
1004+
1005+
if (!isset($options['required'])) {
1006+
$options['required'] = $context->isRequired($fieldName);
1007+
}
1008+
10111009
$type = $context->type($fieldName);
10121010
$fieldDef = $context->attributes($fieldName);
10131011

tests/TestCase/View/Helper/FormHelperTest.php

Lines changed: 24 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7299,92 +7299,53 @@ public function testHtml5InputException() {
72997299
}
73007300

73017301
/**
7302-
* Tests that the 'on' key validates as expected on create
7302+
* Tests that formhelper sets required attributes.
73037303
*
73047304
* @return void
73057305
*/
7306-
public function testRequiredOnCreate() {
7307-
$this->markTestIncomplete('Need to revisit once models work again.');
7308-
$this->Form->create('Contact');
7309-
7310-
$result = $this->Form->input('Contact.imrequiredonupdate');
7311-
$expected = array(
7312-
'div' => array('class' => 'input text'),
7313-
'label' => array('for' => 'ContactImrequiredonupdate'),
7314-
'Imrequiredonupdate',
7315-
'/label',
7316-
'input' => array(
7317-
'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
7318-
'id' => 'ContactImrequiredonupdate'
7319-
),
7320-
'/div'
7321-
);
7322-
$this->assertTags($result, $expected);
7306+
public function testRequiredAttribute() {
7307+
$this->article['required'] = [
7308+
'title' => true,
7309+
'body' => false,
7310+
];
7311+
$this->Form->create($this->article);
73237312

7324-
$result = $this->Form->input('Contact.imrequiredoncreate');
7313+
$result = $this->Form->input('title');
73257314
$expected = array(
73267315
'div' => array('class' => 'input text required'),
7327-
'label' => array('for' => 'ContactImrequiredoncreate'),
7328-
'Imrequiredoncreate',
7316+
'label' => array('for' => 'title'),
7317+
'Title',
73297318
'/label',
73307319
'input' => array(
7331-
'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
7332-
'id' => 'ContactImrequiredoncreate',
7333-
'required' => 'required'
7320+
'type' => 'text',
7321+
'name' => 'title',
7322+
'id' => 'title',
7323+
'required' => 'required',
73347324
),
73357325
'/div'
73367326
);
73377327
$this->assertTags($result, $expected);
73387328

7339-
$result = $this->Form->input('Contact.imrequiredonboth');
7340-
$expected = array(
7341-
'div' => array('class' => 'input text required'),
7342-
'label' => array('for' => 'ContactImrequiredonboth'),
7343-
'Imrequiredonboth',
7344-
'/label',
7345-
'input' => array(
7346-
'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
7347-
'id' => 'ContactImrequiredonboth',
7348-
'required' => 'required'
7349-
),
7350-
'/div'
7351-
);
7352-
$this->assertTags($result, $expected);
7329+
$result = $this->Form->input('title', ['required' => false]);
7330+
$this->assertNotContains('required', $result);
73537331

7354-
$this->Form->inputDefaults(array('required' => false));
7355-
$result = $this->Form->input('Contact.imrequired');
7332+
$result = $this->Form->input('body');
73567333
$expected = array(
73577334
'div' => array('class' => 'input text'),
7358-
'label' => array('for' => 'ContactImrequired'),
7359-
'Imrequired',
7335+
'label' => array('for' => 'body'),
7336+
'Body',
73607337
'/label',
73617338
'input' => array(
7362-
'type' => 'text', 'name' => 'Contact[imrequired]',
7363-
'id' => 'ContactImrequired'
7364-
),
7365-
'/div'
7366-
);
7367-
$this->assertTags($result, $expected);
7368-
7369-
$result = $this->Form->input('Contact.imrequired', array('required' => false));
7370-
$this->assertTags($result, $expected);
7371-
7372-
$result = $this->Form->input('Contact.imrequired', array('required' => true));
7373-
$expected = array(
7374-
'div' => array('class' => 'input text required'),
7375-
'label' => array('for' => 'ContactImrequired'),
7376-
'Imrequired',
7377-
'/label',
7378-
'input' => array(
7379-
'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
7380-
'id' => 'ContactImrequired'
7339+
'type' => 'text',
7340+
'name' => 'body',
7341+
'id' => 'body',
73817342
),
73827343
'/div'
73837344
);
73847345
$this->assertTags($result, $expected);
73857346

7386-
$result = $this->Form->input('Contact.imrequired', array('required' => null));
7387-
$this->assertTags($result, $expected);
7347+
$result = $this->Form->input('body', ['required' => true]);
7348+
$this->assertContains('required', $result);
73887349
}
73897350

73907351
/**

0 commit comments

Comments
 (0)