gpt4 book ai didi

php - 为什么多个场景在 Yii 中不起作用?

转载 作者:可可西里 更新时间:2023-11-01 00:09:07 25 4
gpt4 key购买 nike

我在我的应用程序中使用了多个场景,但遇到的问题是每次最后一个场景都会覆盖第一个场景。


型号:

public function rules()
{
return array(
[...]
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'),
array('cost_labour', 'cost_labour_func', 'match',
'pattern' => '/^[a-zA-Z]+$/',
'message' => 'Do not enter zero or/and characters for Labour Charges!',
'on' => 'cost_labour_func'),
);
}

Controller :

public function actionUpdate ($id)
{
if (isset($_POST['TblEnquiry']))
{
[...]
$model->setScenario('cost_spare_func');
$model->setScenario('cost_labour_func');
}
}

最佳答案

关于 documents :

Firstly it is important to note that any rules not assigned a scenario will be applied to all scenarios.

所以我认为您可能不需要场景,只需使用通用规则/验证即可。

您的规则有一个场景,如下所示:

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'),
);
}

然后在你的 Controller 中你只需要写:

public function actionUpdate ($id)
{
if (isset($_POST['TblEnquiry']))
{
[...]
$model->setScenario('myScenario');
}
}

编辑:
关于this document ,我只看到您只需要 numerical 输入。所以这可能更适合您的需求。由于两者都得到了相同的支票,您可以只进行一次支票,然后将消息传递给它。但就目前而言,这应该可行。

额外:
就像您写的那样,您的规则中还有另一个错误。

  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

这意味着您只能像这样定义一个验证函数:

  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'),

关于php - 为什么多个场景在 Yii 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23288302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com