Answer the question
In order to leave comments, you need to log in
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;
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
/**
* {@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
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 questionAsk a Question
731 491 924 answers to any question