#19034: Plural to MinLength, MaxLengt validation messages
--------------------------------------+------------------------------------
     Reporter:  clay.evil@…           |                    Owner:  nobody
         Type:  Bug                   |                   Status:  new
    Component:  Internationalization  |                  Version:  1.4
     Severity:  Normal                |               Resolution:
     Keywords:                        |             Triage Stage:  Accepted
    Has patch:  1                     |      Needs documentation:  0
  Needs tests:  0                     |  Patch needs improvement:  1
Easy pickings:  0                     |                    UI/UX:  0
--------------------------------------+------------------------------------

Comment (by anonymous):

 i don't know if its any help, but this is my solution to the problem. Im
 using it in my project.


 class NewBaseValidator(BaseValidator):

         def __init__(self, *args, **kwargs):
                 #  make possible to customise messages
                 if "message" in kwargs:
                         self.message = kwargs.pop("message")
                 #  make possible to customise code
                 if "code" in kwargs:
                         self.code = kwargs.pop("code")
                 super(NewBaseValidator, self).__init__(*args, **kwargs)
                 #  if no message is defined at class level, call the
 _get_message method
                 if not self.message:
                         self.message = self._get_message()

         def _get_message(self):
                 raise NotImplementedError

 class MinLengthValidator(NewBaseValidator):
         compare = lambda self, a, b: a < b
         clean   = lambda self, x: len(x)
         message = None
         code = 'min_length'

         def _get_message(self):
                 return ungettext_lazy(u'Ensure this value has at least
 %(limit_value)d character (it has %(show_value)d).',
                         u'Ensure this value has at least %(limit_value)d
 characters (it has %(show_value)d).', self.limit_value)

 class MaxLengthValidator(NewBaseValidator):
         compare = lambda self, a, b: a > b
         clean   = lambda self, x: len(x)
         message = None
         code = 'max_length'

         def _get_message(self):
                 return ungettext_lazy(u'Ensure this value has at most
 %(limit_value)d character (it has %(show_value)d).',
                         u'Ensure this value has at most %(limit_value)d
 characters (it has %(show_value)d).',self.limit_value)

-- 
Ticket URL: <https://code.djangoproject.com/ticket/19034#comment:6>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to