kirqe
kirqe

Reputation: 2470

How to skip validation of a few model fields

I have a model User.

Here're a few validation fields:

validates :first_name, presence:{ message: "please add your First name"}
validates :last_name, presence:{ message: "please add your Last name"}
validates :username, presence:{ message: "please pick a username"},

When I try to sign up using devise, I have a message that I should provide first name and last name. But for registration I only want to use 3 fields shown: username, email, password.

How to skip validation of first and last names during registration? Thanks.

enter image description here

Upvotes: 2

Views: 350

Answers (1)

Kirti Thorat
Kirti Thorat

Reputation: 53038

You don't want the validations for first_name, last_name to run while creating a new user BUT you want them while updating an existing user record. All you need to do is , modify the validations such that they will only be run while updating a user record.

You can do this by using on: :update option as below:

validates :first_name, presence:{ message: "please add your First name"}, on: :update
validates :last_name, presence:{ message: "please add your Last name"}, on: :update

Upvotes: 1

Related Questions