0

I have an form that requires different scenarios based on selections on the form. For example: the user can choose to add a delivery address in a shop that's different from the billing-address (this is simple, in reality, I'll have like 3 or 4 different scenarios).

So, when validating the form, depending on the user's selection, I need to combine two scenarios (the one for the billing address and the one for the delivery address).

How would I combine the two scenarios?

Swissdude
  • 3,486
  • 3
  • 35
  • 68

1 Answers1

1

You can't group scenarios the the rules configuration. Instead you create a list of rules one by one.

You are able to apply more than one scenario to a set of fields.

For example :

public function rules()
{

    return array(
        // For purchases, supply the delivery address
        // For registration, supply the postal address
        // For payment, supply the delivery address and postal address

        array('delivery_address', 'required', 'on' => array('purchase', 'payment'),
        array('postal_address', 'required', 'on' => array('register', 'payment'),
                 // :

    );
}

You cannot have 'conditional' scenarios. To implemented conditional rules, look at implementing custom validations.

public function rules()
{
    return array(
       array('postal_address', 'validateAddresses'),
    );
}

public function validateAddresses($attribute,$params) {
  if (!$this->deliveryAddressSameAsPostal) {
      if ((empty($this->delivery_address)) {
        $this->addError($attribute, 'Please supply a delivery address!');
      }
  }
}
crafter
  • 6,246
  • 1
  • 34
  • 46
  • Thanks crafter. Problem is, that the delivery-address is only needed when the user indicates that it's different from the postal-address. Therefore, I can't really create a scenario for that. Is there something like a conditional scenario? – Swissdude Jan 22 '15 at 10:52
  • In your example, you are confusing scenarios and rules. See my updates. – crafter Jan 23 '15 at 08:25