is_unique in Form Validation on UPDATE |
We have a table that has a section called "slug".
Obviously slug needs to be unique and therefore when we insert we use "is_unique[article.slug]" which works perfectly fine. However the problem is that when we want to edit that slug we need it to again check if is_unique BUT not compare it with itself. Is there a right way around this, because at the moment when we submit the update and slug is not changed it considers it "used" and therefore passes an error. Thanks.
I do a similar thing in my app with username/email address checking.
I simply created a custom validation function and model function which checks if it's unique, but has a parameter "exclude_ids", which I use to pass the user's id through to exclude it from the results returned. I suggest doing the same, as the built-in "is_unique" function won't give you much control/flexibility like this.
for me, if i have to check differents values on UPDATE or INSERT, I create 2 validation rules.
// user_validation_insert => 'email' => 'valid_email|is_unique[user.email]' // user_validation_update => 'email' => 'valid_email' something like that (06-06-2016, 04:29 AM)edoramedia Wrote: We have a table that has a section called "slug". Why edit the slug? My opinion, just update the title without change the slug. Updating the slug cause "404 not found" when bot re-crawling your web page or some visitor had bookmark your page URL.
Keep calm.
(06-06-2016, 05:27 AM)keulu Wrote: for me, if i have to check differents values on UPDATE or INSERT, I create 2 validation rules. But what if you changed your email when editing? It still has to be unique, so the check has to be run, otherwise you could change it to an email which already exists.
If you want to do this, just run a search for the slug and count results in your own validation routine. Then you can exclude the current record easily. However you should not really be changing the page URL without the user agreeing to it, or even being in control of it. Why not add the slug to the form and put an instruction/warning that changing it will change the page url. Also, you can make sure the column is defined as unique and catch the error when updating.
Code: // The following rule has been "borrowed" from |
Welcome Guest, Not a member yet? Register Sign In |