[Form] Add BoundsType to render a lower and an upper bound - #65928
[Form] Add BoundsType to render a lower and an upper bound#65928seb-jean wants to merge 1 commit into
Conversation
|
I think the idea is nice but I'm not sold to the current name. Does a Actually, you used |
44742cf to
2598dea
Compare
|
Also please note that new translation keys in .xlif files will be added in another PR targeting the oldest maintained branch (6.4 right now), not this one |
So I remove the |
|
Yes, this PR should only use plain English values in your classes. If this gets accepted, you'll be able to create a new PR for translations. This eases our work of maintenance between branches |
2598dea to
85196e6
Compare
85196e6 to
d5c586c
Compare
I actually opted for a new type of form since |
c0bc059 to
bbf348b
Compare
|
This does not belong to RangeType. Using an option to change between 2 totally unrelated form structures (one dealing with a single numeric value, the other one dealing with an array with |
Okay, I'm going to go back to how things were before, with a new type of form. |
|
Oh yeah, that's right, I misremembered what RangeType is used for. Sorry for leading you in the wrong direction! |
bbf348b to
f330bbe
Compare
dc97078 to
5fb9fe5
Compare
|
Done. The PR is back to a dedicated The naming question is still open: |
yceruto
left a comment
There was a problem hiding this comment.
LGTM, this would removes the mechanical boilerplate of defining two fields.
My main concern is that the more important part "validating their ordering" is still left to the caller. The tests show this clearly, with the same Callback repeated to enforce from <= to.
I think this new type would be much more useful if it provides an optional ordering check out of the box, with a callable for types it cannot compare directly:
$builder->add('price', BetweenType::class, [
'type' => MoneyType::class,
'compare' => true, // or a callable for inner types the component cannot order
]);This would remove the boilerplate that is easier to get wrong and make the type more valuable as a core abstraction.
5fb9fe5 to
bf0d191
Compare
aa744f1 to
f78ccf3
Compare
f78ccf3 to
c5861d8
Compare
c5861d8 to
2d34187
Compare
BoundsTyperenders a lower and an upper bound of the same inner type, the wayRepeatedTyperenders the same type twice. The inner type is an option and carries its own configuration:Options reach both bounds through
options, and a single bound throughfrom_options/to_options, rather than through a hand-maintained list of forwarded options asDateTypedoes for its three children.The bounds are two plain children named
fromandto, with no view transformer on the parent, so anarray{from: mixed, to: mixed}property and an object exposing the two bounds both map, and keys the type knows nothing about survive a round trip. A model that names them otherwise is reached withproperty_path:A range whose bounds are all empty reverts to
nullon submission, the way an entirely empty date does inDateType; a half-open range is meaningful and is kept as is.comparechecks the ordering.trueorders scalars,DateTimeInterfaceand enums natively, enums following their declaration order, which is the order the inner type lists them in:The bounds are compared in their normalized form, the shape the inner type reasons in rather than the one the model ends up holding. A
DateTypeis therefore ordered as a date whatever itsinputoption turns it into, includingstringwith aninput_formatthat does not sort lexicographically, and aChoiceTypelisting enum cases is ordered likeEnumType.Anything else takes a callable, since the component cannot order arbitrary inner types:
It reports
compare_messageon the lower bound, the one a reader has to move to make the range valid. Validation stays available for everything the type cannot express:BoundsTypeValidatorExtensionmaps what validation says about the range as a whole onto the lower bound too, the wayRepeatedTypeValidatorExtensionmaps onto the first field.Since the last round:
BetweenTypetoBoundsType(@yceruto)from_name/to_nameare gone: the children are alwaysfromandto, andproperty_pathcovers a model that names them otherwise (@stof)compareoption (@yceruto), which orders enums natively so thatEnumTypeneeds no callablesetAllowedValues('compound', true)rather than redefining the default, and a shorter docblock (@yceruto)One point on
compareI would like settled in review:trueis inclusive, as the review asked for (from <= to). A strictfrom < tocurrently needsstatic fn ($from, $to): int => $from < $to ? -1 : 1, which reads poorly. Should the callable be a predicate returning whether the bounds are ordered, rather than a comparator?