custom model error

hi

how i can custom model error for very complex operations ?

for example i need manage error in client.

example


#rules

return [

   ['username','required'],

   ['username','string','min' => 10],

   ['username',function(){

     #custom error

     $this->addError('username','NotExists','Message');

     $this->addError('test','NotExists','Message');

   }],

];


#error output

'username' => [

   'Required' => ['err1','err n',...],

   'StringInvalid' => ['err1','err n',...],

   'StringMin' => ['err1','err n',...],

   'NotExists' => ['err1','err n',...],

],

'test' => [

   'notExists' => ['err1','err n',...],

],




Hi,

Your question isn’t clear enough.

  1. Are you talking about the messages of the client-side validation?

  2. Do you want a ‘username’ + ‘test’ that already exists? I mean, do you want to use ‘exist’ validator for the combination of ‘username’ + ‘test’?

Almost,Yes.

no no,this is only example.

i want complex management error in client by sending output of model($model->errors) from server in response.

for example :

1- my action url is Person/Add

2- my model parameter in this action is ‘name’. (only one parameter)

3- my rules is [‘name’,‘string’,‘max’ => 3,‘tooLong’ => ‘Oh, this is tooLong Error in string validator.’];

4- send request from client [‘name’ => ‘softark’]

5- validate request by model and return errors : [‘name’ => [‘Oh, this is tooLong Error in string validator.’]],

6- process from client (like show error)

What i want ?

in step 5 and 6 from above example, i want change by this steps :

*5- validate request by model and return errors [‘name’ => [‘StringTooLong’ => ‘Oh, this is tooLong Error in string validator.’]]

*6- from client if error is ‘StringTooLong’ then do action.

actually, i want to manage my errors by unique key in client(like javascript or another system).

i think this feature not exists in yii and is BC break.

what is your comment ?

It sounds like you are trying to construct a different error reporting mechanism for Yii’s ActiveForm.

I wouldn’t try to do it.

oops!

so, below code is standard? and can i use ?


$model->addError('UserNotFound','User not found!');

The first parameter is intended to be an attribute name of the model.


http://www.yiiframework.com/doc-2.0/yii-base-model.html#addError()-detail

I’m not sure if it could be a virtual one without any underlying column or explicit definition.