Error handling in Actions

Hello everybody, I justed started a few days ago to work with the yii2.0 framework.

On my site the user can enter a licence key. After pressing the submit button I check on multiple things like

  • does the entered key exists
  • is the entered key already in use
  • is the entered key expired

On each case I want to give the user some error message. What is the best way to do this with the yii framework?

public function actionActivateTemplateKey()
{
    $model = new TemplateKeyActivationForm();
    $templateKey = TemplateKey::find()->where(...)->one();

    if($templateKey == null)
    {
        // key does not exists
        return $this->render('activate-key', ['model' => $model, 'MSG_KeyNotExists' => $model->LicenceKey]);
    }
    if($templateKey->is_used)
    {
        // key is already in use, return error message
        return $this->render('activate-key', ['model' => $model, 'MSG_KeyAlreadyUsed' => $model->LicenceKey]);
    }

    ...
}

My first idea was to add one more value to the view. So I can easily check in the view file

if(isset($MSG_KeyAlreadyUsed)

My other idea was too add something like an error-code to the modell object (TemplateKeyActivationForm). If the error-code is filled I can check the several error-codes at the view.
The error-code could also be replaced with an enum, but then I will create an enum for every Form where any validation will happen.

(one general question: Should the validation happen in the action method or better in the model-object?)

Is there any better solution for this? Both don’t feel really good. Thanks alot!

This sounds like input validation, see

https://www.yiiframework.com/doc/guide/2.0/en/input-validation

on how to do it in Yii.

on the model, it has all functionality that you need.

  1. create a form model for the form where the user enters the key:
    https://www.yiiframework.com/doc/guide/2.0/en/input-forms
  2. Write validation rules
    https://www.yiiframework.com/doc/guide/2.0/en/input-validation

Thanks for your help @CeBe. I want don’t want to show the error message under the input field. I want to build some custom container where I can display the error. Something like this:

The message “Input must be filled” is a standard message. This is okay. But for a message like “The key is already taken” the error should be a bit more noticeable.

Is that possible? I searched now for a while but couldn’t find anything. Thanks!

You can always access your errors through yii\base\Model::getErrorSummary() or yii\base\Model::getErrors($attribute) or yii\base\Model::getFirstError($attribute).

Thanks for your comment @Coksnuss .
I have one more question so far. Can I deactivate the standard output under the input field? Then I should have everything to get my desired state.

Of course. This is handled in the view. Since you didn’t post anything in particular regarding your view template, I can only assume that you are using yii\widgets\ActiveForm? In that case you can simply adapt yii\widgets\ActiveField::$template (defaults to “{label}\n{input}\n{hint}\n{error}”).

For instance:

/** @var ActiveForm $form */
$form->field($model, 'licenceKey', ['template' => '{label}\n{input}\n{hint}'])

Yes, I am using the ActiveForm.
Can I set the error templates rule specific?

I want to show the
Input must be filled -Message direct under the input field.
Messages like
Key is already taken or Key is no valid
should be shown above the input field.

No thats not (natively) possible because an error is always associated with a specific attribute. You could introduce a new attribute in your form model to which you assign the error messages that you want to display in the error summary.