reset password form validation in ruby on rails
reset password form validation in ruby on rails
I have an issue, I'm implementing the form validation on reset password form in ruby on rails, i don't have the email field on reset password form but email field exist in login form. when we click submit button, its show "Email must contain a valid email address.!" as well. Below the code of reset password form.
class Admin::PasswordResetsController < ApplicationController
layout 'template'
VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-.]+.[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
Below the code of user model..
class User < ApplicationRecord
#attr_accessor :id, :email, :password
before_create generate_token(:auth_token)
has_secure_password
VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-.]+.[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
Below the html code of edit.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>
1 Answer
1
You need to skip the validation of email in order to achieve this. Add a condition to tell Rails to check the email validation only when it is required like so.
validates :email, format: if: :email_required?, with: VALID_EMAIL_REGEX, message: 'must contain a valid email address.!'
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.