Added ability to override class name for uniqueness validator #520

Merged
merged 1 commit into from Apr 2, 2013

4 participants

@arr-ee

Closes #519

Added :client_validations option to the validate_uniqueness_of,
where one can specify the real class name for the remote validation.

For example, we have a wrapper class for the User model, UserForm, and
it uses remote uniqueness validation for the email field.

class UserForm
  include ActiveRecord::Validations
  attr_accessor :email
  validates_uniqueness_of :email
end

However, this won't work since middleware will try to perform validation
against UserForm, and it's not persisted.

Now one can state which class should be used for validation:

class UserForm
  include ActiveRecord::Validations
  attr_accessor :email
  validates_uniqueness_of :email, :client_validations => { :class =>
  User }
end
@coveralls

Coverage remained the same when pulling 9bf286d on arr-ee:override_class_for_uniqueness_validator into b674e02 on bcardarella:3-2-stable.

View Details

@bcardarella bcardarella commented on an outdated diff Apr 2, 2013
+end
+...
+<% form_for(UserForm.new) do %>
+...
+```
+
+However, this won't work since middleware will try to perform validation against UserForm, and it's not persisted.
+
+This is solved by passing `client_validations` options hash to the validator, that currently supports one key — `:class`, and setting correct name to the form object:
+
+```ruby
+class UserForm
+ include ActiveRecord::Validations
+ attr_accessor :email
+ validates_uniqueness_of :email, :client_validations => { :class =>
+ User }
@bcardarella
bcardarella added a line comment Apr 2, 2013

We should suggest the developer use a string version of the class. So 'User' instead of User. Because the validations occur on the class level we are dealing with load order issue for the code evaluation. For example, if the form class was AccountForm and it was referencing a User class the app will throw an error on production as the lazy loading of classes is only in development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bcardarella

This looks good! Merge away 👍

@arr-ee arr-ee Added ability to override class name for uniqueness validator
Added `:client_validations` option to the `validate_uniqueness_of`,
where one can specify the real class name for the remote validation.

For example, we have a wrapper class for the User model, UserForm, and
it uses remote uniqueness validation for the email field.

```
class UserForm
  include ActiveRecord::Validations
  attr_accessor :email
  validates_uniqueness_of :email
end
```

However, this won't work since middleware will try to perform validation
against UserForm, and it's not persisted.

Now one can state which class should be used for validation:

```
class UserForm
  include ActiveRecord::Validations
  attr_accessor :email
  validates_uniqueness_of :email, :client_validations => { :class =>
  User }
end
```
b045df6
@coveralls

Coverage remained the same when pulling 5310341 on arr-ee:override_class_for_uniqueness_validator into b674e02 on bcardarella:3-2-stable.

View Details

@arr-ee arr-ee merged commit c84462f into DavyJonesLocker:3-2-stable Apr 2, 2013
@coveralls

Coverage remained the same when pulling b045df6 on arr-ee:override_class_for_uniqueness_validator into b674e02 on bcardarella:3-2-stable.

View Details

@midu

Hi, I work with ilya (the guy who submitted the issue) and I think you guys deserve some hearts: ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️ ❤️

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