ZF2 Validation - Field required if another element is not empty - zend-framework2

I've had experience with making custom validation in Zend Framework 2 (using version 2.0.5 at present). I'm interested in creating a change password section in an "edit profile" form. What I want to be able to do, is have 3 fields:
Current Password,
New Password,
Confirm New Password.
Then I want to validate as follows:
If a new password is set, current password must also be set (and authenticated), and confirm new password should match new password.
If a current password is set, the new password and confirm new password must also be required.
If none are set, allow the edit of the rest of the profile, so continue validation.
I think you can get the gist of what I'm after, I'm looking for a reusable way to do this using Zend Framework 2. Ideally, creating a custom validator so that the forms can be reused, thought I suspect a factory approach may be better. Anything so I don't have to check it in the controller/service layer and repeat myself wherever I want to use this.
Kind Regards,
ise

You could add a custom filter, but I think this could also be done using validation groups and separately validate groups of inputs.
you could add the password fields to a separate group and only validate that group if the main password is not empty.
http://framework.zend.com/manual/2.0/en/modules/zend.input-filter.intro.html
http://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html#validation-groups-for-fieldsets-and-collection

Related

What is difference between First Name used for Forms and First Name used for Email Only in Oracle Eloqua

eloqua page- email field merge
What is difference between First Name used for Forms and First Name used for Email Only in Oracle Eloqua??
These are default field merges provided by Eloqua. The "for Forms" has no default value if blank, so if the user matches an existing cookie but does not currently have a First Name on their corresponding contact record, nothing will prepopulate on the form. You wouldn't want a default value to potentially be set to their contact record if they submit the form without changing the value. Conversely, the "for Email Only" has a default value set, since in most emails you might not have a first name, but you rather have "Hi Loyal ACME Customer," than "Hi ,".

MVC server side validation conflicts with client side validation

With ASP.NET MVC 5, how can one mix server side business rules and logic with client side basic validation? Specifically, using #Html.ValidationMessageFor()?
Here's a common scenario; imagine a login form with username + password. The username has to be an email address, and is of course required. So you have a model with Data Annotations of [Required] and [EmailAddress] on the username field, but when you submit your form to the controller, you check to see if that email address is a valid user/email address registered at your site. If it is not, you might do something like:
ModelState.AddModelError( "Username", "This username isn't registered at this site.")
Unfortunately, when that goes back to the view, client side unobtrusive validation sees that your field has a valid email address in it, so it removes your custom server side supplied error at page load! (Evidently MS' implementation of jquery validate does an automatic validation on page load.)
This seems to create a conflict with a very common scenario of additional server side validation rules... and due to client design requirements, we can't simply put the message into a ValidationSummary -- so please, don't suggest that. We need the messages to appear in their place under the fields themselves, where the other normal 'required' and 'must be an email address' validation messages would appear.
Finally, this is just one example of a business logic validation rule; there are more -- custom validation data attributes are likewise not a valid option for everything, and neither is remove (ajax) validation!
Seems like a fairly significant oversight?

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.

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

Saving data in rails session to use in form on next request

Let's say I have a table called positions (as in job positions). On the position show page I display all the detail about the job - awesome. At the bottom I need the prospective applicant to input their professional license # before continuing onto the next page which is the actual applicant creation form. I also need to take that license # and have it populate that field on the applicant form (again on the proceeding page).
I realize there are a couple ways to do this. Possibly the more popular option would be to store that value in the session. I am curious how to do this in the simplest manner?
My idea:
Create a table specifically for license #'s.
Add a small form on the position show page to create license # (with validation)
Store newly created license in session - not sure what to put in which controller?
On applicant creation form populate from session the license #.
This would assume applicants only have one license.
Thoughts?
Appreciate the help!
Don't store this in the session! Pass that as an hidden field.
Let's say the user starts the form, then open the form again in a new window or something... then the session variable would be shared between the two forms. Other problems would occur if the cookie gets removed (session expire, user clear cache...)
This is not good. The best way is using a POST variable. GET works as well but messes up the URL
Seems like a good idea. As for #3, for whatever controller is called in the transition from 2 -> 4, that would be the controller where you store the session, as such:
session[:license_number] = your_license_number_information
From there, it can be called the same way (session[:license_number]) to get it.
The hidden field is safer for data persistence. However is not not then coded in the HTML output? That can be a great data security issue.
This is a trade-off to be considered.

Resources