I am using conditional validations for a multi-step form, very much like this Railscast: http://railscasts.com/episodes/217-multistep-forms.
with_options :if => lambda { |o| o.current_step == "contact" } do |entry|
entry.validates :name, :presence => true, :length => {:maximum => 50}
end
This causes the automatic "required" field marking to not function. The fields all end up with an "optional" class. The validations still work as expected upon form submission. Is this a known issue? Something I am doing wrong?
This is intentional. At the moment you add conditional validation to an attribute, Simple Form requires you to explicitly pass required as executing the if/unless clauses on its own is very brittle.
Any condition (:if, :unless) added to the attribute will disable the required feature. SimpleForm won't evaluate the condition to see whether to add the required mark or not, this is better handled in the form by ourselves.
Excellent, thanks!
_executing the if/unless clauses on its own is very brittle_
Could you please elaborate on this? We have a local patch to simple_form that handles conditional validations for every type of block that can be legitimately passed to the ActiveRecord/ActiveModel validation. Anything that would break this functionality would also break the model validation itself, so by this definition, I consider our patch to be robust, and we are willing to take the time to turn it into a pull request. I feel that handling validations on the fly, but skipping conditional validations, leaves a fairly large gap in functionality as users will have to manually recreate the conditional validation logic for a given attribute in the view layer everywhere that such attributes are used with simple_form. This forces a violation of both DRY and basic encapsulation.
Furthermore, we plan to put the functionality behind a configurable feature switch defaulted to off so as to be backwards compatible.
@pivotal-casebook today users can put whatever they want in :if and :unless. It could for example call a method that generates side effects and doing so automatically while generating a form is an extremely surprising behaviour. That's why we require users to do it explicitly.
I feel that handling validations on the fly, but skipping conditional validations, leaves a fairly large gap
I think we are overestimating the size of the gap here. ;) In the simplest scenario, we have this in our model:
validates_presence_of :name, if: :some_condition?
Which means all you need in your view is:
f.input :name, required: @model.some_condition?
In the most complex cases... we go back exactly to the point we were talking about: why do you have complex validations in your if/unless clauses? It is time to clean it up (and believe me, people do have crazy stuff in it).
TL;DR: we want to avoid executing model code when generating the form. In any case, thanks a lot for the interest in improving Simple Form!
f.input :name, required: true doesn't seem to work, the form is submitted without any validation. Has this recently changed or can you point us to another resource explaining best practice?
Edit: perhaps I'm mixing up form building/presentation with validation. I need to look at client_side_validations.
@rosskevin I believe you mean browser validations. Do you have it enabled in simple_form config file?
SimpleForm.browser_validations = true
SimpleForm.browser_validations is under initializers > simple_form > config.browser_validations.
for me I had to reset rails server after seting it to true.
Most helpful comment
@pivotal-casebook today users can put whatever they want in
:ifand:unless. It could for example call a method that generates side effects and doing so automatically while generating a form is an extremely surprising behaviour. That's why we require users to do it explicitly.I think we are overestimating the size of the gap here. ;) In the simplest scenario, we have this in our model:
Which means all you need in your view is:
In the most complex cases... we go back exactly to the point we were talking about: why do you have complex validations in your if/unless clauses? It is time to clean it up (and believe me, people do have crazy stuff in it).
TL;DR: we want to avoid executing model code when generating the form. In any case, thanks a lot for the interest in improving Simple Form!