Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional validations break "required" field marking #322

Closed
mattfordham opened this issue Sep 12, 2011 · 8 comments
Closed

Conditional validations break "required" field marking #322

mattfordham opened this issue Sep 12, 2011 · 8 comments

Comments

@mattfordham
Copy link

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?

@josevalim
Copy link
Contributor

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.

@carlosantoniodasilva
Copy link
Member

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.

@mattfordham
Copy link
Author

Excellent, thanks!

@pivotal-casebook
Copy link

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.

@josevalim
Copy link
Contributor

@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!

@rosskevin
Copy link

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.

@freemanoid
Copy link

@rosskevin I believe you mean browser validations. Do you have it enabled in simple_form config file?

SimpleForm.browser_validations = true

@justanotherkevin
Copy link

justanotherkevin commented Apr 19, 2017

SimpleForm.browser_validations is under initializers > simple_form > config.browser_validations.
for me I had to reset rails server after seting it to true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

7 participants