简体   繁体   中英

Rails validates_presence_of with multiple fields

I have multiple fields that should be validated with presence option. I am trying to use validates_presence_of instead of validates .

Here is my code

validates_presence_of : first_name, last_name, birthday, sex,home, message: "This field shouldn't be blank"

But I want to show field name in the message. How can I show field name in message?

If you remove the message hash, and just use

validates_presence_of :first_name, :last_name, :birthday, :sex, :home

This will display a message containing all the field names that are blank when you are saving your object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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