Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unknown scenario exception #16598

Closed
borodulin opened this issue Aug 3, 2018 · 5 comments
Closed

Unknown scenario exception #16598

borodulin opened this issue Aug 3, 2018 · 5 comments

Comments

@borodulin
Copy link

borodulin commented Aug 3, 2018

if (!isset($scenarios[$scenario])) {
throw new InvalidArgumentException("Unknown scenario: $scenario");
}

Why do we need such an exception?

I have model

class Contact extends \yii\db\ActiveRecord
{
    const SCENARIO_CREATE = 'create';
    const SCENARIO_UPDATE = 'update';

    public static function tableName()
    {
        return 'contact';
    }

    public function rules()
    {
        return [
            [['name'], 'required'],
            [['manager_id', 'type_id', 'ownership_id'], 'integer'],
            [['phones'], 'validatePhones', 'on' => self::SCENARIO_CREATE],
            [['manager_id'], 'validateManager', 'skipOnEmpty' => false, 'on' => self::SCENARIO_UPDATE],
        ];
    }
}

And I have Rest Controller

class ContactController extends \yii\rest\ActiveController
{

    public $modelClass = 'common\models\Contact';

    public $createScenario = Contact::SCENARIO_CREATE;
    public $updateScenario = Contact::SCENARIO_UPDATE;`
}

If I need to disable validator on "manager_id" field in Contact model, then I have to remove $updateScenario from ContactController. It seems to me it is not convenient.

I wish to create a basic Controller class with predefined scenarios and use these scenarios in models whenever I want, but now it is impossible.

@mgrechanik
Copy link
Contributor

mgrechanik commented Aug 3, 2018

Why do we need such an exception?

because it is logical.

@borodulin
Copy link
Author

it is not logical.

@rob006
Copy link
Contributor

rob006 commented Aug 3, 2018

You want to use scenario that is unsupported by model?

@borodulin
Copy link
Author

borodulin commented Aug 3, 2018

I think that there is no need to generate an exception if the model does not support the scenario, or it should be customizable.

@rob006
Copy link
Contributor

rob006 commented Aug 3, 2018

Using unsupported feature does not make sense and should throw exception - it is usually unintended configuration error or simple typo. Ignoring this is not an option, it will hide errors and make debugging much harder.

You can always override scenarios() to return all defined scenarios, even if there are no rules for them. Or just change scenario name:

const SCENARIO_UPDATE = self::SCENARIO_DEFAULT;

@rob006 rob006 closed this as completed Aug 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants