CodeIgniter Forums
Validation rule for UNIQUE and NULL fields - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Validation rule for UNIQUE and NULL fields (/showthread.php?tid=87446)



Validation rule for UNIQUE and NULL fields - joho - 04-20-2023

Is there some validation rule that will check for unique non-null values?

MySQL allows an index to be UNIQUE, but still allow multiple such columns to be NULL. I.e. it only applies the UNIQUE constraint if the column has a non-NULL value.

Does the same exist in CI validation rules?


RE: Validation rule for UNIQUE and NULL fields - Mni.day - 04-20-2023

PHP Code:
if (!is_null($data['foo'])){
    $validation->setRules($foo_uniq_rule);




RE: Validation rule for UNIQUE and NULL fields - joho - 04-20-2023

Sorry, not sure I follow?


RE: Validation rule for UNIQUE and NULL fields - InsiteFX - 04-20-2023

You have these 2 Rules by default,

PHP Code:
is_not_unique
is_unique 

If that does not work for you than you will need to create your own Custom Rule.


RE: Validation rule for UNIQUE and NULL fields - joho - 04-21-2023

Fair enough. But shouldn't is_unique handle this in the same way the database handles it? I guess that's my point.