-
Notifications
You must be signed in to change notification settings - Fork 22
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
govuk_date_field caused non-numerical values to automatically be cast to 0
#489
Comments
Hey @patrick-laa, thanks for reporting this. I just had a play and wasn't able to get it to work without the
I removed the diff --git a/lib/govuk_design_system_formbuilder/elements/date.rb b/lib/govuk_design_system_formbuilder/elements/date.rb
index b7f574b..84b0a2c 100644
--- a/lib/govuk_design_system_formbuilder/elements/date.rb
+++ b/lib/govuk_design_system_formbuilder/elements/date.rb
@@ -8,7 +8,7 @@ module GOVUKDesignSystemFormBuilder
include Traits::Supplemental
include Traits::HTMLClasses
- SEGMENTS = { day: '3i', month: '2i', year: '1i' }.freeze
+ SEGMENTS = { day: '3', month: '2', year: '1' }.freeze
MULTIPARAMETER_KEY = { day: 3, month: 2, year: 1 }.freeze
def initialize(builder, object_name, attribute_name, legend:, caption:, hint:, omit_day:, maxlength_enabled:, form_group:, date_of_birth: false, **kwargs, &block) I haven't delved right into how Rails deals with multiparameter attributes behind the scenes but based on this comment it looks like it's expecting either a integer or float only. |
@peteryates so the issue you have in your error is that the month is being represented as the string "March" rather than the string "3". Without seeing your attributes I can't comment on what might be going on there beyond that. The comment in the Rails codebase you highlighted says "You can also specify a typecast character" (my italics) - you can see here in the ternary operator that if one isn't provided, the input string is left as a string. In our codebases we've built a couple of custom multiparam fields that don't use |
Ah, that makes sense. I didn't realise you'd created and integrated a custom type too! I'll have a closer look. Really appreciate this by the way, wasn't sure of the best approach for tackling this problem and this looks most promising. |
This change should make it easier for people to add custom date validation by giving them control of the identifiers used by Rails' multiparameter fields. In the linked PR the i (which tells Rails to cast to an Integer) is removed. ministryofjustice/laa-submit-crime-forms#681 Refs #489
This change should make it easier for people to add custom date validation by giving them control of the identifiers used by Rails' multiparameter fields. In the linked PR the i (which tells Rails to cast to an Integer) is removed. ministryofjustice/laa-submit-crime-forms#681 Refs #489
I've released the change that makes teh segments configurable last week and have just pushed a follow up that makes those defaults overridable. |
In general, if a user enters invalid data in a form field, on submission we want to re-render the form with what they entered pre-populated as entered, and an error message saying what is wrong with it.
Because govuk_date_field appends an
i
to the identifier of each field (c.f. https://github.com/x-govuk/govuk-form-builder/blob/main/lib/govuk_design_system_formbuilder/elements/date.rb#L11), the name of the field comes out as something likeperson[date_of_trade(3i)]
, and thati
tells Rails to cast the provided value as an integer before it is assigned to an attribute on a model (c.f. https://github.com/rails/rails/blob/d90b4e2/activerecord/lib/active_record/base.rb#L1884) , meaning that by the time validations run,"four"
has been cast to0
, and there's no way for a standard validator to know that what was originally entered was a string, so the best we can do is show a validation saying "The value must be greater than zero" instead of "The value must be a number".If you removed that
i
, then it would be up to the attribute type to cast to integer or not, and that would allow non-valid strings to be left as strings, allowing for a more intuitive UX when showing an error message.The text was updated successfully, but these errors were encountered: