CodeIgniter Forums
in_unique Validation Rule Help - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: in_unique Validation Rule Help (/showthread.php?tid=88901)



in_unique Validation Rule Help - purpleprawn - 11-29-2023

Hello! 
I am building a section for people to update their email addresses along with their name. I have a validation rule that checks that the email address entered is unique but I also need to ensure that the error doesn't happen if the email hasn't changed. I have came up with this rule based on the docs but it just throws the error.

PHP Code:
'email'=> 'required|valid_email|is_unique[users.email, users.email, '.$existingUserDetails['email'].']'

I tried using {email} instead of hard coding the value but throws an error: The placeholder field cannot use placeholder: email

Just trying to avoid having to roll my own rule for this as is_unique ticks all the boxes.


RE: in_unique Validation Rule Help - grimpirate - 11-29-2023

This example https://codeigniter4.github.io/userguide/libraries/validation.html#validation-placeholders shows exactly what you're attempting to achieve. The only possible difference I see is that your statement for is_unique should be is_unique[users.email,email,{email}]. Though you should probably be using the user id instead to ignore the user's present email (as per the example).

In a more general sense. The user is opting to change their email. It doesn't matter what their previous email is. Whether they update it to a new value or the same value is irrelevant. The only thing that requires validation is that they aren't trying to use someone else's registered email.


RE: in_unique Validation Rule Help - purpleprawn - 12-06-2023

(11-29-2023, 01:39 PM)grimpirate Wrote: This example https://codeigniter4.github.io/userguide/libraries/validation.html#validation-placeholders shows exactly what you're attempting to achieve. The only possible difference I see is that your statement for is_unique should be is_unique[users.email,email,{email}]. Though you should probably be using the user id instead to ignore the user's present email (as per the example).

In a more general sense. The user is opting to change their email. It doesn't matter what their previous email is. Whether they update it to a new value or the same value is irrelevant. The only thing that requires validation is that they aren't trying to use someone else's registered email.

Sorry for the delay in getting back to this! So the ID isn't part of the form so I can't use the example unless you can pass in the ID?