Adding one error message to two checkboxes in Rails - ruby-on-rails

I have two checkboxes that I'm adding a simple custom validator on. I want to check that either one or the other (or both) are checked, but that they aren't both empty.
To do this, I've written put a simple unless checkbox1 || checkbox2 line in my custom validator method. However, when outputting the error message, I realized that the error.add takes the form field as first parameter. I don't want the error message to be specifically for one of the other checkbox; I just want it to say, one or both need to be selected.
How can I do this?

Just found the answer:
errors.add(:base, 'Generic error message')

Related

Check in view that form have errors?

(ZF2) Is there a possibility to check in view that the form is displayed for the first time, or "returned" because of errors?
Of course I can in controller base on isValid add additional variable, but I'm curious if there is a ready solution to this?
You can check if the form has been validated with $form->hasValidated() if it hasn't you prob can be pretty sure it's displayed for "the first time".
Additionally you could check if the form contains any error messages $form->getMessages(). That way you could tell if it is validated and contains validation errors.

How to add multiple message validation in only one growl message

I have a p:growl for show message validation, but it shows a growl for each message validation, is there any way to show multiples messages validation in only one message p:growl?
It's just how the growl works. Each message will appear in its own panel. I don't think you can change it unless you develop a custom growl component on your own and put <p:messages> in it :)
As Mr.J4mes has mentioned, it's not a 'add on' function of Growl, you could however get PrimeFaces source and recompile it with your changes/requirements.
GrowlRenderer, puts all of the messages into a JSON object e.g..
{summary:"Successful",detail:"Hello ss",severity:'info'},{summary:"Second Message",detail:"Additional Info Here...",severity:'info'}]});});
Then the grow.js line 32 does a for each loop of 'each mesage', and does a render... using renderMessage on line 50. (may may not change depends fversion you're using)...
basically, you could do several things to fix it, (ie pass in all objects not for each and do it in the renderMessage, or keep as is but just don't end the tags until all messages are finished)..
This is less work than creating your own widget etc, adding a var to the primefaces widget, and conditionally doing this is probably a better option - otherwise you're totlally overwriting the functionality.

Is there any way to keep validate field optional in rails?

I have User model which includes 7 fields. for all these fields validation is written.i have two form where i am displaying fields depend on condition. in one form i have name password and city and other form i have role,phone and name.
When i try to submit the first form i got the error which says phone and role field are required resulting into failure of form.
Is there any way by which i can submit both form without getting the validation errors ??
Note : i want my logic to be in model only.. Please help me with this problem.
You could use a conditional validation to achieve what you want:
See here: http://guides.rubyonrails.org/active_record_validations.html#conditional-validation
However, this can quickly get hard to manage. Depending on the condition you're switching on, it'd probably be a cleaner design to use a 'Form Object' which will give you more control and let you do validations without the messy conditional logic.
See section #3 of this blog post for more detail:
http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
Using this pattern, you would check for your condition in the controller then determine which form object to send to the view.

What happens inbetween clicking the button Submit and the method create in Rails?

I ask this because I have a form with a radio button set to nil :
= f.radio_button :estimate_type, nil
I have debugger right at the beginning of my method call :
def create
debugger
When I hit the debugger, I check out my params, and they say the value is on not nil.
Enter Insanity wolf. Somehow this is getting converted on click. And I've scoured the entire app looking for possibly a leaky javascript file, or anything closely resembling the word 'on'. I've checked all my bases. Defaults in schema.rb, jquery click events, model validations, you name it. Nothing with the word "on" anywhere.
So the real question is, is there a way I can throw a debugger in a place in which if I were to click submit, the debugger would appear before the model validation, and then hopefully where the params are still what they are in the form. And then I can follow it down the trail and see where it goes wrong.
It doesn't have anything to do with your JavaScript. This is something that I've experienced before as well, but I'm not sure why it converts nil to 'on'. I do know that passing in :nil as a symbol returns a null string, as well as just simply passing in false.
A better approach to trying to solve your problem may be to put the debugger in the validation callback itself.
Nothing to do with rails - you could verify this by using your browser's network inspector to see that the browser is actually sending the parameter value "on".
By trying to set the value to nil (which doesn't really make sense - parameter values are always strings) you're suppressing the value attribute entirely from the generated HTML.
The standard says that in this case the default value for the input shall be "on" and so that is what your browser submits.

How do i filter and validate form fields in symfony 1.4?

Im trying to integrate a content filtering API. My plan was to use pre/post validators but I've lost may way somehow.
What i need to do is send the values to the content filtering service. If the response comes back that the content has been filtered it will also return a modified value for the field (basic profanity filtering... matches are replace with asterisks). Thats all well and good i can throw validation errors no problem - simple stuff.
However i dont want just throw errors. What needs to happen is that validation errors are thrown as normal, but the values are modified in the form for re-display.
Basically if someone posts something naughty i want them to get a validation error saying their post has been modified, they can re-submit the now "clean" post, or they can go about editing it to make it clean without the word replacements.
But do clean on a validator either throws an error OR returns cleaned values, not both. How can i go about implementing both? This will be used on many different forms with many different field names, so modifying methods on the form or a form base class isnt really an option - it needs to happen in the validation sub-framework somehow.
You can adjust this plugin for your needs http://www.symfony-project.org/plugins/WebPurifyPlugin

Resources