adding a custom validation

300 views
Skip to first unread message

Alex

unread,
Sep 15, 2011, 2:15:18 AM9/15/11
to Spree
Is there a good way to add a custom validation that would hook into
Spree's processing code? I have a page where the user needs to select
a value from a dropdown, and I will then assign a taxon to the new
Product being created based on their selection. And I want to make
sure that they do select something (I want the dropdown to start with
the blank option selected, so as to force the users to choose
something).

I can add a custom validate function to check the state of things, but
is there a way to properly hook up any resulting errors to Spree's
standard error handling method, so that the user is redirected back to
the original page and the field(s) in question are highlighted?

Thanks,
Alex Slepak

Brian Quinn

unread,
Sep 15, 2011, 3:57:56 AM9/15/11
to spree...@googlegroups.com
Hi Alex,
Your description is a little vague, but it mentions you are creating a product so you could just create a product_decorator.rb and include a standard active record validation in there?

#app/models/product_decorator.rb

Product.class_eval do
validates #blah 
end


Hope that helps.

Brian Quinn

-------------------------------------------
Rails Dog LLC
2 Wisconsin Circle, Suite 700
Chevy Chase, MD 20815
-------------------------------------------

--
You received this message because you are subscribed to the Google Groups "Spree" group.
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.

Alex

unread,
Sep 15, 2011, 5:22:21 PM9/15/11
to Spree
Thanks Brian. I haven't had much trouble adding that sort of
validation, but I ran into some issues and questions when I added an
actual validate function that's supposed to do some custom validation.
Specifically, in my case, I'm checking if certain taxons had been
added to the new product. It looks like the validate code is actually
being hit, and the error flash message is showing up when it is, is
there also a way to associate these checks with specific fields in my
form?

The specific setup I have is a list of Product Categories, stored
internally as taxons, are presented to the user as a dropdown list
when creating a new product. By default, the dropdown is selecting an
empty option, so they have to go in and pick one. If they don't, I
want to be able to catch this error in my validation (by noting that
no taxon from the Categories taxonomy had been assigned to my new
product), flag it, send the user back to that same page with a flash
error, and highlight the dropdown control telling them that they need
to make a selection. At this point, I think I've been able to
accomplish most of that, except for highlighting the dropdown field...

Thanks!
Alex
> > To post to this group, send email to spree...@googlegroups.com (mailto:spree...@googlegroups.com).
> > To unsubscribe from this group, send email to spree-user+...@googlegroups.com (mailto:spree-user+...@googlegroups.com).

Christopher Maujean

unread,
Sep 15, 2011, 7:42:35 PM9/15/11
to spree...@googlegroups.com

Alex,
You would use something like @product.errors.on(:field, "message")

--Christopher

> To post to this group, send email to spree...@googlegroups.com.
> To unsubscribe from this group, send email to spree-user+...@googlegroups.com.

Brian Quinn

unread,
Sep 16, 2011, 4:39:18 AM9/16/11
to spree...@googlegroups.com
You can just check the params hash in the update / create action, and like Chris said, use errors.on to associate your custom error with the model.

Also, be sure to render (and not redirect) as @product.errors will be cleared (unlike flash) on a redirect.



Brian Quinn

-------------------------------------------
Rails Dog LLC
2 Wisconsin Circle, Suite 700
Chevy Chase, MD 20815
-------------------------------------------

Reply all
Reply to author
Forward
0 new messages