简体   繁体   中英

How can I make all fields required unless specified otherwise in Rails forms?

I have many forms. All of a fields required except one. Any way mark them required except one as optional?

Don't think there is a way to do this other than putting required: true for each field

Or Put a validation in your model for each column

validates :order, presence: true

There is no other built in way. You could write a wrapper that adds required: true to all fields by default. It is however, easiest to just put all params in the model and validate by

presence: true 

and add HTML to the fields

required: true 

Can you please let us know whether the normal validation by model is sufficient for your purposes? There is no need to add extra HTML attributes.

In simple_form gem you can set attributes to be required by default.

config.required_by_default = true

Then you can overwrite by using

, required: false

you could try to write such a line yourself if you do not use the SimpelForm gem.

If not sufficient please edit your question by stating what exactly you need. According to my knowledge there is only validation in rails as built in function.

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