Skip to content

Commit

Permalink
Removing ClassRegistry use from FormHelper and its test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Aug 11, 2010
1 parent 90b0ac7 commit 576eba3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 42 deletions.
25 changes: 9 additions & 16 deletions cake/libs/view/helpers/form.php
Expand Up @@ -188,7 +188,6 @@ protected function _isRequiredField($validateProperties) {
function create($model = null, $options = array()) {
$created = $id = false;
$append = '';
$view =& ClassRegistry::getObject('view');

if (is_array($model) && empty($options)) {
$options = $model;
Expand Down Expand Up @@ -253,7 +252,7 @@ function create($model = null, $options = array()) {

$actionDefaults = array(
'plugin' => $this->plugin,
'controller' => $view->viewPath,
'controller' => $this->_View->viewPath,
'action' => $options['action'],
0 => $id
);
Expand Down Expand Up @@ -372,8 +371,7 @@ public function end($options = null) {
$this->setEntity(null);
$out .= $this->Html->tags['formend'];

$view =& ClassRegistry::getObject('view');
$view->modelScope = false;
$this->_View->modelScope = false;
return $out;
}

Expand Down Expand Up @@ -421,8 +419,7 @@ public function secure($fields = array()) {
*/
function __secure($field = null, $value = null) {
if (!$field) {
$view =& ClassRegistry::getObject('view');
$field = $view->entity();
$field = $this->_View->entity();
} elseif (is_string($field)) {
$field = Set::filter(explode('.', $field), true);
}
Expand Down Expand Up @@ -535,8 +532,7 @@ public function error($field, $text = null, $options = array()) {
*/
function label($fieldName = null, $text = null, $options = array()) {
if (empty($fieldName)) {
$view = ClassRegistry::getObject('view');
$fieldName = implode('.', $view->entity());
$fieldName = implode('.', $this->_View->entity());
}

if ($text === null) {
Expand Down Expand Up @@ -761,11 +757,10 @@ public function input($fieldName, $options = array()) {
(!isset($options['options']) && in_array($options['type'], $types)) ||
(isset($magicType) && $options['type'] == 'text')
) {
$view =& ClassRegistry::getObject('view');
$varName = Inflector::variable(
Inflector::pluralize(preg_replace('/_id$/', '', $fieldKey))
);
$varOptions = $view->getVar($varName);
$varOptions = $this->_View->getVar($varName);
if (is_array($varOptions)) {
if ($options['type'] !== 'radio') {
$options['type'] = 'select';
Expand Down Expand Up @@ -1229,8 +1224,7 @@ public function hidden($fieldName, $options = array()) {
public function file($fieldName, $options = array()) {
$options = array_merge($options, array('secure' => false));
$options = $this->_initInputField($fieldName, $options);
$view =& ClassRegistry::getObject('view');
$field = $view->entity();
$field = $this->_View->entity();

foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $suffix) {
$this->__secure(array_merge($field, array($suffix)));
Expand Down Expand Up @@ -1942,10 +1936,9 @@ protected function _name($options = array(), $field = null, $key = 'name') {
return $options;
}

$view = ClassRegistry::getObject('view');
$name = $view->field;
if (!empty($view->fieldSuffix)) {
$name .= '[' . $view->fieldSuffix . ']';
$name = $this->_View->field;
if (!empty($this->_View->fieldSuffix)) {
$name .= '[' . $this->_View->fieldSuffix . ']';
}

if (is_array($options)) {
Expand Down
38 changes: 12 additions & 26 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -678,7 +678,6 @@ function setUp() {
$this->Form = new FormHelper($this->View);
$this->Form->params['action'] = 'add';

ClassRegistry::addObject('view', $view);
ClassRegistry::addObject('Contact', new Contact());
ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
Expand Down Expand Up @@ -708,7 +707,6 @@ function setUp() {
* @return void
*/
function tearDown() {
ClassRegistry::removeObject('view');
ClassRegistry::removeObject('Contact');
ClassRegistry::removeObject('ContactNonStandardPk');
ClassRegistry::removeObject('ContactTag');
Expand Down Expand Up @@ -2088,8 +2086,8 @@ function testInputSelectType() {
$this->assertTags($result, $expected);

$this->Form->data = array('Model' => array('user_id' => 'value'));
$view =& ClassRegistry::getObject('view');
$view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');

$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.user_id', array('empty' => true));
$expected = array(
'div' => array('class' => 'input select'),
Expand All @@ -2111,8 +2109,8 @@ function testInputSelectType() {
$this->assertTags($result, $expected);

$this->Form->data = array('Model' => array('user_id' => null));
$view =& ClassRegistry::getObject('view');
$view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');

$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.user_id', array('empty' => 'Some Empty'));
$expected = array(
'div' => array('class' => 'input select'),
Expand All @@ -2135,8 +2133,8 @@ function testInputSelectType() {
$this->assertTags($result, $expected);

$this->Form->data = array('Model' => array('user_id' => 'value'));
$view =& ClassRegistry::getObject('view');
$view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');

$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.user_id', array('empty' => 'Some Empty'));
$expected = array(
'div' => array('class' => 'input select'),
Expand All @@ -2159,8 +2157,8 @@ function testInputSelectType() {
$this->assertTags($result, $expected);

$this->Form->data = array('User' => array('User' => array('value')));
$view =& ClassRegistry::getObject('view');
$view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');

$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('User.User', array('empty' => true));
$expected = array(
'div' => array('class' => 'input select'),
Expand Down Expand Up @@ -2212,8 +2210,7 @@ function testInputWithNonStandardPrimaryKeyMakesHidden() {
* @return void
*/
function testInputOverridingMagicSelectType() {
$view =& ClassRegistry::getObject('view');
$view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.user_id', array('type' => 'text'));
$expected = array(
'div' => array('class' => 'input text'),
Expand All @@ -2224,8 +2221,7 @@ function testInputOverridingMagicSelectType() {
$this->assertTags($result, $expected);

//Check that magic types still work for plural/singular vars
$view =& ClassRegistry::getObject('view');
$view->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
$this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.type');
$expected = array(
'div' => array('class' => 'input select'),
Expand All @@ -2245,8 +2241,7 @@ function testInputOverridingMagicSelectType() {
* @return void
*/
function testInputMagicSelectChangeToRadio() {
$view =& ClassRegistry::getObject('view');
$view->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
$result = $this->Form->input('Model.user_id', array('type' => 'radio'));
$this->assertPattern('/input type="radio"/', $result);
}
Expand Down Expand Up @@ -2281,7 +2276,6 @@ function testFormInputs() {
);
$this->assertTags($result, $expected);

$View = ClassRegistry::getObject('view');
$this->Form->create('Contact');
$this->Form->params['prefix'] = 'admin';
$this->Form->action = 'admin_edit';
Expand Down Expand Up @@ -3242,8 +3236,7 @@ function testSelectMultiple() {
* @return void
*/
function testHabtmSelectBox() {
$view =& ClassRegistry::getObject('view');
$view->viewVars['contactTags'] = array(
$this->View->viewVars['contactTags'] = array(
1 => 'blue',
2 => 'red',
3 => 'green'
Expand Down Expand Up @@ -4974,13 +4967,6 @@ function testFileUploadField() {
* @return void
*/
function testFileUploadOnOtherModel() {
ClassRegistry::removeObject('view');
$controller =& new Controller();
$controller->name = 'ValidateUsers';
$controller->uses = array('ValidateUser');
$controller->constructClasses();
$view =& new View($controller, true);

$this->Form->create('ValidateUser', array('type' => 'file'));
$result = $this->Form->file('ValidateProfile.city');
$expected = array(
Expand Down

0 comments on commit 576eba3

Please sign in to comment.