Welcome Guest, Not a member yet? Register   Sign In
custom validation rule fails
#1

(This post was last modified: 07-14-2023, 06:31 AM by sjender.)

Hi all,
I have this strange behaviour in my custom validation rule.
In my model I have this validation:
PHP Code:
    protected $validationRules = [
        'name'    => 'is_double_unique[categories,name,parent_id,{parent_id},id,{id}]',
    ]; 

You can see I use 2 variables: parent_id and id.
In my controller I run:
PHP Code:
$category $this->categoriesModel->find($categoryId);

$category->fill($this->request->getPost());

 if(!
$this->categoriesModel->save($category)){
   
$this->session->setFlashdata('errors'$this->categoriesModel->errors());
    
   return 
view('edit'$this->data);
 } 
I can see $category is filled when I print_r it:
Code:
[attributes:protected] => Array
        (
            [id] => 37
            [parent_id] => 33
            [name] => 1236
            [created_at] => 2023-07-10 13:22:07
            [updated_at] => 2023-07-14 13:15:52
        )

    [original:protected] => Array
        (
            [id] => 37
            [parent_id] => 33
            [name] => 123
            [created_at] => 2023-07-10 13:22:07
            [updated_at] => 2023-07-14 13:15:52
        )

    [_cast:CodeIgniter\Entity\Entity:private] => 1


But my validation rule doesn't recognize the parent_id...
PHP Code:
SELECT *
FROM `categories`
WHERE `name` = '1236'
AND `parent_id` = '{parent_id}'
AND `id` <> '37' 

As you can see it recognizes the {id}, but not the {parent_id}.
When I add $category->parent_id = 123; it works, but parent_id is already present in the $category entity, I don't want to explicitly set this value...
Reply
#2

(This post was last modified: 08-08-2023, 06:29 AM by sjender.)

I think I already know why this is failing....

Can this be the case because the validator only has access to the primary key and CHANGED values?
In this case the PARENT_ID is not changed, so it might not be available in my custom rule?

Is there a way to force an update on ALL fields, even when they're not changed?
Reply
#3

Where is the code for the custom validation rule?
Reply
#4

I have placed it in \Custom\CustomValidationRules.php
And added that class in The validation config as well
Reply
#5

Show/paste the code here.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB