Comments (6)
If now explained in that way it makes sense, can you do the PR?
Yes, tomorrow morning - I'm at the phone now. Thanks
from form.
Can you give an example of what you want to do since collectAttributes()
returns all the attributes of the formModel:
Line 250 in ebf4532
from form.
Can you give an example of what you want to do since
collectAttributes()
returns all the attributes of the formModel:Line 250 in ebf4532
My current realization (writed today) in abstract class AbstractSearchForm extends FormModel
. I'm using this to send as data in Vue form component. Or store in DB - when user save selected filters to quick access in future
public function toArray(bool $hideEmpty = true): array
{
$array = [];
/**
*I think need $this->attributes or sometihng like $this->getAttributeNames() instead of ReflctionClass/ReflectionProperty
*/
$class = new ReflectionClass($this);
$properties = $class->getProperties(ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property) {
if ($property->isStatic()) {
continue;
}
$name = $property->getName();
$value = $this->getAttributeValue($name);
if ($value === null || is_scalar($value)) {
if ($hideEmpty && ($value === '' || $value === null)) {
continue;
}
$array[$name] = $value;
} elseif (is_array($value)) {
if ($hideEmpty && count($value) === 0) {
continue;
}
$array[$name] = $value;
} elseif ($value instanceof FormModel) {
$nested = $value->toArray($hideEmpty);
if ($hideEmpty && count($nested) === 0) {
continue;
}
$array[$name] = $nested;
}
}
return $array;
}
from form.
Can you give an example of what you want to do since
collectAttributes()
returns all the attributes of the formModel:
Line 250 in ebf4532
My current realization (writed today) in
abstract class AbstractSearchForm extends FormModel
. I'm using this to send as data in Vue form component. Or store in DB - when user save selected filters to quick access in futurepublic function toArray(bool $hideEmpty = true): array { $array = []; /** *I think need $this->attributes or sometihng like $this->getAttributeNames() instead of ReflctionClass/ReflectionProperty */ $class = new ReflectionClass($this); $properties = $class->getProperties(ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC); foreach ($properties as $property) { if ($property->isStatic()) { continue; } $name = $property->getName(); $value = $this->getAttributeValue($name); if ($value === null || is_scalar($value)) { if ($hideEmpty && ($value === '' || $value === null)) { continue; } $array[$name] = $value; } elseif (is_array($value)) { if ($hideEmpty && count($value) === 0) { continue; } $array[$name] = $value; } elseif ($value instanceof FormModel) { $nested = $value->toArray($hideEmpty); if ($hideEmpty && count($nested) === 0) { continue; } $array[$name] = $nested; } } return $array; }
Why do a getAttributes()
, if calling collectAttributes()
has the same attributes, i don't understand.
Line 38 in ebf4532
from form.
Why do a
getAttributes()
, if callingcollectAttributes()
has the same attributes, i don't understand.Line 38 in ebf4532
It will be call twice (or N times) and unnecessary recalculate attribute types just for get names. I can call collectAttributes
in AbstractSearchModel::__construct
, but for what? We already have this array, maybe just add method like in AR
public function attributes(): array
{
return array_keys($this->attributes);
}
from form.
Why do a
getAttributes()
, if callingcollectAttributes()
has the same attributes, i don't understand.
Line 38 in ebf4532
It will be call twice (or N times) and unnecessary recalculate attribute types just for get names. I can call
collectAttributes
inAbstractSearchModel::__construct
, but for what? We already have this array, maybe just add method like in ARpublic function attributes(): array { return array_keys($this->attributes); }
If now explained in that way it makes sense, can you do the PR?
from form.
Related Issues (20)
- Wrong html input pattern for Url::rule HOT 11
- Allow null value for input id and label for attributes
- Widgets should not overwrite explicitly set options
- URL rule regex does not work in HTML field when IDN is enabled
- Use friendly exception for exception "Form model is not set."
- Implement template for each widget from factory. HOT 3
- Use array_key_exists instead of isset on check value
- Adapt after removing result set in validator
- Rename FormErrors to FormErrorSet (and similar) - consistency, best practices HOT 1
- Nested::rule and nested forms validation fails
- Make use of PHP 8 attributes for labels, hints, etc.
- Nested rule - combine dot notation and nested structure
- Yiisoft\Form\Widget\Validator\FieldValidator::getValidatorAttributes() shouldn't apply required attribute for Required rule indiscriminately
- Remove filter of null values in methods for setting classes HOT 1
- Code style - chain calls HOT 15
- Add `$content` parameter to `Field::*Button()` methods
- Yiisoft\Validator\DataSet\AttributeDataSet not found
- Dependency update/adjust to yiisoft/html HOT 2
- Nested array form with translations HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from form.