2015-06-12 65 views
0

只有當hidden_​​field的值與指定值匹配時,我如何才能執行驗證?Rails 4根據隱藏字段的值進行驗證

例如,我有兩種形式,每種都有一個單獨的提交按鈕和一個單獨的hidden_​​field,其值決定是運行一組驗證還是另一個驗證。

這個問題似乎與hidden_​​field檢查(如果我用一個真/假值代替其餘的代碼工作)。

型號:

validates :new_email, presence: true, length: { minimum: 1 }, on: :update, if: :email_update? 
    validates :new_password, presence: true, confirmation: true, length: { minimum: 1 }, on: :update, if: :password_update? 
    validates :new_password_confirmation, presence: true, on: :update, if: :password_update? 
    validate :password_retype, on: :update 

# before_save :copy_values 

    attr_accessor :remember_token, 
       :new_email, 
       :new_password, 
       :existing_password, 
       :update_type 

    def email_update? 
    update_type == "email_only" 
    end 

    def password_update? 
    update_type == "password_only" 
    end 

查看:

<h1>メールアドレス変更</h1> 

<%= form_for(@customer) do |f| %> 
    <p> 
    <%= f.label :new_email, "Email" %><br> 
    <%= f.email_field :new_email, placeholder: "Email" %> 
    </p> 
    <p> 
    <%= f.label :existing_password, "パスワード" %><br> 
    <%= f.password_field :existing_password, placeholder: "Password" %> 
    </p> 
    <p> 
    <%= f.submit "変更する" %> 
    <%= hidden_field :update_type, "email_only" %> 
    </p> 
<% end %> 

<h1>パスワード変更</h1> 

<%= form_for(@customer) do |f| %> 
    <p> 
    <%= f.label :existing_password, "現在のパスワード" %><br> 
    <%= f.password_field :existing_password, placeholder: "Old Password" %> 
    </p> 
    <p> 
    <%= f.label :new_password, "新しいパスワード" %><br> 
    <%= f.password_field :new_password, placeholder: "New Password" %> 
    </p> 
    <p> 
    <%= f.label :new_password_confirmation, "新しいパスワード(確認用)" %><br> 
    <%= f.password_field :new_password_confirmation, placeholder: "Confirm New Password" %> 
    </p> 
    <p> 
    <%= f.submit "変更する" %> 
    <%= hidden_field :update_type, "password_only" %> 
    </p> 
<% end %> 

控制器:

def edit 
    @error_messages = [] 
    @customer = Customer.find_by(params[:customerID]) 
    end 

    def update 
    @error_messages = [] 
    @customer = Customer.find_by(params[:customerID]) 
    if @customer.update(customer_params) 
    #if @customer.update_attributes(customer_params) 
     render "edit" 
    else 
     render "edit" 
    end 
    end 

    private 

    def customer_params 
    params.require(:customer).permit(:new_email, 
             :new_password, 
             :new_password_confirmation, 
             :existing_password, 
             :update_type) 
    end 

PARAMS:

--- !ruby/hash:ActionController::Parameters 
utf8: "✓" 
_method: patch 
authenticity_token: zlYVtgQ0LuhYEpM3cRNf9QwhH8+0THluUTEGxEYprybcJrilWKHiUkJ5ypo1JnKRsbtsDTUp+o1xuRr/Pbkp2w== 
customer: !ruby/hash:ActionController::Parameters 
    new_email: '' 
    existing_password: '' 
commit: "変更する" 
update_type: !ruby/hash:ActionController::Parameters 
    email_only: '' 
controller: customers 
action: update 
id: '2' 

這已經讓我在兩天的最佳時間裏st me不前。 :(

+1

,應該f.hidden_​​field –

+0

良好的漁獲@ChitrankSamaiya添加它作爲一個答案在第二個音符?!:爲什麼不使用'update_attributes'?不確定'update'是否是你想要的。 – nathanvda

+0

我可以親吻你!(加上改變''email_only''爲'value:'email_only'')似乎得到驗證工作預計!非常感謝! – Zild

回答

0

你應該爲什麼你不使用的form_for的hidden_​​field F對象做

<%= f.hidden_field :update_type, :value => "email_only or password_only" %>