S
S
Sergey Shevchenko2017-07-08 00:07:08
Zend Framework
Sergey Shevchenko, 2017-07-08 00:07:08

ZF how to validate on a unique bunch of two fields?

Good people, please!
Here in my table I have a unique bunch of fields table1.`name` and table1.`thingId` (not two separate unique values, but one unique pair of one column with another)
In simple language, I can create such values
​​table.`name` = 'Option1' + table.`thingId` = 1;
table.`name` = 'Option2' + table.`thingId` = 1;
table.`name` = 'Option1' + table.`thingId` = 2;
but the line below should no longer be created, since there is already one above
table.`name` = 'Option1' + table.`thingId` = 1;
208c30f813bd4da581ac2925f0ecaae7.jpgb70edb80185242ad8805b91eb919f84e.jpg
How to make such a validator in zend? Climbing on the internet, I realized that exclude would help me, but I can’t apply it

Here is my validation (it needs to do this)
/**
     * {@inheritDoc}
     */
    protected function _validationRules()
    {
        return [
            'name' => [
                self::PRESENCE => self::PRESENCE_REQUIRED,
                NotEmpty::class,
                [
                    StringLength::class,
                    ['max' => 255],
                ],
                [
                    NoRecordExists::class,
                    [
                        'table' => OptionTableMap::getTableMap()->getName(),
                        'field' => OptionTableMap::getTableMap()->getColumn('name')->getName(),
                        'exclude' => [
                            $this->getData('thingId')
                        ],
                    ],
                ]
            ],

            'slug' => [
                self::PRESENCE => self::PRESENCE_REQUIRED,
                NotEmpty::class,
                [
                    StringLength::class,
                    ['max' => 255],
                ],
            ],

            'thingId' => [
                self::PRESENCE => self::PRESENCE_REQUIRED,
                NotEmpty::class,
                [
                    StringLength::class,
                    ['max' => 11],
                ],
                [
                    Regex::class,
                    ['pattern' => '/^[1-9]\d*$/'],
                ],
                [
                    RecordExists::class,
                    [
                        'table' => ThingTableMap::getTableMap()->getName(),
                        'field' => ThingTableMap::getTableMap()->getColumn('id')->getName(),
                    ]
                ],
            ],

            'select' => [
                [
                    Regex::class,
                    ['pattern' => '/^[01]*$/'],
                ],
            ],

            'accounting' => [
                [
                    Regex::class,
                    ['pattern' => '/^[01]*$/'],
                ],
            ],
        ];
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Shevchenko, 2017-08-01
@lancer_serega

Here is the solution to my problem

/**
     * {@inheritDoc}
     */
    protected function _validationRules()
    {
        $qntValidSelect = new Zend_Db_Select(Zend_Db_Table_Abstract::getDefaultAdapter());
        $qntValidSelect
            ->from(OptionTableMap::TABLE_NAME)
            ->where('Name = ?', $this->getData('name'))
            ->where('ThingId = ?', $this->getData('thingId'));

        return [
            'name' => [
                self::PRESENCE => self::PRESENCE_REQUIRED,
                'NotEmpty',
                [
                    'StringLength',
                    ['max' => 255],
                ],
            ],

            'slug' => [
                self::PRESENCE => self::PRESENCE_REQUIRED,
                'NotEmpty',
                [
                    'StringLength',
                    ['max' => 255],
                ],
            ],

            'thingId' => [
                self::PRESENCE => self::PRESENCE_REQUIRED,
                'NotEmpty',
                [
                    'StringLength',
                    ['max' => 11],
                ],
                [
                    'Regex',
                    ['pattern' => '/^[1-9]\d*$/'],
                ],
                [
                    'DB_RecordExists',
                    [
                        'table' => ThingTableMap::getTableMap()->getName(),
                        'field' => ThingTableMap::getTableMap()->getColumn('id')->getName(),
                    ]
                ],
            ],

            'select' => [
                [
                    'Regex',
                    ['pattern' => '/^[01]*$/'],
                ],
            ],

            'accounting' => [
                [
                    'Regex',
                    ['pattern' => '/^[01]*$/'],
                ],
            ],
        ];
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question