Error handling in Yii's Rest api drives me nuts.

Hey guys, i need to share some of this with you.

When using the rest/api features in Yii, specifically the RestActiveController and you return a model with errors, it will prompt you the model errors in json/xml as:




[

   {"field":"username","message":"Whatever"},

   {"field":"password","message":"Whatever"},

]



And this is good for a single model, but what’s happening when we have to validate multiple models at once?

How do we go about this ?

Moreover, what happens if your main model has related models and they need to be subject to validation as well, and return nice structured error message, as in:




[

   {

       "some-model" : {

           "errors": {

               {"field":"username","message":"Whatever"},

               {"field":"password","message":"Whatever"},

           },

           "some-child/related-model" : {

               "errors": {

                   {"field":"username","message":"Whatever"},

                   {"field":"password","message":"Whatever"},

               }

           }

       }

   }

]



I didn’t see a way to return the errors in this way, or even closer.

Seems that everything the framework can do is to handle only single models, one at a time. Is this correct?

I don’t mind creating code for my specific use case, which i did anyway, but i want to make sure i don’t reinvent the wheel with this…

P.S: I know the json above is malformed, go with it :)

Thanks.