Welcome Guest, Not a member yet? Register   Sign In
Multiple ignore-fields for is_unique-function
#1

I'm having some issues with the Codeigniter 4 validation rules. I'm using the is_unique function within the ruleset in Order to except one single row from the validation.
The problem here is that there are two fields in the table that need to be checked:

Code:
'episodeTitle' => [
                    'label' => 'episodeTitle',
                    'rules' => 'required|max_length[100]|is_unique[episodes.episodeTitle,episodes.episodeID,' . $episodeID . ',episodes.podcastID,' . $podcastID . ']',
                    'errors' => [
                        'required' => lang('Errors.nested.episode.episodeTitleRequired'),
                        'max_length' => lang('Errors.nested.messages.maxLength100'),
                        'is_unique' => lang('Errors.nested.episode.episodeTitleUnique'),
                    ],
                ],

Is it possible, to make the exception depending on 2 or more fields?
I want to check the episodeID AND the podcastID not only one of these values.
Reply
#2

(This post was last modified: 10-28-2020, 06:57 PM by mlurie.)

I'm not sure I am interpreting your question correctly, but in general, if you need two fields to be unique, you will need to create a separate validation rule for each field.

If you are trying to check if the value of the field is unique in two different columns in the same row (I'm not sure why you would ever want to do this) try using the is_unique rule twice, once for each column.  I've never done it before, but it might work.


PHP Code:
'rules' => 'required|max_length[100]|is_unique[table.column1,table.columnID,{columnID}|is_unique[table.column2,table.columnID,{columnID}]'

Please note the correct syntax for the ignore value placeholder described in the documentation: https://codeigniter.com/user_guide/libra...aceholders.

If that doesn't work, you will need to create your own custom validation rule. Check out the documentation to get started: https://codeigniter.com/user_guide/libra...stom-rules.
Reply
#3

Hey, thanks for your answer. Unfortunately, that does not work.

You're interpreting correctly. My issue is the following:

I have a table for podcast Episodes. The Episodes are referring to a podcastID, so it can be possible that there are two episodes for two different podcasts but they have the same title.

So when i use is_uniqe with episodeID as the only exception, another episodeID with the same title could be found but with another podcastID. That's why i need two exceptions.
Reply
#4

Every rules list for check one input text field
- So, if you want to check multi than use your custom code or query

Learning CI4 from my works, from errors and how to fix bugs in the community

Love CI & Thanks CI Teams

Reply
#5

I was able to fix the issue. I made custom function for the validation to check 2 database fields:

   
Code:
    public function is_unique_multiple(string $str = null, string $field, array $data): bool
    {
        // Grab any data for exclusion of a single row.
        list($field, $ignoreField, $ignoreValue, $ignoreField2, $ignoreValue2) = array_pad(explode(',', $field), 5, null);

        // 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($ignoreField2) && ! empty($ignoreValue2))
        {
            $row = $row->where("{$ignoreField2} =", $ignoreValue2);
        }

        return (bool) ($row->get()
                        ->getRow() === null);
    }
  
This ensures that entries with other podcastsIDs will not interfere with the validation Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB