Skip to content

Commit e0dc3a7

Browse files
authored
Merge pull request #16575 from noone-silent/T16567-form-bind-entity-property-check
[#16567] - Fix for Form::bind deprecated warnings.
2 parents ad6678e + 8c14b2e commit e0dc3a7

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

CHANGELOG-5.0.md

+3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
- Changed `Phalcon\Support\HelperFactory` to use the internal mapper for better memory management [#16571](https://github.com/phalcon/cphalcon/issues/16571)
88

99
### Added
10+
11+
- New ini setting `phalcon.form.strict_entity_property_check` for `Phalcon\Forms\Form` to enable strict entity property checking. [#16567](https://github.com/phalcon/cphalcon/issues/16567)
1012

1113
### Fixed
1214

1315
- Fixed `Phalcon\Mvc\Cli\Router` to extend the `Phalcon\Mvc\Cli\RouterInterface` [#16551](https://github.com/phalcon/cphalcon/issues/16551)
1416
- Fixed `Phalcon\Filter\Validation\Validator\StringLength::validate()` to correctly use the `include` parameter [#16560](https://github.com/phalcon/cphalcon/issues/16560)
1517
- Fixed `Phalcon\Db\Column::TYPE_BINARY` and `Phalcon\Db\Column::TYPE_TINYINTEGER` to have unique values [#16532](https://github.com/phalcon/cphalcon/issues/16532)
18+
- Fixed `Phalcon\Forms\Form` to bind only existing properties on entities, based on `phalcon.form.strict_entity_property_check` setting. [#16567](https://github.com/phalcon/cphalcon/issues/16567)
1619

1720
### Removed
1821

config.json

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
"type": "bool",
7070
"default": false
7171
},
72+
"form.strict_entity_property_check": {
73+
"type": "bool",
74+
"default": false
75+
},
7276
"orm.ast_cache": {
7377
"type": "hash",
7478
"default": "NULL"

phalcon/Forms/Form.zep

+9-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,15 @@ class Form extends Injectable implements Countable, Iterator, AttributesInterfac
254254
/**
255255
* Use the public property if it doesn't have a setter
256256
*/
257-
let entity->{key} = filteredValue;
257+
if (!globals_get("form.strict_entity_property_check")) {
258+
let entity->{key} = filteredValue;
259+
260+
continue;
261+
}
262+
263+
if (property_exists(entity, key)) {
264+
let entity->{key} = filteredValue;
265+
}
258266
}
259267
}
260268

tests/unit/Forms/FormEntityCest.php

+62
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,66 @@ public function isValidEntity(UnitTester $I)
5151
$I->assertNotNull($validator->getEntity());
5252
$I->assertSame($product->prd_name, $validator->getEntity()->prd_name);
5353
}
54+
55+
/**
56+
* Tests Phalcon\Forms\Form :: bind()
57+
*
58+
* @author noone-silent <lominum@protonmail.com>
59+
* @since 2024-05-01
60+
*/
61+
public function bindDisabledStrictCheck(UnitTester $I): void
62+
{
63+
$I->wantToTest('Phalcon\Forms\Form - bind() with disabled strict property check');
64+
65+
$this->setNewFactoryDefault(); //create default DI
66+
67+
$product = new Products();
68+
69+
$I->assertFalse(property_exists($product, 'prd_not_exists'));
70+
71+
$form = new Form($product);
72+
$form->setTagFactory($this->container->get("tag"));
73+
$dynamic = new Text('prd_not_exists');
74+
$form->add($dynamic);
75+
$exists = new Text('prd_name');
76+
$form->add($exists);
77+
$form->bind(['prd_name' => 'Test', 'prd_not_exists' => 'TestValue'], $product);
78+
79+
$I->assertEquals('Test', $product->prd_name);
80+
$I->assertEquals('TestValue', $product->prd_not_exists);
81+
}
82+
83+
/**
84+
* Tests Phalcon\Forms\Form :: bind()
85+
*
86+
* @author noone-silent <lominum@protonmail.com>
87+
* @since 2024-05-01
88+
*/
89+
public function bindEnabledStrictCheck(UnitTester $I): void
90+
{
91+
$I->wantToTest('Phalcon\Forms\Form - bind() with enabled strict property check');
92+
93+
$this->setNewFactoryDefault(); //create default DI
94+
95+
$product = new Products();
96+
97+
$I->assertFalse(property_exists($product, 'prd_not_exists'));
98+
99+
// Set setting for GlobalsCest.php
100+
ini_set('phalcon.form.strict_entity_property_check', '1');
101+
102+
$form = new Form($product);
103+
$form->setTagFactory($this->container->get("tag"));
104+
$dynamic = new Text('prd_not_exists');
105+
$form->add($dynamic);
106+
$exists = new Text('prd_name');
107+
$form->add($exists);
108+
$form->bind(['prd_name' => 'Test', 'prd_not_exists' => 'TestValue'], $product);
109+
110+
// Reset setting for GlobalsCest.php
111+
ini_set('phalcon.form.strict_entity_property_check', '0');
112+
113+
$I->assertEquals('Test', $product->prd_name);
114+
$I->assertFalse(property_exists($product, "prd_not_exists"));
115+
}
54116
}

tests/unit/Mvc/GlobalsCest.php

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ private function getExamples(): array
5757
'setting' => 'phalcon.db.force_casting',
5858
'value' => '0',
5959
],
60+
[
61+
'setting' => 'phalcon.form.strict_entity_property_check',
62+
'value' => '0',
63+
],
6064
[
6165
'setting' => 'phalcon.orm.cast_last_insert_id_to_int',
6266
'value' => '0',

0 commit comments

Comments
 (0)