solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 229 forks source link

Validation from class attributes #380

Open radvc opened 7 years ago

radvc commented 7 years ago

Right now Im using virtus in my rails project but Im having a problem to validate class attributes. This are the codes:

class NewAccountForm
 include Virtus
 extend ActiveModel::Naming
 extend ActiveModel::Conversion
 extend ActiveModel::Validations

  attribute :username, String
  attribute :user_type, String
  attribute :profile, ProfileAttribute

  validates_presence_of :username, :user_type
end
class ProfileAttribute
 include Virtus
 extend ActiveModel::Validations

 attribute :firstname, String
 attribute :middlename, String
 attribute :lastname, String
 attribute :email, String

  validates_presence_of :firstname, :middlename, :lastname, :email
end

my form Im using client_side_validation gem

= form_for @account, url: accounts_path, validate: true do |f|
   = f.text_field :username
   = f.fields_for :profile do |profile|
      = profile.text_field :firstname
      = profile.text_field :middlename
      = profile.text_field :lastname

I set validations in ProfileAttribute class but it didn't work. How can I get the validation from class attribute?