• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CI4 Validation is_unique in properties Config/Validation

#1
Hi guys. I have a problem when editing a row having a unique field, I have defined a "users" property in Config \ Validation, when I perform the validation of the controller side, it does not recognize the "ignore_value".



PHP Code:
//--------------------------------------------------------------------
// Rules  Config\Validation
//--------------------------------------------------------------------


 
public $users = [
        'email'=>[
            'label'=>'email',
            'rules'=>'required',
            'errors'=>[]
        ],
        'password'=>[
            'label'=>'password',
            'rules'=>'required',
            'errors'=>[]
        ],
        'username'=>[
            'label'=>'username',
            'rules'=>'required|is_unique[admin_users.username,id,$id]',
            'errors'=>[]
        ],

    ]; 


PHP Code:
abstract class CoreController extends Controller implements Crud
{
    protected $dataResponse = [];
    protected $objForm '';
    protected $form;
    protected $request;
    protected $validationRules// here define name for rule 

PHP Code:
class Users extends CoreController
{
    protected $dataResponse=['breadcum'=>'Users'];

    protected $objForm UserForm::class;
    protected $model UserModel::class;
    protected $validationRules 'users'// set the name for users validation

    public function __construct()
    {

        parent::__construct();
        //Do your magic here
    

PHP Code:
public function new()
    {
        if($this->request->isAJAX() && $this->request->getMethod() === 'post')
        {
            echo $this->form($this->form);

        }else
        {
            if($this->request->getMethod() === 'post')
            {
                if($this->validationRules)
                {
                    if (!$this->validate($this->validationRules)) //set the rules
                    {
                        $this->dataResponse['form'] = $this->form($this->form,TRUE);
                        echo view('admin/base',$this->dataResponse);
                    }
                    else
                    {
                        $this->save($this->model);
                    }
                }else
                {
                    $this->save($this->model);
                }
            }else
            {
                $this->dataResponse['form'] = $this->form($this->form);
                echo view('admin/base',$this->dataResponse);
            }

        }
    



and does not validate for me, for the same id not work, 
Thank you.
Hernan Rodríguez
[email protected]
Reply

#2
I am having the same trouble with the CI4 is_unique validation rule. Although I am indicating an ignore_field and an ignore_value in my rule definition (no spaces per the CI4 documentation), the validation continues to fail when editing an existing record. The validation message returned is:

The Permission name field must contain a unique value.

This situation is precisely the documented reason for having the ignore_field and an ignore_value options. Here is my rules array:

        $rules = [
            'perm' => [
                'label' => 'Permission name',
                'rules' => 'trim|required|is_unique[permission.perm,id,$perm_id]'
            ],
        ];

Am I missing something obvious?
Reply

#3
This functionality is what appears to not be working:

https://codeigniter4.github.io/userguide...aceholders

So rather than use a placeholder {post_element} or php variable $var, I had to write the rule in my controller method as follows to get the is_unique rule to work:

DOES NOT WORK:

$rules = [
'perm' => 'trim|required|is_unique[permission.perm,id,$perm_id]'
];

DOES NOT WORK:

$rules = [
'perm' => 'trim|required|is_unique[permission.perm,id,{id}]'
];



WORKS:

$rules = [
'perm' => 'trim|required|is_unique[permission.perm,id,'.$perm_id.']'
];

I'm pretty sure this is a bug. But again, maybe I'm missing something.
Reply

#4
I've been struggling with exactly the same problem today. I have found no way to get the placeholder valu to work in the ruleset.

My solution has been to leave all the rules in the validation config file, then shortly before running the validation, I re-set the rule in my model by building the rule string with the actual value from the variable, by joining the strings as you have shown. This works.

I agree that it looks like a bug. I've left the placeholder in and if it gets fixed I can simply remove my extra ruleset code.
Reply

#5
I have also been struggling with this today.

Needed to validate a name field to make sure it is unique in the DB whenever creating or editing a record.

The solution Posted by never2ice works when needing to use dynamic data with is_unique.

(The is_unique validation function does appear to work with static information however.)
Reply

#6
Validation Placeholders where a unique feature for Models, but have been ported to Controllers as well. So {id} now works as intended.

Input:
Code:
'perm' => 'trim|required|is_unique[permission.perm,id,$perm_id]'

Output: ' don't evaluate the variables, the output remains the same.
Code:
'perm' => 'trim|required|is_unique[permission.perm,id,$perm_id]'

Input:
Code:
'perm' => 'trim|required|is_unique[permission.perm,id,'.$perm_id.']'

Output: You have opened the string and closed it again, it works.
Code:
'perm' => 'trim|required|is_unique[permission.perm,id,4]'

If you change ' into " it will evaluate the variable.

Input:
Code:
'perm' => "trim|required|is_unique[permission.perm,id,$perm_id]"

Output:
Code:
'perm' => "trim|required|is_unique[permission.perm,id,4]"
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.