Skip to content

Commit

Permalink
Merge pull request #2943 from cakephp/3.0-submit
Browse files Browse the repository at this point in the history
3.0 FormHelper submit
  • Loading branch information
lorenzo committed Mar 4, 2014
2 parents 0f4b145 + c60f3fb commit d6175bb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 140 deletions.
73 changes: 23 additions & 50 deletions src/View/Helper/FormHelper.php
Expand Up @@ -146,6 +146,7 @@ class FormHelper extends Helper {
'formend' => '</form>',
'hiddenblock' => '<div style="display:none;">{{content}}</div>',
'input' => '<input type="{{type}}" name="{{name}}"{{attrs}}>',
'inputsubmit' => '<input type="{{type}}"{{attrs}}>',
'label' => '<label{{attrs}}>{{text}}</label>',
'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>',
'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>',
Expand All @@ -157,7 +158,8 @@ class FormHelper extends Helper {
'formGroup' => '{{label}}{{input}}',
'checkboxFormGroup' => '{{input}}{{label}}',
'groupContainer' => '<div class="input {{type}}{{required}}">{{content}}</div>',
'groupContainerError' => '<div class="input {{type}}{{required}} error">{{content}}{{error}}</div>'
'groupContainerError' => '<div class="input {{type}}{{required}} error">{{content}}{{error}}</div>',
'submitContainer' => '<div class="submit">{{content}}</div>',
];

/**
Expand Down Expand Up @@ -1461,19 +1463,9 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag
*
* ### Options
*
* - `div` - Include a wrapping div? Defaults to true. Accepts sub options similar to
* FormHelper::input().
* - `before` - Content to include before the input.
* - `after` - Content to include after the input.
* - `type` - Set to 'reset' for reset inputs. Defaults to 'submit'
* - Other attributes will be assigned to the input element.
*
* ### Options
*
* - `div` - Include a wrapping div? Defaults to true. Accepts sub options similar to
* FormHelper::input().
* - Other attributes will be assigned to the input element.
*
* @param string $caption The label appearing on the button OR if string contains :// or the
* extension .jpg, .jpe, .jpeg, .gif, .png use an image if the extension
* exists, AND the first character is /, image is relative to webroot,
Expand All @@ -1482,78 +1474,59 @@ public function postLink($title, $url = null, $options = array(), $confirmMessag
* @return string A HTML submit button
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::submit
*/
public function submit($caption = null, $options = array()) {
public function submit($caption = null, $options = []) {
if (!is_string($caption) && empty($caption)) {
$caption = __d('cake', 'Submit');
}
$div = true;

if (isset($options['div'])) {
$div = $options['div'];
unset($options['div']);
}
$options += array('type' => 'submit', 'before' => null, 'after' => null, 'secure' => false);
$divOptions = array('tag' => 'div');

if ($div === true) {
$divOptions['class'] = 'submit';
} elseif ($div === false) {
unset($divOptions);
} elseif (is_string($div)) {
$divOptions['class'] = $div;
} elseif (is_array($div)) {
$divOptions = array_merge(array('class' => 'submit', 'tag' => 'div'), $div);
}
$options += ['type' => 'submit', 'secure' => false];

if (isset($options['name'])) {
$name = str_replace(array('[', ']'), array('.', ''), $options['name']);
$this->_secure($options['secure'], $this->_secureFieldName($options));
}
unset($options['secure']);

$before = $options['before'];
$after = $options['after'];
unset($options['before'], $options['after']);

$isUrl = strpos($caption, '://') !== false;
$isImage = preg_match('/\.(jpg|jpe|jpeg|gif|png|ico)$/', $caption);

$type = $options['type'];
unset($options['type']);

if ($isUrl || $isImage) {
$unlockFields = array('x', 'y');
if (isset($options['name'])) {
$unlockFields = array(
$options['name'] . '_x', $options['name'] . '_y'
);
$unlockFields = [
$options['name'] . '_x',
$options['name'] . '_y'
];
}
foreach ($unlockFields as $ignore) {
$this->unlockField($ignore);
}
$type = 'image';
}

if ($isUrl) {
unset($options['type']);
$tag = $this->Html->useTag('submitimage', $caption, $options);
$options['src'] = $caption;
} elseif ($isImage) {
unset($options['type']);
if ($caption{0} !== '/') {
$url = $this->webroot(Configure::read('App.imageBaseUrl') . $caption);
} else {
$url = $this->webroot(trim($caption, '/'));
}
$url = $this->assetTimestamp($url);
$tag = $this->Html->useTag('submitimage', $url, $options);
$options['src'] = $url;
} else {
$options['value'] = $caption;
$tag = $this->Html->useTag('submit', $options);
}
$out = $before . $tag . $after;

if (isset($divOptions)) {
$tag = $divOptions['tag'];
unset($divOptions['tag']);
$out = $this->Html->tag($tag, $out, $divOptions);
}
return $out;
$input = $this->formatTemplate('inputsubmit', [
'type' => $type,
'attrs' => $this->_templater->formatAttributes($options),
]);

return $this->formatTemplate('submitContainer', [
'content' => $input
]);
}

/**
Expand Down
90 changes: 0 additions & 90 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -5241,26 +5241,6 @@ public function testSubmitButton() {
);
$this->assertTags($result, $expected);

$result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
$expected = array(
'span' => array('class' => 'submit'),
'input' => array('type' => 'submit', 'value' => 'Test Submit'),
'/span'
);
$this->assertTags($result, $expected);

$result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
$expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
$this->assertTags($result, $expected);

$result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
$expected = array(
'div' => array('class' => 'submit', 'id' => 'SaveButton'),
'input' => array('type' => 'submit', 'value' => 'Test Submit'),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->submit('Next >');
$expected = array(
'div' => array('class' => 'submit'),
Expand All @@ -5284,36 +5264,6 @@ public function testSubmitButton() {
'/div'
);
$this->assertTags($result, $expected);

$before = '--before--';
$after = '--after--';
$result = $this->Form->submit('Test', array('before' => $before));
$expected = array(
'div' => array('class' => 'submit'),
'--before--',
'input' => array('type' => 'submit', 'value' => 'Test'),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->submit('Test', array('after' => $after));
$expected = array(
'div' => array('class' => 'submit'),
'input' => array('type' => 'submit', 'value' => 'Test'),
'--after--',
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
$expected = array(
'div' => array('class' => 'submit'),
'--before--',
'input' => array('type' => 'submit', 'value' => 'Test'),
'--after--',
'/div'
);
$this->assertTags($result, $expected);
}

/**
Expand Down Expand Up @@ -5353,46 +5303,6 @@ public function testSubmitImage() {
'/div'
);
$this->assertTags($result, $expected);

$after = '--after--';
$before = '--before--';
$result = $this->Form->submit('cake.power.gif', array('after' => $after));
$expected = array(
'div' => array('class' => 'submit'),
'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
'--after--',
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->submit('cake.power.gif', array('before' => $before));
$expected = array(
'div' => array('class' => 'submit'),
'--before--',
'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
$expected = array(
'div' => array('class' => 'submit'),
'--before--',
'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
'--after--',
'/div'
);
$this->assertTags($result, $expected);

$result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
$expected = array(
'div' => array('class' => 'submit'),
'--before--',
'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
'--after--',
'/div'
);
$this->assertTags($result, $expected);
}

/**
Expand Down

0 comments on commit d6175bb

Please sign in to comment.