Question

Here is the table:

|policies|
  |id| |name| |date|
    1    ABC   2013-01-01 
    2    DEF   2013-01-21

Here is the controller:

class PolicyController < ApplicationController
   def index
     @policies = Policy.find(:all,:conditions=>['date BETWEEN ? AND ?',params[cam],params[:cam2] ])
   end
end

Here is the model:

class Policy < ActiveRecord::Base
end 

Here is the view:

<% CalendarDateSelect.format=(:hyphen_ampm )%>
<% calendar_date_select_style "silver" %>
<% translation_calendar %>

<% form_tag :controller=>"policy",:action=>"index" do %>
  From: <%= calendar_date_select_tag  "cam", params[:cam]  %>

  To:   <%= calendar_date_select_tag  "cam2",params[:cam2] %>

  <%= submit_tag "Search", :name => nil %></p>
<% end %>

<% @policies.each |p| do %>
  <%= p.date %>
<% end %>      

How can block the SEARCH button until my 2 calendar text have values?

I tried this:

class Policy < ActiveRecord::Base
    validates_presence_of :cam
end

Please somebody can help me please or maybe a javascript?

I will appreciate all kind of help.

Here is what friends told me:

 named_scope :by_calendar_date,lambda { |cam1, cam2| }  {
    if cam1.nil? && cam2.nil?
       scoped
    elsif cam1.nil?
       where("date < ?",cam2)
    elsif cam2.nil?
       where("date > ?", cam1)
    else
       where("date BETWEEN ? AND ?",cam1,cam2)
    end
}
   #this would perform the same functionality so both are not needed but just for edification
def self.by_calendar_date(cam1,cam2)
    if cam1.nil? && cam2.nil?
       scoped
    elsif cam1.nil?
       where("date < ?",cam2)
    elsif cam2.nil?
       where("date > ?", cam1)
    else
       where("date BETWEEN ? AND ?",cam1,cam2)
    end
end 
Was it helpful?

Solution 2

Check out this fiddle... http://jsfiddle.net/qKG5F/641/

HTML

// note the change... I set the disabled property right away
<input type="submit" id="register" value="Register" disabled="disabled" />

JavaScript

(function() {
    $('form > input').keyup(function() {

        var empty = false;
        $('form > input').each(function() {
            if ($(this).val() == '') {
                empty = true;
            }
        });

        if (empty) {
            $('#register').attr('disabled', 'disabled'); // updated according to http://stackoverflow.com/questions/7637790/how-to-remove-disabled-attribute-with-jquery-ie
        } else {
            $('#register').removeAttr('disabled'); // updated according to http://stackoverflow.com/questions/7637790/how-to-remove-disabled-attribute-with-jquery-ie
        }
    });
})()

The nice thing about this is that it doesn't matter how many input fields you have in your form, it will always keep the button disabled if there is at least 1 that is empty. It also checks emptiness on the .keyup() which I think makes it more convenient for usability.

I hope this helps.

OTHER TIPS

Since this is a search feature you do not need to validate in my opinion you could just do this

class Policy < ActiveRecord::Base
    named_scope :by_calendar_date,lambda{|cam1,cam2|
        if cam1.nil? && cam2.nil?
           {:conditions => {}}
        elsif cam1.nil?
           {:conditions => ["date < ?",cam2]}
        elsif cam2.nil?
           {:conditions => ["date > ?", cam1]}
        else
           {:conditions => ["date >= ? AND date <= ?",cam1,cam2]}
        end
   }     
end 

call with

def index
  @policies = Policy.by_calendar_date(params[:cam1],params[:cam2])
end

This way the user can set either param individually or none at all

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top