Php 为什么多个场景在Yii中不起作用?
我在应用程序中使用了多个场景,但每次最后一个场景都会覆盖第一个场景时都会遇到问题Php 为什么多个场景在Yii中不起作用?,php,yii,scenarios,Php,Yii,Scenarios,我在应用程序中使用了多个场景,但每次最后一个场景都会覆盖第一个场景时都会遇到问题 型号: 控制器: 关于: 首先,需要注意的是,任何未分配给场景的规则都将应用于所有场景 因此,我认为您可能不需要场景,只需要使用通用规则/验证 或 您的规则有一个场景,如下所示: public function rules() { return array( [...] array('cost_spares','numerical', 'integerOnly'
型号:
控制器: 关于: 首先,需要注意的是,任何未分配给场景的规则都将应用于所有场景 因此,我认为您可能不需要场景,只需要使用通用规则/验证 或 您的规则有一个场景,如下所示:
public function rules()
{
return array(
[...]
array('cost_spares','numerical',
'integerOnly' => true,
'min' => 1,
'max' => 250,
'tooSmall' => 'You must order at least 1 piece',
'tooBig' => 'You cannot order more than 250 pieces at once',
'message' => 'Do not enter zero or/and characters for Spare parts!',
'on' => 'myScenario'),
array('cost_labour','numerical',
'integerOnly' => true,
'min' => 1,
'max' => 250,
'tooSmall' => 'You must order at least 1 piece',
'tooBig' => 'You cannot order more than 250 pieces at once',
'message' => 'Do not enter zero or/and characters for Labour Charges!',
'on' => 'myScenario'),
);
}
array('cost_spares', 'cost_spare_func',
'message' => 'Do not enter zero or/and characters for Spare parts!',
'on' => 'cost_spare_func'),
array('cost_spares', 'match',
'pattern' => '/^[a-zA-Z]+$/',
'message' => 'Do not enter zero or/and characters for Spare parts!',
'on' => 'cost_spare_func'),
在控制器中,您只需编写:
public function actionUpdate ($id)
{
if (isset($_POST['TblEnquiry']))
{
[...]
$model->setScenario('myScenario');
}
}
编辑:关于,我只看到您只需要
数字
输入。所以这可能更适合你的需要。由于两者都得到了相同的检查,您可以只进行一次检查,然后将消息传递给它。但就目前而言,这应该是可行的
额外的:你的规则中还有一个bug,就像你写的那样
array('cost_spares', 'cost_spare_func', 'match',
'pattern' => '/^[a-zA-Z]+$/',
'message' => 'Do not enter zero or/and characters for Spare parts!',
'on' => 'cost_spare_func'),
这是不可能的。不能将规则验证函数和默认验证(如match
)混合使用
这意味着您只能像这样定义验证函数
:
public function rules()
{
return array(
[...]
array('cost_spares','numerical',
'integerOnly' => true,
'min' => 1,
'max' => 250,
'tooSmall' => 'You must order at least 1 piece',
'tooBig' => 'You cannot order more than 250 pieces at once',
'message' => 'Do not enter zero or/and characters for Spare parts!',
'on' => 'myScenario'),
array('cost_labour','numerical',
'integerOnly' => true,
'min' => 1,
'max' => 250,
'tooSmall' => 'You must order at least 1 piece',
'tooBig' => 'You cannot order more than 250 pieces at once',
'message' => 'Do not enter zero or/and characters for Labour Charges!',
'on' => 'myScenario'),
);
}
array('cost_spares', 'cost_spare_func',
'message' => 'Do not enter zero or/and characters for Spare parts!',
'on' => 'cost_spare_func'),
array('cost_spares', 'match',
'pattern' => '/^[a-zA-Z]+$/',
'message' => 'Do not enter zero or/and characters for Spare parts!',
'on' => 'cost_spare_func'),
或使用如下默认验证:
public function rules()
{
return array(
[...]
array('cost_spares','numerical',
'integerOnly' => true,
'min' => 1,
'max' => 250,
'tooSmall' => 'You must order at least 1 piece',
'tooBig' => 'You cannot order more than 250 pieces at once',
'message' => 'Do not enter zero or/and characters for Spare parts!',
'on' => 'myScenario'),
array('cost_labour','numerical',
'integerOnly' => true,
'min' => 1,
'max' => 250,
'tooSmall' => 'You must order at least 1 piece',
'tooBig' => 'You cannot order more than 250 pieces at once',
'message' => 'Do not enter zero or/and characters for Labour Charges!',
'on' => 'myScenario'),
);
}
array('cost_spares', 'cost_spare_func',
'message' => 'Do not enter zero or/and characters for Spare parts!',
'on' => 'cost_spare_func'),
array('cost_spares', 'match',
'pattern' => '/^[a-zA-Z]+$/',
'message' => 'Do not enter zero or/and characters for Spare parts!',
'on' => 'cost_spare_func'),
这是出于设计-一个记录一次只能分配一个场景。当然,它会覆盖ist,因为您先将
cost\u spare\u func
设置为场景,然后将cost\u labour\u func
设置为实际场景。@DCoder,我想在提交表单时同时显示这两个验证。@Jurik,你能告诉我场景和实际场景吗?如何实现这两个功能才能正确验证?当然,让我来实现你的想法。实现后,我发现“cost\u spare\u func”和“cost\u labour\u func”两个函数不是同时工作的,而是一次工作得很好。我编辑了我的答案。这是我的错误-您只能进行一次检查-因此只能通过匹配进行检查
或通过功能进行检查
-而不是同时进行检查。但在我看来,这可能更符合你的需要。