jeudi 6 avril 2023

ruby - validates if empty string or is a number

I want to validate if a received param is either an empty string or a positive number, i tried with:

validates :my_flield, presence: true, if: -> {((my_flield.is_a? String) && my_flield.empty?) || ((my_flield.is_a? Numeric) && my_flield>0) }

but it doesn't work. I can still either put non empty strings and negative values. How should i do it?

A ruby validation that works

Aucun commentaire:

Enregistrer un commentaire