2

I want to enable users to edit their account - and if they specify a new password update it.

To ensure the correct spelled password, I used the identical validator with a second field. This works while creating a new user, but while editing I faced the following problem:

The password field can be empty and therefore is not required. Since the password-repeat field can be empty as well (just because of you don't need to change the password or fill the password field), I am not required to retype the password in this field if there is any value in the password field.

If I submit an empty password field (not required) and a required password-repeat field, I get the error to please fill the password-repeat field.

How can I enforce the identical validator to be checked but accept empty values if there is no value in the token?

Stoyan Dimov
  • 5,250
  • 2
  • 28
  • 44
ChrJantz
  • 919
  • 1
  • 11
  • 23

1 Answers1

0

Recently I had the same issue. Here is what I did. For the ADD user form I added the password and retype-password filed with a "required" options. For the EDIT form I did two things:

1) Before rendering the form, I made sure that the password (and the retype-password) fields are empty. When you pull the user to populate the for unset the password field.

2) When submitting the EDIT form before you check the form's isValid change the require option of the password(s) filed to false. After that again before isValid, check if the password field from the post is empty. If it is empty unset the password fields from the array before you pass it to the form for validation.

The result will be that when you submit content in the password fields it will check and validate if identical. If the password value is empty, the previous password will be preserved as the user update query will not touch the password field.

You can add a small note to the password fields like: "Keep empty to preserve the old password".

Hope this helps :)

Stoyan

Stoyan Dimov
  • 5,250
  • 2
  • 28
  • 44
  • This approach goes against the re-usability of the input filter though. This logic should not be manipulated at runtime from outside the input filter itself. – Ocramius Mar 20 '13 at 08:36
  • I agree with that :). Would you support two different form factory objects or closures that inject two different input filter one for add user and another of edit user. And instead of injecting the form in the controller, pull it from the Service Locator. If not what would your approach be? – Stoyan Dimov Mar 20 '13 at 08:39
  • check the issue that I marked as possible dupe of this one: that's the correct approach to this one in my opinion :) – Ocramius Mar 20 '13 at 08:41
  • I agree with your other post but I also think that this case here is different. He wants to have the required for the ADD-ing user but wants to remove it for the EDIT-ing user. Therefore, to leave the input filter untouched (as you proposed) he need two input filters for the adding and editing :) – Stoyan Dimov Mar 20 '13 at 08:49
  • that could be an input filter that uses a provided or NOT provided user ID instead. It's still the chicken-egg game of having one field depending on another though ;) – Ocramius Mar 20 '13 at 08:52
  • I haven't thought about this approach. I like it, because its all logic contained in the input filter itself. Thanks :) – Stoyan Dimov Mar 20 '13 at 08:54