I’ve tried setting up a form validation that would ensure that at least 1 and at most 3 tags must be included in the form. But it isn’t working as an empty form is still processed, but a form with 4 comma-seperated tags is validated correctly.
Controller
def update
@product = Product.find(params[:id])
current_user.tag(@product, :with => params[:product][:tag_list], :on => :tags)
if @product.update_attributes(params[:product])
redirect_to :root, :notice => "Added"
else
render :action => 'edit'
end
end
Form
<%= form_for @product do |f| %>
<%= f.label :tag_list, "Your tags" %> <%= f.text_field :tag_list, :value => @product.tags_from(current_user) %>
<p><%= f.submit "Change" %></p>
<%= f.error_messages %>
<% end %>
Model
validate :required_info
validates_size_of :tag_list,
:maximum => 3
private
def required_info
if( tag_list.empty? and description.empty? )
errors.add_to_base "Add one"
end
end
Just looking at this part of the model, I think you’d rather do
if(tag_list.empty? or description.empty?)because you want both of them to be filled.For the second validation, I’m not an
act_as_taggableuser so I can’t answer you now.