Welcome Guest, Not a member yet? Register   Sign In
Form validation is_unique: valid if value is in current row, invalid on any other row
#1

(This post was last modified: 05-09-2021, 05:32 AM by BFlokstra. Edit Reason: Solved it myself )

I have a form where I edit customer information. There are two fields that have to be unique:
- organisatie_naam,
- contactpersoon_email

When inserting the customer the is_unique validation works perfectly. If the organisatie_naam or contactpersoon_email already exists in the table I get the appropriate validation error. Now I want to do the same when updating the customer. As you can imagine, the customers name (is a company name) doesn't change that often while the postal address of contact person can change on occasion. 
I use the same form to update all customer information at the same time. 

Is there a way to use the is_unique validation to check if the field organisatie_naam is unique while ignoring the row it is in? 

What am I experiencing now?
Right now when I do not change the customer name but do change (for example) the postal address I get a validation error: "The organisatie_naam field must contain a unique value."
This is understandable. That name is already in the database. But how do I make the validation ignore the row this specific customer is in, to that the validation checks if the customer name is nog present in any of the OTHER rows and therefore unique in this context.

As you can see I added the id to the is_unique validation hoping this would do what I want, but no luck.

PHP Code:
$rules = [
                'organisatie_naam' => 'required|alpha_numeric_punct|min_length[4]|is_unique[organisaties.organisatie_naam,organisaties.id]',
            ]; 

Edit:
Okay, I solved it myself. I hate it when I'm facing a problem, find a forum post with the exact problem I'm having and the OP says "I sloved it" without showing how he did it. So here is my solution:

It turns out that in the validation rule you have to not only pass the column name that where the rule has to look for the rown to be ignored. You also need to pass the exact value that needs to be ignored. So I changes this:
'organisatie_naam' => 'required|alpha_numeric_punct|min_length[4]|is_unique[organisaties.organisatie_naam,organisaties.id]',

to this:
'organisatie_naam' => 'required|alpha_numeric_punct|min_length[4]|is_unique[organisaties.organisatie_naam,organisaties.id,' . $id.']',
Reply
#2

Thank you for sharing your solution!  Cool
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

You're welcome! It's so irritating when people don't do that. It's like they thing "I had to look for days, let others do the same"
Reply
#4

But it is in the documentation.
is_unique

Сhecks if this field value exists in the database. Optionally set a
column and value to ignore, useful when updating records to ignore itself.

is_unique[table.field,ignore_field,ignore_value]
Reply
#5

I know it is in the documentation. You never completely read over something when being stuck on a problem for a while?
Reply
#6

Thanks for posting your solution BFlokstra! Are you validating in a config file or in the controller?

I've been trying to solve the same issue in the 'In Model' validation (i.e. setting the model property (array) $validationRules) like this:
Code:
protected $validationRules    = [
        'A_code' => 'required|alpha|exact_length[2]',
        'B_code' => 'required|is_natural|max_length[3]',
        'BB_code' => 'required|alpha|is_unique[bcca.BB_code,bcca.bcca_id,'.$id.']',
    ];

But I'm getting this error:
Code:
ErrorException #64
Constant expression contains invalid operations search →

APPPATH\Models\Administration\BccaModel.php at line 14

Anyone?
Reply
#7

https://www.php.net/manual/en/language.o...erties.php
" but this initialization must be a constant value."
Reply
#8

(This post was last modified: 01-25-2022, 01:55 AM by Zeff.)

(01-24-2022, 08:41 AM)iRedds Wrote: https://www.php.net/manual/en/language.o...erties.php
" but this initialization must be a constant value."
Thanks iRedds, I missed this one. I initialize $this->validationRules in my constructor now and the message has disappeared.
I followed the in model validation on https://codeigniter.com/user_guide/model...ating-data where the notation {id} is suggested (and apparently, this notation CAN be used in class property / so doesn't need to be set in the constructor).
But still, the is_unique() validation rule was not ignored during update for the given id...
My mistake: I forgot to add a hidden field in the form holding the id value  Blush
So: problem solved, thanks for the golden tip!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB