I have a form which has a select box with categories. If a particular category is selected, another select box/dropdown is displayed to the user. I need this second dropdown to be a required field when this category is selected.
I cannot add validates :course, :presence => {:message => 'Course cannot be blank.'} to the model because this field is not always required, I need some other way to make it required only when certain category is selected in the first dropbox.
Thanks for your help
You could try using the if argument to validates, like this:
validates :course, :presence => {:message => "Course cannot be blink."}, :if => Proc.new { |u| u.first_dropdown_value == 'value_that_you_validate_courses_for' }
For more information, check out the Rails documentation on conditional validation.
Related
I have some trouble here trying to include an include_blank option for 'option_groups_from_collection_for_select'
I would like to have include_blank option but have the currently selected value displayed instead of a blank selection on update action. I have tried this here but it still shows blank.
select_tag(:candidate_source, option_groups_from_collection_for_select(grouped_candidate_sources, :second, :first, :id, :source), { :include_blank => true, :selected => :source })
The last argument in the option_groups_from_collection_for_select is the selected option, which should map to your value.
Since your option_key_method (4th arg) is :id you're allowed to pass the form object directly as an argument. Example:
select_tag(:candidate_source, option_groups_from_collection_for_select(grouped_candidate_sources, :second, :first, :id, :source, f.object.source), { :include_blank => true }
Anyway, see here for more argument information: http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/option_groups_from_collection_for_select
In update user form, I want to split password from rest of the form.
In user model I validate it.
I want to have
form 1
User infos fields
form 2
password and confirm fields.
in php I would do something like if post password is empty, ignore it.
is there something like
if :password
do validation
end
I need the model part, I have the rest.
just need an idea how to split validation in model or how to achieve this.
I hope i did understand you
class User << AR
validates :password,
:lenght => {:between => 3..20},
:confirmation => true,
:if => Proc.new { self.password }
end
I have a form with a date select.
I have the following form:
<%= date_select :user, :birthday, {:start_year => Time.now.year, :end_year => 1910,:order => [:day,:month,:year], :prompt => { :day => 'day', :month => 'month', :year => 'year' }}, { :class => "default" } %>
Validation in my model:
validates :birthday, :if => :should_validate_birthday?,
:presence => {:message => "Please enter your friend's birthdate"},
:date => { :after => Date.civil(1910,1,1), :before => Date.today, :message => "Please enter a valid date"}
Here is an example of what the user submits in the log:
"user"=>{"name"=>"rewrwe", "birthday(3i)"=>"1", "birthday(2i)"=>"", "birthday(1i)"=>"2008", "email"=>""}}
NOTE that the value for the month is blank.
IN the controller I create the user
create
#user = User.new(params[:user])
If #user.save
#go to the confirm page
end
end
No error messages are shown even though the month is missing because for some reason when I try to save the model it converts an empty month to "January" or 01.
This is very frustrating as I don't want users to submit bad data by accident.
How can I stop Rails from doing this and make sure all the date information is submitted?
Separate the three fields with attr_accessor:
class User < ActiveRecord::Base
attr_accessor :birthday_year, :birthday_month, :birthday_date
validates_presence_of :birthday_year, :birthday_month, :birthday_date
# ...
end
And then in your view just have different selects for each of them. That's all the date_select does but it splits them up in the view, rather than in the model, which makes it hard to validate each field.
This seems like a simple question but I can't seem to find an answer short of writing custom validators. I have this validator
validates :password, :presence => true, :confirmation => true, :length => { :minimum => 5}
there are more rules applied such as some regex for complexity, but this gives the gist.
The issue is that I only want presence applied on create, everything else needs to be on create and update. Because the user may not be changing a password when updating their information.
I tried splitting the rules
validates :password, :presence => true, :on => :create
validates :password, # The rest of the rules
This resulted in all rules being ignored for update. Is there a simple way to apply only one rule to create and the rest to everything?
You can try keeping it in one line, but applying :on => :create to just the :presence check:
validates :password, :presence => {:on => :create}, :confirmation => true, :length => { :minimum => 5}
However, I'm not sure it makes sense to always require a minimum length, but not always require presence -- if you update an existing record with a blank password, it's going to fail validations anyway since the length is 0.
My hunch is that the problem is that the validate :password call is not additive. Can you switch the presence check to:
validates_presence_of :password, :on=>:create
And then keep your other validations using the validate. Does that work?
I have a couple of simple models that are associated like so:
MODELS
class Task < ActiveRecord::Base
belongs_to :user
validates :name, :presence => true, :message => 'Name cannot be blank, Task not saved'
end
class User < ActiveRecord::Base
has_many :tasks
end
VIEW has a call in it like so:
user.tasks <-- then I loop through the tasks
The Issue:
In the task model --
when I use:
validates :name, :presence => true , :message => 'Name cannot be blank, Task not saved'
I get a 500 error:
ActionView::Template::Error (uninitialized constant User::Task):
NameError in View file
when I use:
validates_presence_of :name
Everything works.
I thought the both validates methods above where the same...is the issue have to do with associations and how validation tie into associated models. I have a hunch that something is going on with the way things are associated, but it is just a hunch.
Any help will be appreciated. Thank very much.
When you use the newer validates :name format, you can put multiple validations in one line rather than having to have multiple lines for each type of validation. Because of this, when Rails hits your :message parameter, it thinks it's a validation method rather than a message associated with :presence. Try this instead:
validates :name, :presence => {:message => 'Name cannot be blank, Task not saved'}
Also, depending on how you display your errors, this error may actually show up as 'Name Name cannot be....'; if so, you'll want to set the message to just 'cannot be blank, Task not saved'.