Welcome Guest, Not a member yet? Register   Sign In
SofDelete and validation is_unique
#1

I have validation in my project: is_unique [parameters.variable, id, {id}]

PHP Code:
<?php

namespace App\Models;

use 
CodeIgniter\Model;

class 
Parametros extends Model
{
    protected $table      'parametros';
    protected $primaryKey 'id';

    protected $returnType 'object';
    protected $useSoftDeletes true;

    protected $allowedFields = ['descricao''variavel''tipo''valor'];

    protected $useTimestamps true;
    protected $createdField  'created_at';
    protected $updatedField  'updated_at';
    protected $deletedField  'deleted_at';

    protected $validationRules    = [
        'descricao' => "required",
        'variavel' => "required|is_unique[parametros.variavel,id,{id}]",
        'tipo' => "required"
    ];
    protected $validationMessages = [];
    protected $skipValidation     false;



I use sof delete, when performing the registration it is validating considering the deleted records.

How can I ignore deleted items (deleted_at == null) in the is_unique validation in my project?


Thanks Smile
Reply
#2

In the current CI4 implementation all Validations include soft deleted records. Means: they are NOT ignored, but taken into consideration during evaluation of given validation criteria. So you have 4 options:
1) either permanently delete all soft deleted records before applying built-in is_unique validator, or
2) write your custom validator that will ignore soft deleted records when checking for uniqueness,
3) raise a feature request to have a setting whether soft deleted records should be included or excluded during validations (obviously, is_unique and is_not_unique should behave the same), and then wait till it gets approved, implemented and including into upcomming release, or
4) extend built-in validator and propose your change to be included into next version of CI4 Smile
Reply
#3

(02-24-2020, 03:12 PM)zahhar Wrote: In the current CI4 implementation all Validations include soft deleted records. Means: they are NOT ignored, but taken into consideration during evaluation of given validation criteria. So you have 4 options:
1) either permanently delete all soft deleted records before applying built-in is_unique validator, or
2) write your custom validator that will ignore soft deleted records when checking for uniqueness,
3) raise a feature request to have a setting whether soft deleted records should be included or excluded during validations (obviously, is_unique and is_not_unique should behave the same), and then wait till it gets approved, implemented and including into upcomming release, or
4) extend built-in validator and propose your change to be included into next version of CI4 Smile
Hello zahhar, thank you very much.

I made the following change to the is_unique validation to solve my problem:

PHP Code:
public function is_unique(string $str nullstring $field, array $data): bool
    
{
        
// Grab any data for exclusion of a single row.
        
list($field$ignoreField$ignoreValue$deletedField) = array_pad(explode(','$field), 4null);

        
// Break the table and field apart
        
sscanf($field'%[^.].%[^.]'$table$field);

        
$db Database::connect($data['DBGroup'] ?? null);

        
$row $db->table($table)
                
  ->select('1')
                
  ->where($field$str)
                
  ->limit(1);

        if (! empty(
$ignoreField) && ! empty($ignoreValue))
        {
            
$row $row->where("{$ignoreField} !="$ignoreValue);
        }

        if(! empty(
$deletedField)) {
            
$row $row->where($deletedFieldnull);
        }

        return (bool) (
$row->get()
                        ->
getRow() === null);
    } 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB