Skip to content

Commit

Permalink
Merge pull request #14218 from SidRoberts/fixes-14217
Browse files Browse the repository at this point in the history
Form::clear() now correctly clears single fields
  • Loading branch information
sergeyklay authored Jun 28, 2019
2 parents 437470d + 4559ba6 commit ac770e5
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
- Fixed `Phalcon\Config::merge()` not merging numeric values properly [#13201](https://github.com/phalcon/cphalcon/issues/13201), [#13768](https://github.com/phalcon/cphalcon/issues/13768)
- Fixed `Phalcon\Validation\Validator\File\AbstractFile` missing the resolution of the `value` property [#14198](https://github.com/phalcon/cphalcon/issues/14198)
- Fixed `Phalcon\Storage\Adapter\Stream` [#14190](https://github.com/phalcon/cphalcon/issues/14190)
- `Phalcon\Form\Form::clear()` now correctly clears single fields. [#14217](https://github.com/phalcon/cphalcon/issues/14217)

## Removed
- Removed `Phalcon\Session\Factory`. [#13672](https://github.com/phalcon/cphalcon/issues/13672)
Expand Down
61 changes: 22 additions & 39 deletions phalcon/Forms/Form.zep
Original file line number Diff line number Diff line change
Expand Up @@ -218,61 +218,44 @@ class Form extends Injectable implements Countable, Iterator, AttributesInterfac
{
var elements, element, data, field;

let data = this->data;
let data = this->data,
elements = this->elements;

/**
* If fields is string, clear just that field.
* If it's array, clear only fields in array.
* If null, clear all
*/
if fields === null {
let data = [];

for element in elements {
Tag::setDefault(
element->getName(),
element->getDefault()
);
}
} else {
if typeof fields == "array" {
for field in fields {
if isset data[field] {
unset data[field];
}
}
} else {
if typeof fields != "array" {
let fields = [fields];
}

for field in fields {
if isset data[field] {
unset data[field];
}
}
}

let this->data = data,
elements = this->elements;

/**
* If fields is string, clear just that field.
* If it's array, clear only fields in array.
* If null, clear all
*/
if typeof elements == "array" {
if fields === null {
for element in elements {
if fetch element, elements[field] {
Tag::setDefault(
element->getName(),
element->getDefault()
);
}
} else {
if typeof fields == "array" {
for element in elements {
if in_array(element->getName(), fields) {
Tag::setDefault(
element->getName(),
element->getDefault()
);
}
}
} else {
if fetch element, elements[fields] {
Tag::setDefault(
element->getName(),
element->getDefault()
);
}
}
}
}

let this->data = data;

return this;
}

Expand Down
151 changes: 145 additions & 6 deletions tests/integration/Forms/Form/ClearCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,155 @@ public function _before(IntegrationTester $I)
}

/**
* Tests Phalcon\Forms\Form :: clear()
* Tests Phalcon\Forms\Form :: clear() - all
*
* @author Phalcon Team <[email protected]>
* @since 2018-11-13
* @author Sid Roberts <https://github.com/SidRoberts>
* @since 2019-06-28
*/
public function formsFormClearAll(IntegrationTester $I)
{
$I->wantToTest('Forms\Form - clear() - all');

$name = new Text('name');
$email = new Email('email');
$password = new Password('password');

$form = new Form();

$form
->add($name)
->add($email)
->add($password)
;

$entity = new \stdClass();

$form->bind(
[
'name' => 'Sid Roberts',
'email' => '[email protected]',
'password' => 'hunter2',
],
$entity
);

$form->clear();

$I->assertNull(
$form->get('name')->getValue()
);

$I->assertNull(
$form->get('email')->getValue()
);

$I->assertNull(
$form->get('password')->getValue()
);
}

/**
* Tests Phalcon\Forms\Form :: clear() - fields array
*
* @author Sid Roberts <https://github.com/SidRoberts>
* @since 2019-06-28
*/
public function formsFormClearFieldsArray(IntegrationTester $I)
{
$I->wantToTest('Forms\Form - clear() - fields array');

$name = new Text('name');
$email = new Email('email');
$password = new Password('password');

$form = new Form();

$form
->add($name)
->add($email)
->add($password)
;

$entity = new \stdClass();

$form->bind(
[
'name' => 'Sid Roberts',
'email' => '[email protected]',
'password' => 'hunter2',
],
$entity
);

$form->clear(
[
'email',
'password',
]
);

$I->assertEquals(
'Sid Roberts',
$form->get('name')->getValue()
);

$I->assertNull(
$form->get('email')->getValue()
);

$I->assertNull(
$form->get('password')->getValue()
);
}

/**
* Tests Phalcon\Forms\Form :: clear() - field string
*
* @author Sid Roberts <https://github.com/SidRoberts>
* @since 2019-06-28
*/
public function formsFormClear(IntegrationTester $I)
public function formsFormClearFieldString(IntegrationTester $I)
{
$I->wantToTest('Forms\Form - clear()');
$I->wantToTest('Forms\Form - clear() - field string');

$name = new Text('name');
$email = new Email('email');
$password = new Password('password');

$I->skipTest('Need implementation');
$form = new Form();

$form
->add($name)
->add($email)
->add($password)
;

$entity = new \stdClass();

$form->bind(
[
'name' => 'Sid Roberts',
'email' => '[email protected]',
'password' => 'hunter2',
],
$entity
);

$form->clear('password');

$I->assertEquals(
'Sid Roberts',
$form->get('name')->getValue()
);

$I->assertEquals(
'[email protected]',
$form->get('email')->getValue()
);

$I->assertNull(
$form->get('password')->getValue()
);
}

/**
Expand Down

0 comments on commit ac770e5

Please sign in to comment.