I have a web-app with an location text field that has an auto complete that pulls data from Google Maps to complete the location typed in. What I want to to is force the user to choose the specific spelling and format that the auto complete gives for the location that is being typed i.o.w. a validation. The user thus can't type anything random in the location field.
I have:
validates :location, :presence => true
,but that doesn't really help. Is there a custom validation? Or one I'm not aware of?
Thanks
I guess you need to use before_validation thing like the following
before_validation do
self.location = YourClassName.find_by_location_name(location)
end
It's just an idea to get that worked. It's not an exact answer you needed.
Related
I defined a custom EachValidator to see if an attribute has leading or trailing whitespace. I know you can add it to the model like so:
validates :name, whitespace: true
But in the controller I want to call just run just the whitespace validator for some form feedback.
I can run it like this:
Validators::WhitespaceValidator.new(attributes: :name).validate_each(obj, :name, obj.name)
Is there a shorter way to call the specific validator? Like you can do user.valid? but that runs all of the validations. I only want the whitespace validator on the name attribute.
Since you did not come here to be told that your idea is bad and people will hate it: here is an idea that you can play with: :on
https://guides.rubyonrails.org/active_record_validations.html#on
validates :name, whitespace: true, on: :preview
and then in the controller:
def something
#model.valid?(:preview)
end
If you want to run the validation also on createand update you can specify something like
on: [:create,:update,:preview]
(Thanks engineersmnky)
Also: I don't think that giving early feedback to users, before they actually save the record is a bad idea if properly implemented.
This feels like trying to solve a problem (leading/trailing whitespace) by creating a new problem (rejecting the object as invalid), and I agree with the comment about this being a brittle strategy. If the problem you want to solve here is whitespace, then solve that problem for the specific attributes where it matters before saving the object; check out
Rails before_validation strip whitespace best practices
Just a theory question,
I have already made a field in my form required, should I still use Active Record Validations presence option? (validates *, :presence => true)
Thanks
Yes, you must add Model Level(Back-end) validation, cause sometimes, Smart people or developer can remove front-end validation by "Inspect element", at that time you can not prevent him/her by living blank values in textbox.
In this type of scenario only Back-end validation prevent users from filling blank data.
validates :column_name, presence: true
Use validates_url can validate url format.
Set validation in model:
validates :homepage, uniqueness: true, url: true
Can't validate these two cases as unique url:
https://stackoverflow.com
https://stackoverflow.com/
But they should be the same. If the first one is inserted into database, the second one should be validated and not allow to be inserted.
I didn't find a way to realize it using validates_url. Is it possible?
So the two validations, uniqueness and URL, happen separately, and there is nothing in the uniqueness check to handle the fact that those two URLs are essentially the same - instead, the string values are technically different, and thus it doesn't trip the uniqueness validation.
What you could do is look to tidy up your URL data before validation, with a before_validation callback in your model:
before_validation :process_url
def process_url
self.homepage = self.homepage.slice(0, self.homepage.length - 1) if self.homepage.present? && self.homepage.ends_with?("/")
end
This is called before the validations kick in, and will make sure that if the homepage attribute is present (even if you add a presence validation later if it becomes non-optional, remember this is running before validations), then any trailing / is removed.
Those two URL strings will then be the same after tidying up, and thus the second time around the validation will kick in and stop it from being saved.
Hope that helps!
I do use rubular.com to validate using regex. You can try this ^[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\.*)?$
I've followed the rails tutorial and have a working users system, but one thing annoys me: When saving a user, even just in the console, I have to turn off validations since it will not save without both password and password confirmation saved, and set to the same thing.
This is a pain, and seems potentially dangerous if I added other important validations to the user. So is there a good way of dealing with this? Really I want the validation to apply only when the update is coming from the user, not from my own code.
So the user model has this:
validates :password, length: { minimum: 6 }
has_secure_password # this does the hashing, comparisions, etc for us.
validates :password_confirmation, presence: true
This works great when the user is created, and for when the user is editing his/her profile since it requires that the password is entered again. But, for other edits, it's annoying. eg:
user = User.find_by_name("bob")
user.reputation = 200
user.save(validate: false) # I really don't want to have to care about password validations here.
You can call
#user.save(:validate => false)
You could try just validating on create. That will make it so the password (or other variables) is only necessary on registration but not when youre editing an existing user.
If you want to have control over which attributes to skip validations for, you may want to try using the ValidationSkipper gem:
https://github.com/npearson72/validation_skipper
Why don't you just create different models for it, a secure model for logging & password changes, and a profile model for anything else, giving out reputations, medals, and stuff.
Creating new functions in the first model allows you to totally forget about the second one, like
def reputation value
self.profile.reputation = value
self.profile.save
end
A little late to the party but if you want to update a single attribute you can use the update_attribute method which skips validation checks. In this case you could call
user.update_attribute :reputation, 200
I have a model called Contacts.
Contacts can have different status "bad, positive, wrong..."
These status may need to be changed over time, but across all contacts, they are the same options.
Should I model it this way:
Contacts.rb
belongs_to :status_contact
StatusContacts.rb
has_many :contacts
Then I manually populate the types of status in the table?
I then want to use Ajax to click a button corresponding to a value to update the value for Contacts.
It looks like you're trying to ensure that the values for your status are always going to restricted to a set of possible answers of your choosing. If that's all you're trying to do, there's no special need for a separate table. You can use the magic of ActiveRecord validations instead here.
First, create a string database column for Contact called :status.
Then you can use a validation to ensure that the values are limited to the ones you want. In Rails 3, you can do it like this:
validate :status, :inclusion => { :in => %w( bad positive wrong ) }
(If you're using Rails 2, use #validates_inclusion_of instead.)
In ActiveRecord, validations check that the object's values are valid before saving; it refuses to persist the object into the database until all validations pass.
Your naming strikes me as a little weird—ContactStatus sounds a little nicer to me—but I see this as being the general idea to achieve what you want.
No clear answer yet --- I think I need to use the table because it would allow the users to add and modify the types of status used across their application.