Overriding model validation in spree 1.1.3

690 views
Skip to first unread message

Jeremy Rottman

unread,
Aug 16, 2012, 7:14:20 PM8/16/12
to spree...@googlegroups.com
I am working on an extension and I have run into an issue with overriding validation of a model.

In the address model there is the validation for phone
validates :firstname, :lastname, :address1, :city, :zipcode, :country, :phone, :presence => true

For this store, we have no need for the phone to be stored. I have written a model decorator based on anther thread on this group.

This is what my decorator looks like. However I am still unable to override this model.

module Spree

  Address.class_eval do

    has_one :creditcard    

    def validate 

      errors = self.errors.map {|attr, message| [attr, message]} 

      self.errors.clear 

      errors.each do |attr, message

        self.errors.add(attr, message) unless attr == "phone" 

      end 

    end 

  end

end


This is the response I am getting back from spree:

{\"error\":\"The order could not be transitioned. Please fix the errors and try again.\",\"errors\":{\"bill_address.phone\":[\"can't be blank\"],\"ship_address.phone\":[\"can't be blank\"]}}", @response=#<Net::HTTPClientError 422  readbody=true>, @headers={"content-type"=>["text/html"], "x-ua-compatible"=>["IE=Edge"], "cache-control"=>["no-cache"], "x-request-id"=>["96ad34f13152b1d4b1ede587818c745f"], "x-runtime"=>["0.694875"], "content-length"=>["176"], "server"=>["WEBrick/1.3.1 (Ruby/1.9.2/2011-07-09)"], "date"=>["Thu, 16 Aug 2012 23:10:10 GMT"], "connection"=>["close"]}


Abhishek

unread,
Aug 16, 2012, 10:15:55 PM8/16/12
to spree...@googlegroups.com
Jeremy,
There has been a discussion on this before:
Have a look.

--
You received this message because you are subscribed to the Google Groups "Spree" group.
To view this discussion on the web visit https://groups.google.com/d/msg/spree-user/-/_57qh3nArFsJ.
To post to this group, send email to spree...@googlegroups.com.
To unsubscribe from this group, send email to spree-user+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spree-user?hl=en.

Jeremy Rottman

unread,
Aug 17, 2012, 11:31:32 AM8/17/12
to spree...@googlegroups.com
I referenced that exact post in my original post. This does not seem to work in Spree 1.1.x. The code that I posted is in fact the exact same code the original poster in that thread used. But it does not work anymore.

Abhishek

unread,
Aug 17, 2012, 3:14:57 PM8/17/12
to spree...@googlegroups.com
Here's what worked for me -

class MyValidator < ActiveModel::Validator 
  def validate(record)
    record.errors.messages.delete(:phone)
  end
end

Address.class_eval do
  include ActiveModel::Validations
  validates_with MyValidator
end



To view this discussion on the web visit https://groups.google.com/d/msg/spree-user/-/JApSBWcDNFwJ.
Reply all
Reply to author
Forward
0 new messages