我有这张桌子:
+-----+-------------+------------+--------------+---------+--------------+--------------------+-------------------+-------------+----------+------------+--------+---------+
| id | rule_number | profile_id | dest_ip_type | dest_ip | dest_ip_mask | dest_iprange_first | dest_iprange_last | dest_fqdn | protocol | dest_ports | target | enabled |
+-----+-------------+------------+--------------+---------+--------------+--------------------+-------------------+-------------+----------+------------+--------+---------+
| 187 | 3 | 56 | net | 8.8.8.8 | 32 | | | | both | 53 | ACCEPT | 1 |
| 188 | 2 | 56 | range | | 0 | 192.168.1.20 | 192.168.1.250 | | both | 53 | ACCEPT | 1 |
| 189 | 1 | 56 | fqdn | | 0 | | | my.host.com | both | 80 | ACCEPT | 1 |
+-----+-------------+------------+--------------+---------+--------------+--------------------+-------------------+-------------+----------+------------+--------+---------+
我的控制器根据" dest_ip_type"调用Validator。 :
public function validationNet(Validator $validator)
{
$validator = $this->validationDefault($validator);
$validator
->notEmpty('dest_ip', __('IP is required'))
->notEmpty('dest_ip_mask', __('Mask is required'))
->allowEmpty('dest_iprange_first')
->allowEmpty('dest_iprange_last')
->allowEmpty('dest_fqdn');
$validator
->add('dest_ip', 'notEmpty', [
'rule' => ['ip', 'ipv4'],
'message' => __('Invalid IP Address')
]);
return $validator;
}
public function validationIprange(Validator $validator)
{
$validator = $this->validationDefault($validator);
$validator
->notEmpty('dest_iprange_first', __('IP is required'))
->notEmpty('dest_iprange_last', __('IP is required'))
->allowEmpty('dest_ip')
->allowEmpty('dest_ip_mask')
->allowEmpty('dest_fqdn');
$validator
->add('dest_iprange_first', 'ipv4', [
'rule' => ['ip', 'ipv4'],
'message' => __('Invalid IP Address')
]);
$validator
->add('dest_iprange_last', 'ipv4', [
'rule' => ['ip', 'ipv4'],
'message' => __('Invalid IP Address')
]);
return $validator;
}
public function validationFqdn(Validator $validator)
{
$validator = $this->validationDefault($validator);
$validator
->notEmpty('dest_fqdn')
->allowEmpty('dest_iprange_first')
->allowEmpty('dest_iprange_last')
->allowEmpty('dest_ip')
->allowEmpty('dest_ip_mask');
$validator
->add('dest_fqdn', 'notEmpty', [
// Regex for FQDN
'rule' => ['custom', "/(?=^.{4,253}$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)/i"],
'message' => __('Invalid Hostname')
]);
return $validator;
}
我创建了3个RulesChecker,以不同的方式检查唯一性,但是如何告诉Validator或Controller不要调用默认值&#34; buildRules&#34;函数但强制调用替代buildRules?
public function buildNetRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['profile_id'], 'Profiles'));
$rules->add($rules->isUnique(['profile_id', 'dest_ip', 'protocol', 'dest_ports']));
return $rules;
}
public function buildRangeRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['profile_id'], 'Profiles'));
$rules->add($rules->isUnique(['profile_id', 'dest_iprange_first', 'dest_iprange_last', 'protocol', 'dest_ports']));
return $rules;
}
public function buildFqdnRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['profile_id'], 'Profiles'));
$rules->add($rules->isUnique(['profile_id', 'dest_fqdn', 'protocol', 'dest_ports']));
return $rules;
}
答案 0 :(得分:0)
我自己找到了解决方案。 buildRules能够在相同的函数内检查不同的isunique规则。也许buildRules不检查值为Empty或null的isUnique规则...
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->existsIn(['profile_id'], 'Profiles'));
$rules->add($rules->isUnique(['profile_id', 'dest_ip', 'protocol', 'dest_ports']));
$rules->add($rules->isUnique(['profile_id', 'dest_iprange_first', 'dest_iprange_last', 'protocol', 'dest_ports']));
$rules->add($rules->isUnique(['profile_id', 'dest_fqdn', 'protocol', 'dest_ports']));
return $rules;
}