Permalink
Browse files

Unskip required attribute tests for input().

Add missing required attribute/classname conventions and remove lengthy
tests.
  • Loading branch information...
1 parent 99be72f commit 450d3c172d46c5ed8562e28dacce7e82a9cd44b0 @markstory markstory committed Feb 26, 2014
Showing with 33 additions and 74 deletions.
  1. +9 −11 src/View/Helper/FormHelper.php
  2. +24 −63 tests/TestCase/View/Helper/FormHelperTest.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);
@@ -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);
}
/**

0 comments on commit 450d3c1

Please sign in to comment.