3

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');
  }
}
Jurik
  • 3,244
  • 1
  • 31
  • 52
Kapur
  • 173
  • 3
  • 10
  • That's by design - a record can only have one scenario assigned at a time. – DCoder Apr 25 '14 at 08:53
  • Of course it is overwriting ist, because you set first `cost_spare_func` as scenario and then `cost_labour_func` as the actual scenario. – Jurik Apr 25 '14 at 08:54
  • @DCoder, i wanted to show the two validation at a time when by submitting the form. – Kapur Apr 25 '14 at 08:57
  • @Jurik, can you please tell me about scenario and actual scenario? How to implement these two to have validated properly? – Kapur Apr 25 '14 at 08:59

1 Answers1

9

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'),
Jurik
  • 3,244
  • 1
  • 31
  • 52
  • Sure, let me implement your ideas. – Kapur Apr 25 '14 at 09:14
  • After implementing this I found that two functions 'cost_spare_func','cost_labour_func' are not working simultaneously but working fine at a time. – Kapur Apr 25 '14 at 09:30
  • I edited my answer. It was my mistake - you can have only ONE check - so only check by `match` or check by `function` - not both. But as I see your pattern, I think this might fit better to your needs. – Jurik Apr 25 '14 at 09:43