MyEntity.phpモデルがあります。モデル スクリプトの一部として、いくつかのルールといくつかのシナリオが定義されています。
public function rules()
{
return [
[['myentity_id', 'myentity_title', 'myentity_content', 'myentity_date'], 'required'],
[['myentity_id'], 'integer'],
[['myentity_title', 'myentity_content'], 'string', 'max' => 120],
[['myentity_date'], 'safe'],
];
}
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios['scenario_one'] = ['myentity_id', 'myentity_title'];
$scenarios['scenario_two'] = ['myentity_id', 'myentity_content'];
return $scenarios;
}
私はさまざまなシナリオを持つことができる必要があり、さまざまなアクションに対して特定の検証 (パラメーターによる) のみをアクティブにする必要があります。たとえば、actionOne の場合は scenario_one、actionTwo の場合は scenario_two などです。
コントローラーのコードの一部を次に示します。
public function actionOne($id)
{
$modelMyEntity = $this->findModel($id);
$modelMyEntity->scenario = 'scenario_one';
.
.
.
}
public function actionTwo($id)
{
$modelMyEntity = $this->findModel($id);
$modelMyEntity->scenario = 'scenario_two';
.
.
.
}
ここで、検証がまったく行われてはならない scenario_three が必要です。データベースへの保存中に失敗しないように、コードに追加のチェックを行います。フォームの送信を妨げているため、検証が適用されていないことを確認する必要があります。どのシナリオも適用しない場合は、デフォルトのシナリオが適用されます (リストされているすべての検証がアクティブになります。これは、必要なシナリオとはまったく逆です)。