Question

I am using multiple scenarios in my application but facing a problem that every time the last scenario overrides the first one.


Model:

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');
  }
}
Was it helpful?

Solution

Regarding to the documents:

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

So I think you maybe do not need a scenario and just use common rules/validation.

OR

You have ONE scenario for your rules like this:

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

And in your controller you just write:

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

Edit:
Regarding to this document, I just see that you only want numerical input. So this might fit better to your needs. And since both got same check, you could just do one check and pass the message later into it. But for now, this should work.

Extra:
There is another bug in your rules like you wrote.

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

That is not possible. You can not mix a rule validate function and a default validation like match.

That means you can only define a validation function like this:

  array('cost_spares', 'cost_spare_func',
    'message' => 'Do not enter zero or/and characters for Spare parts!',
    'on' => 'cost_spare_func'),

OR use a default validation like this:

  array('cost_spares', 'match',
    'pattern' => '/^[a-zA-Z]+$/',
    'message' => 'Do not enter zero or/and characters for Spare parts!',
    'on' => 'cost_spare_func'),
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top