我正在尝试在CakePHP 3中编写验证规则,检查是否设置了名字和姓氏或公司名称。
校验:
$validator
->add('prename', 'custom', [
'rule' => [$this, 'validateName'],
'message' => __('Prename and lastname OR company name must be set.')
]);
$validator
->add('lastname', 'custom', [
'rule' => [$this, 'validateName'],
'message' => __('Prename and lastname OR company name must be set.')
]);
$validator
->add('name', 'custom', [
'rule' => [$this, 'validateName'],
'message' => __('Prename and lastname OR company name must be set.')
]);
规则定义:
public function validateName($check, array $context)
{
if((!empty($context['data']['prename']) && !empty($context['data']['lastname'])) || !empty($context['data']['name'])){
return true;
} else {
return false;
}
}
但验证不符合预期。如果我输入公司名称,我会得到prename和lastname的验证错误,说明这些字段是必需的。当我输入名字和姓氏时,它说公司名称是必需的。
我做错了什么?
答案 0 :(得分:2)
我还遇到了CakePhp 3.x这种奇怪的行为。我设法解决了这个问题。
以下是解决问题的代码
$validator
->notEmpty('company_name', 'Prename and lastname OR company name must be set.', function ($context){
if((!empty($context['data']['prename']) && !empty($context['data']['lastname'])) || !empty($context['data']['name'])){
return false;
} else {
return true;
}
});
$validator
->notEmpty('prename', 'Prename and lastname OR company name must be set.', function ($context){
if((!empty($context['data']['prename']) && !empty($context['data']['lastname'])) || !empty($context['data']['name'])){
return false;
} else {
return true;
}
});
$validator
->notEmpty('lastname', 'Prename and lastname OR company name must be set.', function ($context){
if((!empty($context['data']['prename']) && !empty($context['data']['lastname'])) || !empty($context['data']['name'])){
return false;
} else {
return true;
}
});
我希望这可以解决您的问题。
答案 1 :(得分:2)
所提供的答案都不能100%发挥作用,所以我将发布我的想法。我正在使用CakePHP 3文档中描述的conditional validation。
<强> CustomersTable.php:强>
public function validationDefault(Validator $validator)
{
$validator->notEmpty('name', __("Required when prename and lastname is empty."), function ($context) {
return !$context['data']['prename'] && !$context['data']['lastname'];
});
$validator->notEmpty('prename', __('Required when company or lastname is empty.'), function ($context) {
return !$context['data']['name'];
});
$validator->notEmpty('lastname', __('Required if company or prename is empty.'), function ($context) {
return !$context['data']['name'];
});
return $validator;
}
此方法在提交数据时正确验证数据。但是,有些浏览器说必须填写这些字段,因为它们是必需的。为避免这种情况,我们必须将字段定义为required => false
:
add.ctp查看文件:
echo $this->Form->input('prename', [
'label' => ['text' => __('Prename (*): ')],
'required' => false,
]);
echo $this->Form->input('lastname', [
'label' => ['text' => __('Lastname (*): ')],
'required' => false,
]);
echo $this->Form->input('name', [
'label' => ['text' => __('Company name (*): ')],
'required' => false,
]);
答案 2 :(得分:0)
我们在生产中运行以下代码,与您的代码非常相似:
$validator
->add('various_fee_text', 'custom', [
'rule' => [$this, 'variousFieldsChecker'],
'message' => __('Udfyld både Tekst og DKK feltet'),
])
->allowEmpty('various_fee_text');
Vaildation规则:
/**
* Method checks whether or not various fee text and various fee prices are either both empty, both set or "dirt",
* meaning only one of them is set and the other is empty. We want either both to be set or both to be empty.
*
* @return bool depending on it's OK or not
*/
public function variousFieldsChecker($value, $context)
{
$varioustext = $context['data']['various_fee_text'];
$variousPrice = $context['data']['various_fee_price'];
if (!empty($varioustext) && !empty($variousPrice)) {
return true;
} elseif ((empty($varioustext) && empty($variousPrice))) {
return true;
}
return false;
}
我认为您需要的是附加->allowEmpty()
之前建议的内容。
答案 3 :(得分:0)
为此CakePhp提供创建自定义验证。您需要在模型类上创建两个验证函数,并在控制器函数上有条件地调用它们。您可以像
一样创建验证功能 public function validationName($validator){
$validator
->requirePresence('prename')
->notEmpty('prename', 'Prename must be set.')
->requirePresence('lastname')
->notEmpty('lastname', 'Lastname must be set.')
->allowEmpty('name');
return $validator;
}
public function validationCompany($validator){
$validator
->requirePresence('name')
->notEmpty('name', 'Company name must be set.')
->allowEmpty('prename')
->allowEmpty('lastname');
return $validator;
}
在控制器上,您可以像
一样有条件地调用这些功能$validator = 'company';
if(empty($this->request->data[''])){
$validator = 'name';
}
$entity = $this->{$this->modelClass}->newEntity();
$entitydata = $this->{$this->modelClass}->patchEntity($entity, $this->request->data, ['validate' => $validator]);
$this->{$this->modelClass}->save($entitydata);
答案 4 :(得分:-2)
你需要查看表格文件名或在你的html视图部分检查输入的ID。