【问题标题】:Why multiple scenario are not working in Yii?为什么多个场景在 Yii 中不起作用?
【发布时间】:2014-04-25 08:45:11
【问题描述】:

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


型号:

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

控制器:

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

【问题讨论】:

  • 这是设计使然 - 一条记录一次只能分配一个场景。
  • 当然是覆盖ist,因为你先设置cost_spare_func为场景,然后cost_labour_func为实际场景。
  • @DCoder,我想在提交表单时同时显示两个验证。
  • @Jurik,你能告诉我场景和实际场景吗?如何实现这两个才能正确验证?

标签: php yii scenarios


【解决方案1】:

关于documents

首先需要注意的是,任何未分配场景的规则都将应用于所有场景。

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

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

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

在您的控制器中,您只需编写:

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 等默认验证。

这意味着您只能像这样定义validation function

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

【讨论】:

  • 好的,让我实现你的想法。
  • 实现后我发现两个函数'cost_spare_func','cost_labour_func'不能同时工作,但一次工作正常。
  • 我编辑了我的答案。这是我的错误 - 您只能进行一次检查 - 所以只能通过 match 进行检查或通过 function 进行检查 - 不能同时进行。但是当我看到您的模式时,我认为这可能更适合您的需求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
  • 2022-09-27
  • 1970-01-01
相关资源
最近更新 更多