Partial model validation

67 views
Skip to first unread message

thomas sevestre

unread,
May 24, 2016, 6:06:25 PM5/24/16
to Ruby on Rails: Core
Hello,

I've been looking for a solution to perform partial validation of a model.
This is often an issue when you have multiple forms to fill a single model (ie with wicked wizard gem).
They are multiple complicated solutions on the web but it would be fairly easy to implement inside the validation framework.

I can provide a patch if interested.

This could be the API:

    my_object.save(partial_validation: array_of_attributes_to_validate)

The modified functions of active_record/validations (on branch 4.2)

    def valid?(context = nil, partial_validation = nil)
      context ||= (new_record? ? :create : :update)
      output = super(context)
      if partial_validation
        (errors.keys - partial_validation).each do |attr|
          errors.delete(attr)
        end
      end
      errors.empty? && output
    end

    def perform_validations(options={}) # :nodoc:
      options[:validate] == false || valid?(options[:context], options[:partial_validation].map(&:to_sym))
    end

Let me know if interested

Regards,
Thomas

Andrew Kaspick

unread,
May 24, 2016, 6:44:01 PM5/24/16
to Ruby on Rails: Core
You can use the context param or a "form object" if you only need to do partial validations.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-co...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

thomas sevestre

unread,
May 25, 2016, 2:54:43 AM5/25/16
to Ruby on Rails: Core
The context param is ok for some cases, ie for implementing a state machine like the business logic of an order.

But in my opinion the model should not change wether it is edited in a single form or in a wizard. You should not need to edit the model when you move an input field from a view to anther... This should be handled in the controller. With the proposed patch :

    def uptade
      ...
      my_object.assign_attributes(params)
      my_object.save(partial_validation: params.keys)
      ...
    end

Done !

Let me know if you are interested with a PR

richard schneeman

unread,
May 26, 2016, 12:30:06 PM5/26/16
to thomas sevestre, rubyonra...@googlegroups.com
I Not sure if you saw or not, I have docs for how to cobble this together with existing pieces. https://github.com/schneems/wicked/wiki/Building-Partial-Objects-Step-by-Step


-- 
Richard Schneeman

thomas sevestre

unread,
May 26, 2016, 1:51:08 PM5/26/16
to Ruby on Rails: Core, t...@tcare.fr
Yes I saw it, there is also this solution:
https://gist.github.com/kizzx2/4722784

I've published my current solution here:
https://gist.github.com/ThomasSevestre/82cec7ddfb01caa86b8082c3ac238496
Reply all
Reply to author
Forward
0 new messages