Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1

(^232) CHAPTER 15 ■ INTRODUCTION TO THE ZEND FRAMEWORK
Using Metacommands
You can change the validation error messages through a special mechanism called a metacom-
mand. Metacommands are used as another value in a validation or filtering record. These are
special reserved words, or class constants that do something extra, such as setting the failure
messages. For example, to set the validation messages for your name field, you would use the
MESSAGES metacommand with a validation array, as shown in Listing 15-19.
Listing 15-19. Controlling Error Messages
$validation = array(
'name' => array (
array('StringLength', 1, 64),
Zend_Filter_Input::MESSAGES => array(
array(
Zend_Validate_StringLength::TOO_SHORT => 'Name cannot be blank',
Zend_Validate_StringLength::TOO_LONG => 'Name field is too long'
)
)
)
);
The ALLOW_EMPTY metacommand allows a field to be submitted as blank. The field will still
be required to be present in the input data array—this is called presence—but may have a value
of ''. You might think of Zend Framework presence versus empty as the difference between null
and '' in PHP. Listing 15-20 demonstrates using the ALLOW_EMPTY metacommand.
Listing 15-20. Allowing Empty Fields
$validation = array(
'name' => array (
'Alpha',
Zend_Filter_Input::ALLOW_EMPTY => true
)
);
This field may now contain zero or more alpha characters.
You may use several other metacommands:



  • PRESENCE: Require a key to be present in the input array (Boolean).

  • FIELDS: Don’t use the input key as the field name; override and use this value instead.

  • DEFAULT: Set a default value for the field if it is not present.

  • BREAK_CHAIN: Instead of applying all validators to a field, stop after the first failure.


McArthur_819-9C15.fm Page 232 Thursday, February 28, 2008 7:44 AM

Free download pdf