web/lib/Zend/Dojo/Form/Element/ValidationTextBox.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Dojo
       
    17  * @subpackage Form_Element
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  */
       
    21 
       
    22 /** Zend_Dojo_Form_Element_TextBox */
       
    23 require_once 'Zend/Dojo/Form/Element/TextBox.php';
       
    24 
       
    25 /**
       
    26  * ValidationTextBox dijit
       
    27  *
       
    28  * @uses       Zend_Dojo_Form_Element_TextBox
       
    29  * @package    Zend_Dojo
       
    30  * @subpackage Form_Element
       
    31  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    33  * @version    $Id: ValidationTextBox.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    34  */
       
    35 class Zend_Dojo_Form_Element_ValidationTextBox extends Zend_Dojo_Form_Element_TextBox
       
    36 {
       
    37     /**
       
    38      * Use ValidationTextBox dijit view helper
       
    39      * @var string
       
    40      */
       
    41     public $helper = 'ValidationTextBox';
       
    42 
       
    43     /**
       
    44      * Set invalidMessage
       
    45      *
       
    46      * @param  string $message
       
    47      * @return Zend_Dojo_Form_Element_ValidationTextBox
       
    48      */
       
    49     public function setInvalidMessage($message)
       
    50     {
       
    51         $this->setDijitParam('invalidMessage', (string) $message);
       
    52         return $this;
       
    53     }
       
    54 
       
    55     /**
       
    56      * Retrieve invalidMessage
       
    57      *
       
    58      * @return string|null
       
    59      */
       
    60     public function getInvalidMessage()
       
    61     {
       
    62         return $this->getDijitParam('invalidMessage');
       
    63     }
       
    64 
       
    65     /**
       
    66      * Set promptMessage
       
    67      *
       
    68      * @param  string $message
       
    69      * @return Zend_Dojo_Form_Element_ValidationTextBox
       
    70      */
       
    71     public function setPromptMessage($message)
       
    72     {
       
    73         $this->setDijitParam('promptMessage', (string) $message);
       
    74         return $this;
       
    75     }
       
    76 
       
    77     /**
       
    78      * Retrieve promptMessage
       
    79      *
       
    80      * @return string|null
       
    81      */
       
    82     public function getPromptMessage()
       
    83     {
       
    84         return $this->getDijitParam('promptMessage');
       
    85     }
       
    86 
       
    87     /**
       
    88      * Set regExp
       
    89      *
       
    90      * @param  string $regexp
       
    91      * @return Zend_Dojo_Form_Element_ValidationTextBox
       
    92      */
       
    93     public function setRegExp($regexp)
       
    94     {
       
    95         $this->setDijitParam('regExp', (string) $regexp);
       
    96         return $this;
       
    97     }
       
    98 
       
    99     /**
       
   100      * Retrieve regExp
       
   101      *
       
   102      * @return string|null
       
   103      */
       
   104     public function getRegExp()
       
   105     {
       
   106         return $this->getDijitParam('regExp');
       
   107     }
       
   108 
       
   109     /**
       
   110      * Set an individual constraint
       
   111      *
       
   112      * @param  string $key
       
   113      * @param  mixed $value
       
   114      * @return Zend_Dojo_Form_Element_ValidationTextBox
       
   115      */
       
   116     public function setConstraint($key, $value)
       
   117     {
       
   118         $constraints = $this->getConstraints();
       
   119         $constraints[(string) $key] = $value;
       
   120         $this->setConstraints($constraints);
       
   121         return $this;
       
   122     }
       
   123 
       
   124     /**
       
   125      * Set validation constraints
       
   126      *
       
   127      * Refer to Dojo dijit.form.ValidationTextBox documentation for valid
       
   128      * structure.
       
   129      *
       
   130      * @param  array $constraints
       
   131      * @return Zend_Dojo_Form_Element_ValidationTextBox
       
   132      */
       
   133     public function setConstraints(array $constraints)
       
   134     {
       
   135         array_walk_recursive($constraints, array($this, '_castBoolToString'));
       
   136         $this->setDijitParam('constraints', $constraints);
       
   137         return $this;
       
   138     }
       
   139 
       
   140     /**
       
   141      * Is the given constraint set?
       
   142      *
       
   143      * @param  string $key
       
   144      * @return bool
       
   145      */
       
   146     public function hasConstraint($key)
       
   147     {
       
   148         $constraints = $this->getConstraints();
       
   149         return array_key_exists((string)$key, $constraints);
       
   150     }
       
   151 
       
   152     /**
       
   153      * Get an individual constraint
       
   154      *
       
   155      * @param  string $key
       
   156      * @return mixed
       
   157      */
       
   158     public function getConstraint($key)
       
   159     {
       
   160         $key = (string) $key;
       
   161         if (!$this->hasConstraint($key)) {
       
   162             return null;
       
   163         }
       
   164         return $this->dijitParams['constraints'][$key];
       
   165     }
       
   166 
       
   167     /**
       
   168      * Get constraints
       
   169      *
       
   170      * @return array
       
   171      */
       
   172     public function getConstraints()
       
   173     {
       
   174         if ($this->hasDijitParam('constraints')) {
       
   175             return $this->getDijitParam('constraints');
       
   176         }
       
   177         return array();
       
   178     }
       
   179 
       
   180     /**
       
   181      * Remove a single constraint
       
   182      *
       
   183      * @param  string $key
       
   184      * @return Zend_Dojo_Form_Element_ValidationTextBox
       
   185      */
       
   186     public function removeConstraint($key)
       
   187     {
       
   188         $key = (string) $key;
       
   189         if ($this->hasConstraint($key)) {
       
   190             unset($this->dijitParams['constraints'][$key]);
       
   191         }
       
   192         return $this;
       
   193     }
       
   194 
       
   195     /**
       
   196      * Clear all constraints
       
   197      *
       
   198      * @return Zend_Dojo_Form_Element_ValidationTextBox
       
   199      */
       
   200     public function clearConstraints()
       
   201     {
       
   202         return $this->removeDijitParam('constraints');
       
   203     }
       
   204 
       
   205     /**
       
   206      * Cast a boolean value to a string
       
   207      *
       
   208      * @param  mixed $item
       
   209      * @param  string $key
       
   210      * @return void
       
   211      */
       
   212     protected function _castBoolToString(&$item, $key)
       
   213     {
       
   214         if (is_bool($item)) {
       
   215             $item = ($item) ? 'true' : 'false';
       
   216         }
       
   217     }
       
   218 }