Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Validate user to keep atleast one dynamic field. #197

Closed
usmanarif opened this issue Sep 21, 2012 · 3 comments
Closed

Validate user to keep atleast one dynamic field. #197

usmanarif opened this issue Sep 21, 2012 · 3 comments

Comments

@usmanarif
Copy link

I ma trying to put the validation on dynamic field such that the user can't delete all the dynamic fields from the form. I have tried number of things, but still not getting it done. Is there any solution for that?

@lest
Copy link
Collaborator

lest commented Sep 21, 2012

Could you please explain your issue with some more details? Providing an example application reproducing your issue would be very helpful.

@usmanarif
Copy link
Author

I have got two problems:

  1. How to put the validation so that at least on dynamic nested field (couponrange in my case) should be present in the offers form and user can't remove it.
  2. Is there a way for dynamic text field such it appears with dynamic value every time. For eg: Suppose I have 3 fields "from", "to" & "discount". For the first it appears blank and the user will enter some values such as "1", "5", "25" respectively. Now from the next time onward the "from" field should appear with one successive value compare to "to" field. In this case the "from" field will be "6" for second time. Is it possible in rails to achieve this. I have used nested form to show fields dynamically on click of "Add Another" link. I know I have to write the custom validator but unable to figure it out. Any help will be grateful. Following is my code:

newoffer.html.erb

<%= f.fields_for :couponranges do |builder| %>
<%= render "couponrange_fields", :f => builder %>
<% end %>

<%= link_to_add_fields "Add another range", f, :couponranges %>

offers_controller

def newoffer
1.times do
couponrange = @offer.couponranges.build
end
end

application_helper

module ApplicationHelper
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end

def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, "#{association}", "#{escape_javascript(fields)}")")
end

end

application.js

function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
}

function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}

_couponrange_fields.html.erb

<%= f.label :from, "Coupon value from " %> <%= f.text_field :from, :size => "6" %> <%= f.label :to, "Coupon value to "%> <%= f.text_field :to, :size => "6" %> <%= f.label :from, " Discount offered "%> <%= f.text_field :discount, :size => "6"%> <%= link_to_remove_fields "Remove", f %>

@lest
Copy link
Collaborator

lest commented Sep 22, 2012

It doesn't seem to be a nested_form issue as you're using your own helpers and javascript. I'd recommend using StackOverflow for such questions. Thanks!

@lest lest closed this as completed Sep 22, 2012
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants