Zend_Validate.xml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.validate.introduction">
  4. <title>Introduction</title>
  5. <para>
  6. The <classname>Zend_Validate</classname> component provides a set of commonly needed
  7. validators. It also provides a simple validator chaining mechanism by
  8. which multiple validators may be applied to a single datum in a
  9. user-defined order.
  10. </para>
  11. <sect2 id="zend.validate.introduction.definition">
  12. <title>What is a validator?</title>
  13. <para>
  14. A validator examines its input with respect to some requirements
  15. and produces a boolean result - whether the input successfully
  16. validates against the requirements. If the input does not meet the
  17. requirements, a validator may additionally provide information
  18. about which requirement(s) the input does not meet.
  19. </para>
  20. <para>
  21. For example, a web application might require that a username be
  22. between six and twelve characters in length and may only contain
  23. alphanumeric characters. A validator can be used for ensuring that
  24. usernames meet these requirements. If a chosen username does not
  25. meet one or both of the requirements, it would be useful to know
  26. which of the requirements the username fails to meet.
  27. </para>
  28. </sect2>
  29. <sect2 id="zend.validate.introduction.using">
  30. <title>Basic usage of validators</title>
  31. <para>
  32. Having defined validation in this way provides the foundation for
  33. <classname>Zend_Validate_Interface</classname>, which defines two methods,
  34. <methodname>isValid()</methodname> and <methodname>getMessages()</methodname>. The
  35. <methodname>isValid()</methodname> method performs validation upon the provided
  36. value, returning <constant>TRUE</constant> if and only if the value passes
  37. against the validation criteria.
  38. </para>
  39. <para>
  40. If <methodname>isValid()</methodname> returns <constant>FALSE</constant>, the
  41. <methodname>getMessages()</methodname> returns an array of messages explaining
  42. the reason(s) for validation failure. The array keys are short
  43. strings that identify the reasons for validation failure, and the
  44. array values are the corresponding human-readable string messages.
  45. The keys and values are class-dependent; each validation class
  46. defines its own set of validation failure messages and the unique
  47. keys that identify them. Each class also has a const
  48. definition that matches each identifier for a validation failure
  49. cause.
  50. </para>
  51. <note>
  52. <para>
  53. The <methodname>getMessages()</methodname> methods return validation
  54. failure information only for the most recent
  55. <methodname>isValid()</methodname> call. Each call to
  56. <methodname>isValid()</methodname> clears any messages and errors caused by
  57. a previous <methodname>isValid()</methodname> call, because it's likely
  58. that each call to <methodname>isValid()</methodname> is made for a
  59. different input value.
  60. </para>
  61. </note>
  62. <para>
  63. The following example illustrates validation of an e-mail address:
  64. </para>
  65. <programlisting language="php"><![CDATA[
  66. $validator = new Zend_Validate_EmailAddress();
  67. if ($validator->isValid($email)) {
  68. // email appears to be valid
  69. } else {
  70. // email is invalid; print the reasons
  71. foreach ($validator->getMessages() as $messageId => $message) {
  72. echo "Validation failure '$messageId': $message\n";
  73. }
  74. }
  75. ]]></programlisting>
  76. </sect2>
  77. <sect2 id="zend.validate.introduction.messages">
  78. <title>Customizing messages</title>
  79. <para>
  80. Validate classes provide a <methodname>setMessage()</methodname> method with
  81. which you can specify the format of a message returned by
  82. <methodname>getMessages()</methodname> in case of validation failure. The
  83. first argument of this method is a string containing the error
  84. message. You can include tokens in this string which will be
  85. substituted with data relevant to the validator. The token
  86. <emphasis>%value%</emphasis> is supported by all validators; this is
  87. substituted with the value you passed to <methodname>isValid()</methodname>.
  88. Other tokens may be supported on a case-by-case basis in each
  89. validation class. For example, <emphasis>%max%</emphasis> is a token
  90. supported by <classname>Zend_Validate_LessThan</classname>.
  91. The <methodname>getMessageVariables()</methodname> method returns an array
  92. of variable tokens supported by the validator.
  93. </para>
  94. <para>
  95. The second optional argument is a string that identifies the
  96. validation failure message template to be set, which is useful when
  97. a validation class defines more than one cause for failure. If you
  98. omit the second argument, <methodname>setMessage()</methodname> assumes the
  99. message you specify should be used for the first message template
  100. declared in the validation class. Many validation classes only have
  101. one error message template defined, so there is no need to specify
  102. which message template you are changing.
  103. </para>
  104. <programlisting language="php"><![CDATA[
  105. $validator = new Zend_Validate_StringLength(8);
  106. $validator->setMessage(
  107. 'The string \'%value%\' is too short; it must be at least %min% ' .
  108. 'characters',
  109. Zend_Validate_StringLength::TOO_SHORT);
  110. if (!$validator->isValid('word')) {
  111. $messages = $validator->getMessages();
  112. echo current($messages);
  113. // "The string 'word' is too short; it must be at least 8 characters"
  114. }
  115. ]]></programlisting>
  116. <para>
  117. You can set multiple messages using the <methodname>setMessages()</methodname>
  118. method. Its argument is an array containing key/message pairs.
  119. </para>
  120. <programlisting language="php"><![CDATA[
  121. $validator = new Zend_Validate_StringLength(array('min' => 8, 'max' => 12));
  122. $validator->setMessages( array(
  123. Zend_Validate_StringLength::TOO_SHORT =>
  124. 'The string \'%value%\' is too short',
  125. Zend_Validate_StringLength::TOO_LONG =>
  126. 'The string \'%value%\' is too long'
  127. ));
  128. ]]></programlisting>
  129. <para>
  130. If your application requires even greater flexibility with which it
  131. reports validation failures, you can access properties by the same
  132. name as the message tokens supported by a given validation class.
  133. The <property>value</property> property is always available in a validator;
  134. it is the value you specified as the argument of
  135. <methodname>isValid()</methodname>. Other properties may be supported on a
  136. case-by-case basis in each validation class.
  137. </para>
  138. <programlisting language="php"><![CDATA[
  139. $validator = new Zend_Validate_StringLength(array('min' => 8, 'max' => 12));
  140. if (!validator->isValid('word')) {
  141. echo 'Word failed: '
  142. . $validator->value
  143. . '; its length is not between '
  144. . $validator->min
  145. . ' and '
  146. . $validator->max
  147. . "\n";
  148. }
  149. ]]></programlisting>
  150. </sect2>
  151. <sect2 id="zend.validate.introduction.static">
  152. <title>Using the static is() method</title>
  153. <para>
  154. If it's inconvenient to load a given validation class and create an
  155. instance of the validator, you can use the static method
  156. <methodname>Zend_Validate::is()</methodname> as an alternative invocation
  157. style. The first argument of this method is a data input value,
  158. that you would pass to the <methodname>isValid()</methodname> method. The
  159. second argument is a string, which corresponds to the basename of
  160. the validation class, relative to the <classname>Zend_Validate</classname>
  161. namespace. The <methodname>is()</methodname> method automatically loads the
  162. class, creates an instance, and applies the <methodname>isValid()</methodname>
  163. method to the data input.
  164. </para>
  165. <programlisting language="php"><![CDATA[
  166. if (Zend_Validate::is($email, 'EmailAddress')) {
  167. // Yes, email appears to be valid
  168. }
  169. ]]></programlisting>
  170. <para>
  171. You can also pass an array of constructor arguments, if they
  172. are needed for the validator.
  173. </para>
  174. <programlisting language="php"><![CDATA[
  175. if (Zend_Validate::is($value, 'Between', array('min' => 1, 'max' => 12))) {
  176. // Yes, $value is between 1 and 12
  177. }
  178. ]]></programlisting>
  179. <para>
  180. The <methodname>is()</methodname> method returns a boolean value, the same as
  181. the <methodname>isValid()</methodname> method. When using the static
  182. <methodname>is()</methodname> method, validation failure messages are not
  183. available.
  184. </para>
  185. <para>
  186. The static usage can be convenient for invoking a validator ad hoc,
  187. but if you have the need to run a validator for multiple inputs,
  188. it's more efficient to use the non-static usage, creating an
  189. instance of the validator object and calling its
  190. <methodname>isValid()</methodname> method.
  191. </para>
  192. <para>
  193. Also, the <classname>Zend_Filter_Input</classname> class allows you to
  194. instantiate and run multiple filter and validator classes on demand
  195. to process sets of input data. See
  196. <link linkend="zend.filter.input">Zend_Filter_Input</link>.
  197. </para>
  198. <sect3 id="zend.validate.introduction.static.namespaces">
  199. <title>Namespaces</title>
  200. <para>
  201. When working with self defined validators you can give a fourth parameter
  202. to <methodname>Zend_Validate::is()</methodname> which is the namespace
  203. where your validator can be found.
  204. </para>
  205. <programlisting language="php"><![CDATA[
  206. if (Zend_Validate::is($value, 'MyValidator', array('min' => 1, 'max' => 12),
  207. array('FirstNamespace', 'SecondNamespace')) {
  208. // Yes, $value is ok
  209. }
  210. ]]></programlisting>
  211. <para>
  212. <classname>Zend_Validate</classname> allows also to set namespaces as default.
  213. This means that you can set them once in your bootstrap and have not to give
  214. them again for each call of <methodname>Zend_Validate::is()</methodname>. The
  215. following code snippet is identical to the above one.
  216. </para>
  217. <programlisting language="php"><![CDATA[
  218. Zend_Validate::setDefaultNamespaces(array('FirstNamespace', 'SecondNamespace'));
  219. if (Zend_Validate::is($value, 'MyValidator', array('min' => 1, 'max' => 12)) {
  220. // Yes, $value is ok
  221. }
  222. if (Zend_Validate::is($value,
  223. 'OtherValidator',
  224. array('min' => 1, 'max' => 12)) {
  225. // Yes, $value is ok
  226. }
  227. ]]></programlisting>
  228. <para>
  229. For your convenience there are following methods which allow the handling of
  230. namespaces:
  231. </para>
  232. <itemizedlist>
  233. <listitem>
  234. <para>
  235. <emphasis><methodname>Zend_Validate::getDefaultNamespaces()</methodname></emphasis>:
  236. Returns all set default namespaces as array.
  237. </para>
  238. </listitem>
  239. <listitem>
  240. <para>
  241. <emphasis><methodname>Zend_Validate::setDefaultNamespaces()</methodname></emphasis>:
  242. Sets new default namespaces and overrides any previous set. It accepts
  243. either a string for a single namespace of an array for multiple namespaces.
  244. </para>
  245. </listitem>
  246. <listitem>
  247. <para>
  248. <emphasis><methodname>Zend_Validate::addDefaultNamespaces()</methodname></emphasis>:
  249. Adds additional namespaces to already set ones. It accepts either a string
  250. for a single namespace of an array for multiple namespaces.
  251. </para>
  252. </listitem>
  253. <listitem>
  254. <para>
  255. <emphasis><methodname>Zend_Validate::hasDefaultNamespaces()</methodname></emphasis>:
  256. Returns <constant>TRUE</constant> when one or more default namespaces are
  257. set, and <constant>FALSE</constant> when no default namespaces are set.
  258. </para>
  259. </listitem>
  260. </itemizedlist>
  261. </sect3>
  262. </sect2>
  263. <sect2 id="zend.validate.introduction.translation">
  264. <title>Translating messages</title>
  265. <para>
  266. Validate classes provide a <methodname>setTranslator()</methodname> method with
  267. which you can specify a instance of <classname>Zend_Translate</classname> which
  268. will translate the messages in case of a validation failure. The
  269. <methodname>getTranslator()</methodname> method returns the set translator instance.
  270. </para>
  271. <programlisting language="php"><![CDATA[
  272. $validator = new Zend_Validate_StringLength(array('min' => 8, 'max' => 12));
  273. $translate = new Zend_Translate(
  274. array(
  275. 'adapter' => 'array',
  276. 'content' => array(
  277. Zend_Validate_StringLength::TOO_SHORT => 'Translated \'%value%\''
  278. ),
  279. 'locale' => 'en'
  280. )
  281. );
  282. $validator->setTranslator($translate);
  283. ]]></programlisting>
  284. <para>
  285. With the static <methodname>setDefaultTranslator()</methodname> method you can set
  286. a instance of <classname>Zend_Translate</classname> which will be used for all
  287. validation classes, and can be retrieved with
  288. <methodname>getDefaultTranslator()</methodname>. This prevents you from setting a
  289. translator manually for all validator classes, and simplifies your code.
  290. </para>
  291. <programlisting language="php"><![CDATA[
  292. $translate = new Zend_Translate(
  293. array(
  294. 'adapter' => 'array',
  295. 'content' => array(
  296. Zend_Validate_StringLength::TOO_SHORT => 'Translated \'%value%\''
  297. ),
  298. 'locale' => 'en'
  299. )
  300. );
  301. Zend_Validate::setDefaultTranslator($translate);
  302. ]]></programlisting>
  303. <note>
  304. <para>
  305. When you have set an application wide locale within your registry, then this
  306. locale will be used as default translator.
  307. </para>
  308. </note>
  309. <para>
  310. Sometimes it is necessary to disable the translator within a validator.
  311. To archive this you can use the <methodname>setDisableTranslator()</methodname> method,
  312. which accepts a boolean parameter, and <methodname>translatorIsDisabled()</methodname>
  313. to get the set value.
  314. </para>
  315. <programlisting language="php"><![CDATA[
  316. $validator = new Zend_Validate_StringLength(array('min' => 8, 'max' => 12));
  317. if (!$validator->isTranslatorDisabled()) {
  318. $validator->setDisableTranslator();
  319. }
  320. ]]></programlisting>
  321. <para>
  322. It is also possible to use a translator instead of setting own messages with
  323. <methodname>setMessage()</methodname>. But doing so, you should keep in mind, that the
  324. translator works also on messages you set your own.
  325. </para>
  326. </sect2>
  327. </sect1>
  328. <!--
  329. vim:se ts=4 sw=4 et:
  330. -->