I'm relatively new to RoR so pardon if this is a really stupid
question. I have a text field where people will be entering a
username. I need to figure out a way to either
1. validate the field so that it will not be accepted unless they've
entered all uppercase letters for their username -- less ideal
2. regardless of what they enter in the field (uppercase or lowercase)
I need to convert the username value to uppercase before it's stored
in the database.
Here is my form:
<% form_for(@post) do |f| %>
<%= f.error_messages %>
Or change the setter (rather than messing around with filters
def username=(value)
write_attribute(:username, value.upcase)
end
Is the setter called if you assign to the attributes hash (e.g. with new or
create)?
I guess it must be, but I'd generally put that in a callback to be sure
Cheers,
ooo.... good question - don't know without testing it...
But more to the point, I don't like changing users' input before
storing it in the DB. We've shown how you *can* do it, and you've said
you *need* to, but I'd wonder whether that's really a *want* to...
either way... it works either way.
Hey, you don't happen to know of a simple way of implementing a date
picker for a field in a form do you? I was hoping to accomplish this
without needing to track down a plugin for the task.
On a related note: how can I set the default value of a field to be
today's date (just the date, no time value)
Hey, you don't happen to know of a simple way of implementing a date
picker for a field in a form do you? I was hoping to accomplish this
without needing to track down a plugin for the task.
I've used the "calendar_date_select" plugin in the past - it is a
plugin, but it's also very simple.
On a related note: how can I set the default value of a field to be
today's date (just the date, no time value)
Again in the model (one of several ways to address the need) :
def after_initialize
my_date_field = Date.today
end
As an aside, using "date" as a name for a db field might cause you
confusion down the line - it can be a reserved word (depending on the
db). I'd try to give the some more name context: "registration_date"
or whatever.
...hopefully I won't be pressing my luck, but one last question (for
now). So I have a bunch of form fields that I need to add up their
values, and make sure their sum does not except the value entered in a
new form field. If the sum does exceed the form field value then I
want to alert the user and prevent it from saving. Basically I want to
validate that :atWorkHours.value > ( :hoursMeetings.value
+ :hoursTraining.value + :hoursProjTrav.value )
I tried to do something similar to this in my model...but I assume I
just had the syntax completely wrong.
Assuming all of the "hours" attributes all return numbers (something,
somewhere is ensuring that, I hope -- you can add some
validations to your model. Again, there's a couple of ways of doing
this - I normally separate the method that adds the error to base from
the code that checks the condition (so I can check the condition
myself if I want), but you could easily merge them together if you
prefer:
validate :validate_work_hours_not_exceeded
def work_hours_exceeded?
# this is a little different to your equation, as yours would
raise an error if both sides were exactly the same, and I'd guess
that's not the right result - but if not, adjust it to suit
[hours_at_meetings, hours_in_training, hours_travelling].sum >
hours_at_work
end
private
def validate_work_hours_not_exceeded
errors.add_to_base("Do not exceed working hours") if work_hours_exceeded?
end
I took the liberty of tweaking the variable names (although you were
showing them as symbols...) to make them a little more in line with
conventions (and, I think, more easily readable - YMMV
Not having any luck with Calendar Date Select so far ... got it
installed, but it looks like the documentation is out of date @
http://www.railslodge.com/plugins/46-calendar-date-select ...I can't
for the life of me get the pop-up or embedded calendar to
display ...let alone get either to display in a form to take a value.
Hey, you don't happen to know of a simple way of implementing a date
picker for a field in a form do you? I was hoping to accomplish this
without needing to track down a plugin for the task.
On a related note: how can I set the default value of a field to be
today's date (just the date, no time value)
Did you try date_select?
<%=f.date_select :name_of_field %>
It defaults to today date if the field is of type date
It is not too fancy, but it works...