Skip to content
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

alias_attribute error messages #1769

Open
cschilbe opened this issue Apr 21, 2022 · 0 comments
Open

alias_attribute error messages #1769

cschilbe opened this issue Apr 21, 2022 · 0 comments

Comments

@cschilbe
Copy link

cschilbe commented Apr 21, 2022

Environment

  • Ruby 2.6
  • Rails 6.0.4
  • Simple Form 5.1.0

Current behavior

When using active model attribute aliases, validation errors from the original field are not shown in forms. A model will not be valid if there are failing validations on the original attribute but details about the error won't be shown to users.

# app/models/example.rb
class Example
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :name
  alias_attribute :other_name, :name

  validates :name, presence: true

  ...
end
# app/views/example/new.html.erb
<%= simple_form_for @example,  do |f| %>
  <%= f.other_name %>
  <%= f.button :submit %>
<% end %>

Validation of the example would fail and no error would be generated for the other_name field.

Expected behavior

This is just to open up discussions about what might be more optimal behavior.

Current workaround - Explicitly duplicate validations in the model

Developers can add the same validation for the aliased attribute

# app/model/example.rb
...
  validates :other_name, presence: true

This does provide explicit control over validation for each field and allows for customized error messages easily for each attribute and alias and makes the most sense for simple validations. In cases where there may be many complicated validations performed on the underlying field this becomes unnecessary duplication and a possible problematic area down the road.

# app/model/example.rb
  validate :name_is_actual_name
  validate :name_is_not_test_name
  validate :name_isnt_banned
  ...

In this case, we may not want to duplicate the validate declarations and in this case, this might also mean needing to duplicate the methods used to perform the validation since they are specific to the attribute.

Display validation errors from aliased attribute

When fetching errors for an attribute, we could also check if the attribute is an alias and return any errors from the underlying attribute.

This could be done relatively easily with the following change to https://github.com/heartcombo/simple_form/blob/main/lib/simple_form/components/errors.rb#L54

def errors_on_attribute
  object.errors[attribute_name].presence || errors_on_alias || []
end

def errors_on_alias
  return nil unless object.attribute_aliases.key? attribute_name.to_s
  errors_on_explicit_attribute(object.attribute_aliases[attribute_name.to_s])
end

def errors_on_explicit_attribute(attribute)
  object.errors[attribute]
end

Because this may change the behavior of existing applications, an option could be added such as display_aliased_attribute_errors. This could be a simple_form configuration or could be an option set for the field in the view.

I'd be willing to cut a PR if the community feels this would be beneficial. Any feedback about options or configuration required is welcomed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant