Yii Framework Forum: Add Error Codes For Validation Errors - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Add Error Codes For Validation Errors To find out programatically which validation error occured

#1 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 12 September 2012 - 02:26 AM

*
POPULAR

In the current implementation you can very easily find out, if a validation error occurred on an attribute. But it's very hard to find out programatically, which validation error it was. You have to parse an error message like "First name is too short (minimum is 30 characters)" to find out, that length validation failed. Situation gets worse if you customize your error messages and it's almost impossible if you add translation through Yii::t().

Why could that be useful? Some examples:

* You want to write an API for your app and want to give as much freedom to your client developers as possible. So they should decide, how to react on different validation errors

* You want to write your own clientside validation logic in javascript. Different things should happen on different validation errors.

I think there are many more use cases of this.

I don't have a clear idea about implementation. But here's a first suggestion:

We could have getErrorCode($attribute) and getErrorCodes($attribute=null) methods in CModel. They would work much the same as getError and getErrors with the only difference, that they return something like CStringValidator::TOO_SHORT. That constant could be a verbose string like 'string-too-short' or 'number-no-integer'. So the pattern could be '<validator>-<message>'.

What do you think?
5

#2 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 12 September 2012 - 02:49 AM

Nice idea.

Meanwhile, prefixing the localized error message itself with a custom code helps achieving a correct result if I understand well what you intend to have. When dealing with custom validation JS or serving app through an API, the clients would expect a <code> <some special agreed character> <localized error message to display as is if needed>. Just saying :)
0

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 12 September 2012 - 02:56 AM

If i prefix the validation messages, then this also appears on the regular web pages. That's not what i want. And i'd consider this a bit of a hack. :) We should have a proper solution.
0

#4 User is offline   Web Guru 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 27-January 13

Posted 27 October 2013 - 04:21 AM

Hi Mike,

Are you able to find a solution to support error codes? or the only way is as you suggested above

Thanks,
Omar
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users