XF 2.2 JSON_ARRAY column and allowedValues

Scandal

Well-known member
Hello all!
Let's say we have a JSON_ARRAY column (entity) and we need to specify allowedValues.

How could be achieved?
Example:
PHP:
        $structure->columns = [
            'user_id'   => ['type' => self::UINT, 'required' => true],
            'days' => ['type' => self::JSON_ARRAY, 'default' => [],
                'allowedValues' => ['', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']]
        ];
The above example of allowedValues will work with STR type, but not for JSON_ARRAY.
Any idea?
 
You could use a verifier method. Something like (untested)...

PHP:
protected function verifyDays(array $values, string $key, int $type, array $columnOptions): bool 
{
    if (!empty(array_diff($values, $columnOptions['allowedValues'])
    {
        $this->error(\XF::phrase('some_phrase'), $key);
        return false;
    }

    return true;
}

It will be automatically called if it exists (based on the column name), or you can set a custom method name using the verify column option.
 
Back
Top Bottom