| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect2 id="zend.validate.set.notempty">
- <title>NotEmpty</title>
- <para>
- This validator allows you to validate if a given value is not empty. This is often useful
- when working with form elements or other user input, where you can use it to ensure required
- elements have values associated with them.
- </para>
- <sect3 id="zend.validate.set.notempty.options">
- <title>Supported options for Zend_Validate_NotEmpty</title>
- <para>
- The following options are supported for <classname>Zend_Validate_NotEmpty</classname>:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <emphasis><property>type</property></emphasis>: Sets the type of validation
- which will be processed. For details take a look into <link
- linkend="zend.validate.set.notempty.types">this section</link>.
- </para>
- </listitem>
- </itemizedlist>
- </sect3>
- <sect3 id="zend.validate.set.notempty.default">
- <title>Default behaviour for Zend_Validate_NotEmpty</title>
- <para>
- By default, this validator works differently than you would expect when you've worked
- with <acronym>PHP</acronym>'s <methodname>empty()</methodname> function. In
- particular, this validator will evaluate both the integer <emphasis>0</emphasis> and
- string '<emphasis>0</emphasis>' as empty.
- </para>
- <programlisting language="php"><![CDATA[
- $valid = new Zend_Validate_NotEmpty();
- $value = '';
- $result = $valid->isValid($value);
- // returns false
- ]]></programlisting>
- <note>
- <title>Default behaviour differs from PHP</title>
- <para>
- Without providing configuration, <classname>Zend_Validate_NotEmpty</classname>'s
- behaviour differs from <acronym>PHP</acronym>.
- </para>
- </note>
- </sect3>
- <sect3 id="zend.validate.set.notempty.types">
- <title>Changing behaviour for Zend_Validate_NotEmpty</title>
- <para>
- Some projects have differing opinions of what is considered an "empty" value: a string
- with only whitespace might be considered empty, or <emphasis>0</emphasis> may be
- considered non-empty (particularly for boolean sequences). To accomodate differing
- needs, <classname>Zend_Validate_NotEmpty</classname> allows you to configure which types
- should be validated as empty and which not.
- </para>
- <para>
- The following types can be handled:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <emphasis>boolean</emphasis>: Returns <constant>FALSE</constant> when the
- boolean value is <constant>FALSE</constant>.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>integer</emphasis>: Returns <constant>FALSE</constant> when an integer
- <emphasis>0</emphasis> value is given. Per default this validation is not
- activated and returns <constant>TRUE</constant> on any integer values.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>float</emphasis>: Returns <constant>FALSE</constant> when an float
- <emphasis>0.0</emphasis> value is given. Per default this validation is not
- activated and returns <constant>TRUE</constant> on any float values.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>string</emphasis>: Returns <constant>FALSE</constant> when an empty
- string <emphasis>''</emphasis> is given.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>zero</emphasis>: Returns <constant>FALSE</constant> when the single
- character zero (<emphasis>'0'</emphasis>) is given.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>empty_array</emphasis>: Returns <constant>FALSE</constant> when an
- empty <emphasis>array</emphasis> is given.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>null</emphasis>: Returns <constant>FALSE</constant> when an
- <constant>NULL</constant> value is given.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>php</emphasis>: Returns <constant>FALSE</constant> on the same reasons
- where <acronym>PHP</acronym> method <methodname>empty()</methodname> would
- return <constant>TRUE</constant>.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>space</emphasis>: Returns <constant>FALSE</constant> when an string
- is given which contains only whitespaces.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>object</emphasis>: Returns <constant>TRUE</constant>.
- <constant>FALSE</constant> will be returned when <property>object</property> is
- not allowed but an object is given.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>object_string</emphasis>: Returns <constant>FALSE</constant> when an
- object is given and it's <methodname>__toString()</methodname> method returns an
- empty string.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>object_count</emphasis>: Returns <constant>FALSE</constant> when an
- object is given, it has an <classname>Countable</classname> interface and it's
- count is 0.
- </para>
- </listitem>
- <listitem>
- <para>
- <emphasis>all</emphasis>: Returns <constant>FALSE</constant> on all above types.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- All other given values will return <constant>TRUE</constant> per default.
- </para>
- <para>
- There are several ways to select which of the above types are validated. You can give
- one or multiple types and add them, you can give an array, you can use constants, or you
- can give a textual string. See the following examples:
- </para>
- <programlisting language="php"><![CDATA[
- // Returns false on 0
- $validator = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::INTEGER);
- // Returns false on 0 or '0'
- $validator = new Zend_Validate_NotEmpty(
- Zend_Validate_NotEmpty::INTEGER + Zend_NotEmpty::ZERO
- );
- // Returns false on 0 or '0'
- $validator = new Zend_Validate_NotEmpty(array(
- Zend_Validate_NotEmpty::INTEGER,
- Zend_Validate_NotEmpty::ZERO
- ));
- // Returns false on 0 or '0'
- $validator = new Zend_Validate_NotEmpty(array(
- 'integer',
- 'zero',
- ));
- ]]></programlisting>
- <para>
- You can also provide an instance of <classname>Zend_Config</classname> to set the
- desired types. To set types after instantiation, use the
- <methodname>setType()</methodname> method.
- </para>
- </sect3>
- </sect2>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|