Wprowadzenie do Zend Framework

     Nauka Zend Framework


     Przewodnik po Zend Framework

  • Zend_Gdata
  • Zend_Http
  • Zend_InfoCard
  • Zend_Json
  • Zend_Layout
  • Zend_Ldap
  • Zend_Loader
  • Zend_Locale
  • Zend_Log
  • Zend_Mail
  • Zend_Markup
  • Zend_Measure
  • Zend_Memory
  • Zend_Mime
  • Zend_Navigation
  • Zend_Oauth
  • Zend_OpenId
  • Zend_Paginator
  • Zend_Pdf
  • Zend_ProgressBar
  • Zend_Queue
  • Zend_Reflection
  • Zend_Registry
  • Zend_Rest

  • Zend_Search_Lucene
  • Zend_Serializer
  • Zend_Server
  • Zend_Service
  • Zend_Session
  • Zend_Soap
  • Zend_Tag
  • Zend_Test
  • Zend_Text
  • Zend_TimeSync
  • Zend_Tool
  • Zend_Tool_Framework
  • Zend_Tool_Project
  • Zend_Translate
  • Zend_Uri
  • Zend_Validate
  • Zend_Version
  • Zend_View
  • Zend_Wildfire
  • Zend_XmlRpc
  • ZendX_Console_Process_Unix
  • ZendX_JQuery
  • Translation 21.3% Update 2011-11-16 - Revision 24356 - Version ZF 1.11.x

    73.5. Validation Messages

    Each validator which is based on Zend_Validate provides one or multiple messages in the case of a failed validation. You can use this information to set your own messages, or to translate existing messages which a validator could return to something different.

    These validation messages are constants which can be found at top of each validator class. Let's look into Zend_Validate_GreaterThan for an descriptive example:

    protected $_messageTemplates = array(
    self::NOT_GREATER => "'%value%' is not greater than '%min%'",

    As you can see the constant self::NOT_GREATER refers to the failure and is used as key, and the message itself is used as value of the message array.

    You can retrieve all message templates from a validator by using the getMessageTemplates() method. It returns you the above array which contains all messages a validator could return in the case of a failed validation.

    $validator = new Zend_Validate_GreaterThan();
    $messages  $validator->getMessageTemplates();

    Using the setMessage() method you can set another message to be returned in case of the specified failure.

    $validator = new Zend_Validate_GreaterThan();
    'Please enter a lower value',

    The second parameter defines the failure which will be overridden. When you omit this parameter, then the given message will be set for all possible failures of this validator.

    73.5.1. Using pre-translated validation messages

    Zend Framework is shipped with more than 45 different validators with more than 200 failure messages. It can be a tedious task to translate all of these messages. But for your convenience Zend Framework comes with already pre-translated validation messages. You can find them within the path /resources/languages in your Zend Framework installation.

    [Notatka] Used path

    The resource files are outside of the library path because all of your translations should also be outside of this path.

    So to translate all validation messages to German for example, all you have to do is to attach a translator to Zend_Validate using these resource files.

    $translator = new Zend_Translate(
    'adapter' => 'array',
    'content' => '/resources/languages',
    'locale'  => $language,
    'scan' => Zend_Translate::LOCALE_DIRECTORY
    [Notatka] Used translation adapter

    As translation adapter Zend Framework chose the array adapter. It is simple to edit and created very fast.

    [Notatka] Supported languages

    This feature is very young, so the amount of supported languages may not be complete. New languages will be added with each release. Additionally feel free to use the existing resource files to make your own translations.

    You could also use these resource files to rewrite existing translations. So you are not in need to create these files manually yourself.

    73.5.2. Limit the size of a validation message

    Sometimes it is necessary to limit the maximum size a validation message can have. For example when your view allows a maximum size of 100 chars to be rendered on one line. To simplify the usage, Zend_Validate is able to automatically limit the maximum returned size of a validation message.

    To get the actual set size use Zend_Validate::getMessageLength(). If it is -1, then the returned message will not be truncated. This is default behaviour.

    To limit the returned message size use Zend_Validate::setMessageLength(). Set it to any integer size you need. When the returned message exceeds the set size, then the message will be truncated and the string '...' will be added instead of the rest of the message.

    [Notatka] Where is this parameter used?

    The set message length is used for all validators, even for self defined ones, as long as they extend Zend_Validate_Abstract.

    digg delicious meneame google twitter technorati facebook