Problem/Motivation
PHP's empty() function is not enough to check for $settings['min'] or $settings['max'] for numeric item - \Drupal\Core\Field\Plugin\Field\FieldType\NumericItemBase::getConstraints(), because their value can be numeric zero, and in such case constraint is not returned.
This results in possible invalid numeric values for items that have min or max boundary set to 0.
Rough example:
Custom entity class, baseFieldDefinitions()
$fields['hour'] = BaseFieldDefinition::create('integer')
->setLabel(new TranslatableMarkup('Hour'))
->setSetting('min', 0)
->setSetting('max', 23);
Custom code
$e = CustomEntity::create([ 'hour' => -100 ]);
$violations = $e->validate();
Validation passes w/o errors, because NumericItemBase::getConstraints() does not return constraint to check for minimum.
Proposed resolution
Replace !empty() check with check for non-empty string (because an empty string is default value of min/max settings):
if ($settings['min'] !== '') {}
| Comment | File | Size | Author |
|---|---|---|---|
| #26 | interdiff-24-26.txt | 3.01 KB | drugan |
| #26 | 2838121-26.patch | 3.76 KB | drugan |
| #24 | 2838121-24.patch | 4.3 KB | drugan |
| #19 | 2838121-19.patch | 1.87 KB | joe_carvajal |
| #10 | interdiff-7-10.txt | 1.62 KB | shadcn |
Comments
Comment #2
Anonymous (not verified) commentedWhat if we replace the
emptyto the isEmpty? E.g. for cases where min/max = NULL?Comment #3
pingwin4eg@vaplas isEmpty() checks the $value property of an object, but the bug is in settings, not the value. Also min/max are never NULL. If min/max settings are not set, then default values of $settings['min'] and $settings['max'] are empty strings.
Comment #4
Anonymous (not verified) commented@pingwin4eg, thanks for the explanation. Your suggestion makes sense to me.
Comment #7
Anonymous (not verified) commentedComment #9
amateescu commentedSince
is_numeric()returns FALSE (https://3v4l.org/523Qg) forNULLand''(an empty string), let's drop theempty()check and just keep theis_numeric()one.Let's use the short array syntax here.
Comment #10
shadcn commentedUpdated as per #9
Comment #11
amateescu commentedGreat!
Comment #12
cilefen commentedI am definitely for this fix in principle, but I noticed that one cannot POST out-of-range values even with the max or min set to 0, because the Drupal\Core\Render\Element\Number::validateNumber prevents it with its own validation code. So we have two range validations in core: one for form posting and a lower-level one for fields themselves when we are not dealing with forms. I find this odd. That's not a reason not to fix this, but I'd like to hear if anybody knows the history and would like to sort this out, maybe in a follow-up.
My question is: Why isn't Drupal\Core\Render\Element\Number::validateNumber using the same constraint code as NumericItemBase and is that something we want?
edited
Comment #13
drugan commentedThis issue is fixed here:
#2816859: Allow the 'step' to be configured as a NumberWidget setting
Additionally per form display mode number widget settings are introduced. Also, all the number field types now have unsigned storage setting on the UI. Also, the size storage setting is exposed on the UI.
Comment #14
pingwin4eg@drugan Actually, not. I reviewed your patch from #2816859-25: Allow the 'step' to be configured as a NumberWidget setting (the part with
NumericItemBase::getConstraints()) and there you do the following:and your
Numeric::toStringbasically does:return (string) $number;Now imagine there's a 0 (zero) in that setting. Constraints are still not returned.
P.S.: That's a big patch, in which you make a lot of changes and introduce new APIs. IMHO you should break it down into multiple smaller issues (like this one). This way it'd be faster for others to review/rewrite small patches.
Comment #15
pingwin4egSo where did we stuck here?
@cilefen Concerning your worries, I think that should not hold this fix. A follow-up issue can be created.
#10 patch looks good to me too.
Comment #17
pingwin4egFail reason: Line numbers doesn't match.
Comment #18
joe_carvajalI'll reroll it!
Comment #19
joe_carvajalHum, looks strange, but the patch applied perfectly for me. I reupload it with a minor difference (array short syntax and commit ref id).
Comment #20
joe_carvajalComment #21
pingwin4egThen I guess the dreditor (or cgit) shows wrong line numbers, and patch #10 failed because of array syntax (though it's in unchanged lines).
Current Drupal core -
if (!empty($settings['min'])) {is on 82nd line in cgit. And dreditor shows it is on 83rd.Comment #22
drugan commented@pingwin4eg
Sorry for getting you confused. Yes, you're right there: if $min = '0' then the constraint will not work. I am going to make this change in my patch.
BTW, I am also subscribing for RTBC on the #10 patch.
Comment #23
alexpottI think we should add further tests of what happens when one is not set. I.e something like:
For both min and max case.
Also when both min and max are 0 I think it is worth adding something like:
I spent a long time thinking about whether this is the right place for this test and finally concluded it is after seeing the tests in \Drupal\KernelTests\Core\TypedData\TypedDataTest::testGetAndSet().
Comment #24
drugan commented@alexpott
Did you mean this?
Comment #25
alexpott@drugan yep the new tests are looking good.
This change whilst technically correct is not in scope for this patch.
Thinks about the code we are test - rather than validating a 0 value here we should use a value of 5.
Comment #26
drugan commented@alexpott
Yes, I agree on that. We should check the value which is equal to a setting, then the value which is greater by one and then the value which is less by one than the setting.
Comment #27
alexpottThe test changes look good good.
I pondered whether this might introduce php errors if
minis not set. It would, but these keys are added in \Drupal\Core\Field\Plugin\Field\FieldType\NumericItemBase::defaultFieldSettings() so they have to be set. So all good here as far as I can see.Comment #28
drugan commented@alexpott
What I've found on number field settings is the following:
The FieldItemBase::defaultSettings() return is empty array. The NumericitemBase class merges its default min and max settings which are empty strings with FieldItemBase::defaultSettings(). The IntegerItem class merges its default min and max which are empty strings with NumericitemBase::defaultSettings(). Therefore min and max double ensured they exist and have an empty string value by default.
The NumericItemBase->getSettings() return is the result of the following merge:
FieldItemBase::defaultSettings() + NumericitemBase::defaultSettings() + IntegerItem::defaultSettings() + IntegerItem::defaultStorageSettings() + actual field settings and field storage settings saved on the UI form.
So, if the field is created with all default values it has min and max values as NULL.
Why is not empty string?
This is because Number::validateNumber() will not even try to validate the empty string which further goes to FormElement::valueCallback() to be returned as NULL, disregarding of whatever the $input value is. That's how default min and max string values become NULL values after the field being saved empty on a form.
The conclusion of the above is:
The min and max settings are always exist in the NumericItemBase->getSettings() return either as empty strings (in code) or NULL values or actual numeric values saved on those fields.
Comment #29
drugan commentedThe #26 fix is merged into the latest patch of:
#2816859: Allow the 'step' to be configured as a NumberWidget setting
So, if you want to get more than just properly working constraints on the number filed, please take the patch in the link for testing.
Comment #41
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
Could someone confirm #3090629: NumericItemBase never sets minimum value constraint if minimum value is zero. solves this issue?
Comment #43
quietone commentedThe issue summary at #3090629: NumericItemBase never sets minimum value constraint if minimum value is zero. does look like it is solving the same problem and so does the code changes. I doubled checked and tested as well. I am closing this as a duplicate and moving credit over there.
Cheers