简体   繁体   English

如何验证视图中的参数?

[英]How can validate params in the view?

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? 在我的2个日历文本具有值之前,如何阻止SEARCH按钮?

I tried this: 我尝试了这个:

class Policy < ActiveRecord::Base
    validates_presence_of :cam
end

Please somebody can help me please or maybe a javascript? 请有人可以帮助我,或者可以使用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 

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 这样,用户可以单独设置参数或完全不设置参数

Check out this fiddle... http://jsfiddle.net/qKG5F/641/ 看看这个小提琴... http://jsfiddle.net/qKG5F/641/

HTML HTML

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

JavaScript 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. 这样做的好处是,与表单中有多少输入字段无关,如果至少有1个为空,它将始终使按钮保持禁用状态。 It also checks emptiness on the .keyup() which I think makes it more convenient for usability. 它还会检查.keyup()上的.keyup() ,我认为这样会使可用性更方便。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM