Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form::clear() now correctly clears single fields #14218

Merged
merged 3 commits into from
Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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