在Rails中重置密码表单验证

时间:2018-09-03 05:54:28

标签: validation ruby-on-rails-5

我有一个问题,我正在使用ruby on rails在重置密码表单上实施表单验证,我在重置密码表单上没有电子邮件字段,但是登录表单中存在电子邮件字段。当我们单击提交按钮时,其显示为“电子邮件必须包含有效的电子邮件地址。!”也一样在“重置密码”表单的代码下方。

class Admin::PasswordResetsController < ApplicationController

 layout 'template'  

 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

def new

end

def edit
  @page_title = 'Reset Password'
  @user = User.find_by_password_reset_token!(params[:id])
end

def update
  @user = User.new(update_params)
  if @user.valid?   
          @user = User.find_by_password_reset_token!(params[:id])
          if @user.password_reset_sent_at < 2.hours.ago
            flash[:danger] = "Your Password reset link has been expired!"
            redirect_to new_admin_password_reset_path
          elsif @user.update_attributes(update_params)
            flash[:success] = "Your Password has been successfully reset!"
            redirect_to admin_login_path
          else
            render :edit
          end
 else
      render 'edit'
 end
end

def update_params
    params.require(:user).permit(:password)
end

private
      def valid_email(email)
        email.present? && (email =~ VALID_EMAIL_REGEX)
      end

end

在用户模型代码下面。

  class User < ApplicationRecord
       #attr_accessor :id, :email, :password

        before_create { generate_token(:auth_token) }

        has_secure_password

        VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
        validates :email, format: { with: VALID_EMAIL_REGEX, message: 'must contain a valid email address.!' }
        #validates :password, presence: true
        #length: { minimum: 4, maximum: 16 }

        def send_password_reset
          generate_token(:password_reset_token)
          self.password_reset_sent_at = Time.zone.now
          save!
          UserMailer.password_reset(self).deliver
        end

        def generate_token(column)
          begin
            self[column] = SecureRandom.urlsafe_base64
          end while User.exists?(column => self[column])
        end

    end

edit.html的html代码下面

<% content_for :title, @page_title %>
      <div class="forgot" style="min-height:0px">
          <%= form_for @user, url: admin_password_reset_path(params[:id]), method: :patch, html: {class: "forget-form"} do |f| %>

            <% if @user.errors.full_messages.any? %>
                <% @user.errors.full_messages.reverse.each do |error_message| %>
                  <div class="alert alert-danger"><%= error_message %></div>
                <% end %>
            <% end %>


            <div class="form-group">
              <%= f.label :new_password %>
              <%= f.password_field :password, class: "form-control", placeholder: "New Password" %>
            </div>
            <div class="form-group">
              <%= f.label :password_confirmation %>
              <%= f.password_field :password_confirmation, class: "form-control", placeholder: "Confirm Password" %>
            </div>
            <div class="actions"><%= f.submit "Update Password", class: "btn btn-primary btn-block" %></div>
          <% end %>
      </div>

Below the screenshot of form.

1 个答案:

答案 0 :(得分:0)

您需要跳过电子邮件验证才能实现此目的。添加条件以告知Rails仅在需要时才检查电子邮件验证。

validates :email, format: { if: :email_required?, with: VALID_EMAIL_REGEX, message: 'must contain a valid email address.!' }