Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 model validation message from the label assigned via ActiveForm

Is there any way to prompt for validation with the attribute's label assigned via ActiveForm?

For instance I have a model attribute amount and the label defined in its attributeLabels function is "Amount"

But while generating form I neeed the label "Fees" so:
$form->field($model, 'amount')->textInput(['maxlength' => true])->label('Fees')

After validation it prompts me "Amount cannot be blank" - it is known to me that we can write a rule to change message but according to my requirements, same attribute (from same model) is having different labels on different forms.

I know that back-end implementation of default message uses:
'message' => '{attribute} cannot be blank.' does anyone know if there is any {x} by which the assigned label in ActiveForm can be retrieved?


PS: I know that this problem can be resolved by scenarios. But it would be hectic to write rule for every field which has dual label.

like image 742
Danish Ahmed Avatar asked Dec 14 '25 02:12

Danish Ahmed


2 Answers

There is no such way, sorry! What you are doing is overriding the label within the view. Actually within the form to be more precise. If you check out what the label()-method does, you will see that it calls activeLabel() from the Html-helper. This method in turn renders your label and returns the code.

As you can see, none of this information gets written back to the model. Therefore, during the validation process, you won't have your new label at hand, because it never makes its way into the model.

Your cleanest option is the one you mentioned already. Use scenarios to decide which validation rule (and therefore message) to use. OR you could create your own public property in which you write your temporary label like so:

class MyModel extends \yii\db\ActiveRecord
{
    public $myTempLabel;

    public function attributeLabels()
    {
        $myLabel = $this->myTempLabel === null ? Yii::t('app', 'defaultLabel') : $this->myTempLabel;

        return [
            //other labels...
            'myField'=>$myLabel,
            //other labels...
        ];
    }
}

In your view you can then set the value back into the attribute within your model.

Sorry for not being able to help better, but that's the way it's implemented!

like image 107
PLM57 Avatar answered Dec 15 '25 22:12

PLM57


What you need to do is handle the label changing logic in your attributeLabels() function.

class MyModel extends \yii\base\Model {

  public $amount_label = 'amount';

  public function attributeLabels() {
    return [
       'amount' => $this->amount_label
    ];
  }
}

Then in your controller.

$model = new MyModel;
$model->amount_label = 'fees';
...

Of course you may want to set the label in a different way. For example, if your model as a type attribute, and this is the attribute which determines the label, you could do a conditional based on that type attribute.

like image 33
Maddelynne P Avatar answered Dec 15 '25 21:12

Maddelynne P