Skip to content

Commit

Permalink
Adding disableField() to start allowing disabled fields to be manipul…
Browse files Browse the repository at this point in the history
…ated

from the view/helper.
  • Loading branch information
markstory committed Jun 15, 2011
1 parent 2a15e63 commit 83184c9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
44 changes: 43 additions & 1 deletion lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -1051,6 +1051,7 @@ public function testFormSecurityInputDisabledFields() {
'disabledFields' => array('first_name', 'address')
);
$this->Form->create();
$this->assertEquals($this->Form->request['_Token']['disabledFields'], $this->Form->disableField());

$this->Form->hidden('Addresses.id', array('value' => '123456'));
$this->Form->input('Addresses.title');
Expand Down Expand Up @@ -1283,12 +1284,52 @@ public function testDisableSecurityUsingForm() {
);
$this->assertEqual($expected, $result);
}

/**
* test disableField
*
* @return void
*/
public function testDisableFieldAddsToList() {
$this->Form->request['_Token'] = array(
'key' => 'testKey',
'disabledFields' => array()
);
$this->Form->create('Contact');
$this->Form->disableField('Contact.name');
$this->Form->text('Contact.name');

$this->assertEquals(array('Contact.name'), $this->Form->disableField());
$this->assertEquals(array(), $this->Form->fields);
}

/**
* test disableField removing from fields array.
*
* @return void
*/
public function testDisableFieldRemovingFromFields() {
$this->Form->request['_Token'] = array(
'key' => 'testKey',
'disabledFields' => array()
);
$this->Form->create('Contact');
$this->Form->hidden('Contact.id', array('value' => 1));
$this->Form->text('Contact.name');

$this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
$this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');

$this->Form->disableField('Contact.name');
$this->Form->disableField('Contact.id');
$this->assertEquals(array(), $this->Form->fields);
}

/**
* testPasswordValidation method
*
* test validation errors on password input.
*
* @access public
* @return void
*/
public function testPasswordValidation() {
Expand Down Expand Up @@ -7127,4 +7168,5 @@ public function testHtml5Inputs() {
public function testHtml5InputException() {
$this->Form->email();
}

}
27 changes: 24 additions & 3 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -438,6 +438,29 @@ public function secure($fields = array()) {
return $this->Html->useTag('block', ' style="display:none;"', $out);
}

/**
* Add to or get the list of fields that are currently disabled.
* Disabled fields are not included in the field hash used by SecurityComponent
* disabling a field once its been added to the list of secured fields will remove
* it from the list of fields.
*
* @param string $name The dot separated name for the field.
* @return mixed Either null, or the list of fields.
*/
public function disableField($name = null) {
if ($name === null) {
return $this->_disabledFields;
}
if (!in_array($name, $this->_disabledFields)) {
$this->_disabledFields[] = $name;
}
$index = array_search($name, $this->fields);
if ($index !== false) {
unset($this->fields[$index]);
}
unset($this->fields[$name]);
}

/**
* Determine which fields of a form should be used for hash.
* Populates $this->fields
Expand Down Expand Up @@ -472,9 +495,7 @@ protected function __secure($lock, $field = null, $value = null) {
$this->fields[] = $field;
}
} else {
if (!in_array($field, $this->_disabledFields)) {
$this->_disabledFields[] = $field;
}
$this->disableField($field);
}
}

Expand Down

0 comments on commit 83184c9

Please sign in to comment.