Check in view that form have errors? - zend-framework2

(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.

Related

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.

Adding one error message to two checkboxes in 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')

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

Rails validation fails but object memory remains unchanged?

First time user long time reader. I have thoroughly looked for an explanation for the problem I'm having via the mighty search engine Google, but alas I have failed to produce any significant insight.
I need to be able to ensure that a model form is not reloaded with invalid data. Since the model stored in memory on the server is edited directly with the parameters of the web form first, and THEN checked for validity, without additional code invalid model data will ALWAYS be sent back to the form. This is less than desirable to me. My question is this: how do I ensure this doesn't happen?
What I'm thinking is I need some mechanism for saving the state of the object before it's modified with the parameters sent from the web form, and then after a failed validation restore the object to it's previous, correct and unmodified state of being.
Help!
Thanks,
Les
The object isn't actually modified in the db if validation fails, even though the object is in an invalid state in the form ... the thinking behind this is that the user wants to see the errors they made so they can correct them.
If you don't want that to be the case, then just read back the object with a WhateverObject.find(x) and assign it to the variable that the form is referencing and it will 'restore' the object to its previous unmodified state.
To add to what concept47 said you can also get the value for a particular field using
object.field_was
Have a look at ActiveRecord::Dirty for details (http://ar.rubyonrails.org/classes/ActiveRecord/Dirty.html)
Using that you could retrieve the original values for just those fields that had validation errors.

Resources