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

Changes implementation of form's and element's getValue() and clear() methods #13500

Merged
merged 9 commits into from
Oct 10, 2018
2 changes: 2 additions & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
- Changed `Phalcon\Db\Dialect\Postgresql::describeReferences` to generate correct SQL, added "on update" and "on delete" constraints
- Changed catch `Exception` to `Throwable` [#12288](https://github.com/phalcon/cphalcon/issues/12288)
- Changed `Phalcon\Mvc\Model\Query\Builder::addFrom` to remove third parameter `$with` [#13109](https://github.com/phalcon/cphalcon/pull/13109)
- `Phalcon\Forms\Form::clear` will no longer call `Phalcon\Forms\Element::clear`, instead it will clear/set default value itself, and `Phalcon\Forms\Element::clear` will now call `Phalcon\Forms\Form::clear` if it's assigned to the form, otherwise it will just clear itself. [#13500](https://github.com/phalcon/cphalcon/pull/13500)
- `Phalcon\Forms\Form::getValue` will now also try to get the value by calling `Tag::getValue` or element's `getDefault` method before returning `null`, and `Phalcon\Forms\Element::getValue` calls `Tag::getDefault` only if it's not added to the form. [#13500](https://github.com/phalcon/cphalcon/pull/13500)

## Removed
- PHP < 7.0 no longer supported
Expand Down
47 changes: 25 additions & 22 deletions phalcon/forms/element.zep
Original file line number Diff line number Diff line change
Expand Up @@ -408,36 +408,30 @@ abstract class Element implements ElementInterface
}

/**
* Returns the element value
* Returns the element's value
*/
public function getValue() -> var
{
var name, form, value;

let name = this->_name,
var name = this->_name,
form = this->_form,
value = null;

/**
* Get the related form
* If element belongs to the form, get value from the form
*/
let form = this->_form;
if typeof form == "object" {
/**
* Gets the possible value for the widget
*/
let value = form->getValue(name);

/**
* Check if the tag has a default value
*/
if typeof value == "null" && Tag::hasValue(name) {
let value = Tag::getValue(name);
}

return form->getValue(name);
}

/**
* Otherwise check Phalcon\Tag
*/
if Tag::hasValue(name) {
let value = Tag::getValue(name);
}

/**
* Assign the default value if there is no form available
* Assign the default value if there is no form available or Phalcon\Tag returns null
*/
if typeof value == "null" {
let value = this->_value;
Expand Down Expand Up @@ -482,11 +476,20 @@ abstract class Element implements ElementInterface
}

/**
* Clears every element in the form to its default value
* Clears element to its default value
*/
public function clear() -> <Element>
{
Tag::setDefault(this->_name, null);
var form = this->_form,
name = this->_name,
value = this->_value;

if typeof form == "object" {
form->clear(name);
} else {
Tag::setDefault(name, value);
}

return this;
}

Expand Down
56 changes: 42 additions & 14 deletions phalcon/forms/form.zep
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace Phalcon\Forms;

use Phalcon\Tag;
use Phalcon\Validation;
use Phalcon\ValidationInterface;
use Phalcon\DiInterface;
Expand Down Expand Up @@ -552,7 +553,7 @@ class Form extends Injectable implements \Countable, \Iterator
*/
public function getValue(string! name) -> var | null
{
var entity, method, value, data, $internal, forbidden;
var entity, method, value, data, $internal, forbidden, element;

let entity = this->_entity;
let data = this->_data;
Expand Down Expand Up @@ -622,6 +623,20 @@ class Form extends Injectable implements \Countable, \Iterator
if method_exists(this, method) {
return this->{method}();
}

/**
* Check if the tag has a default value
*/
if Tag::hasValue(name) {
return Tag::getValue(name);
}

/**
* Check if element has default value
*/
if fetch element, this->_elements[name] {
return element->getDefault();
}

return null;
}
Expand Down Expand Up @@ -661,14 +676,14 @@ class Form extends Injectable implements \Countable, \Iterator
/**
* Clears every element in the form to its default value
*
* @param array fields
* @param array|string|null fields
*/
public function clear(var fields = null) -> <Form>
{
var elements, element, data, field;

let data = this->_data;
if is_null(fields) {
if fields === null {
let data = [];
} else {
if typeof fields == "array" {
Expand All @@ -687,17 +702,30 @@ class Form extends Injectable implements \Countable, \Iterator
let this->_data = data,
elements = this->_elements;

if typeof elements == "array" {
for element in elements {
if typeof fields != "array" {
element->clear();
} else {
if in_array(element->getName(), fields) {
element->clear();
}
}
}
}
/**
* 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 {
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());
}
}
}
}
return this;
}

Expand Down