root

Changeset 701

Show
Ignore:
Timestamp:
09/18/09 00:50:58 (10 months ago)
Author:
wbond
Message:

Fixed tickets #250 and #307 - fValidationException can now take a header message and a list of messages for standard lists of validation errors and fValidation::validate() accepts a boolean parameter to return the messages instead of throwing them as an exception

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fActiveRecord.php

    r700 r701 Hide Line Numbers
    1616 * @link       http://flourishlib.com/fActiveRecord 
    1717 *  
    18  * @version    1.0.0b43 
     18 * @version    1.0.0b44 
     19 * @changes    1.0.0b44  Updated code for new fValidationException API [wb, 2009-09-18] 
    1920 * @changes    1.0.0b43  Updated code for new fRecordSet API [wb, 2009-09-16] 
    2021 * @changes    1.0.0b42  Corrected a grammar bug in ::hash() [wb, 2009-09-09] 
     
    11321133            if ($restriction_messages) { 
    11331134                throw new fValidationException( 
    1134                     sprintf( 
    1135                         "<p>%1\$s</p>\n<ul>\n<li>%2\$s</li>\n</ul>", 
    1136                         self::compose('This %s can not be deleted because:', fORM::getRecordName($class)), 
    1137                         join("</li>\n<li>", $restriction_messages) 
    1138                     ) 
     1135                    self::compose('This %s can not be deleted because:', fORM::getRecordName($class)), 
     1136                    $restriction_messages 
    11391137                ); 
    11401138            } 
     
    24192417        if (!empty($validation_messages)) { 
    24202418            throw new fValidationException( 
    2421                 sprintf( 
    2422                     "<p>%1\$s</p>\n<ul>\n<li>%2\$s</li>\n</ul>", 
    2423                     self::compose("The following problems were found:"), 
    2424                     join("</li>\n<li>", $validation_messages) 
    2425                 ) 
     2419                'The following problems were found:', 
     2420                $validation_messages 
    24262421            ); 
    24272422        } 
  • fEmail.php

    r609 r701 Hide Line Numbers
    1717 * @link       http://flourishlib.com/fEmail 
    1818 *  
    19  * @version    1.0.0b10 
     19 * @version    1.0.0b11 
     20 * @changes    1.0.0b11  Updated to use the new fValidationException API [wb, 2009-09-17] 
    2021 * @changes    1.0.0b10  Fixed a bug with sending both an HTML and a plaintext body [bb-imarc, 2009-06-18] 
    2122 * @changes    1.0.0b9   Fixed a bug where the MIME headers were not being set for all emails [wb, 2009-06-12] 
     
    12991300        if ($validation_messages) { 
    13001301            throw new fValidationException( 
    1301                 sprintf( 
    1302                     "<p>%1\$s</p>\n<ul>\n<li>%2\$s</li>\n</ul>", 
    1303                     self::compose("The email could not be sent because:"), 
    1304                     join("</li>\n<li>", $validation_messages) 
    1305                 ) 
     1302                'The email could not be sent because:', 
     1303                $validation_messages 
    13061304            );   
    13071305        } 
  • fValidation.php

    r670 r701 Hide Line Numbers
    1010 * @link       http://flourishlib.com/fValidation 
    1111 *  
    12  * @version    1.0.0b4 
     12 * @version    1.0.0b5 
     13 * @changes    1.0.0b5  Added the `$return_messages` parameter to ::validate() and updated code for new fValidationException API [wb, 2009-09-17] 
    1314 * @changes    1.0.0b4  Changed date checking from `strtotime()` to fTimestamp for better localization support [wb, 2009-06-01] 
    1415 * @changes    1.0.0b3  Updated for new fCore API [wb, 2009-02-16] 
     
    420421     * @throws fValidationException  When one of the options set for the object is violated 
    421422     *  
    422      * @return void 
    423      */ 
    424     public function validate() 
     423     * @param  boolean $return_messages  If an array of validation messages should be returned instead of an exception being thrown 
     424     * @return void|array  If $return_messages is TRUE, an array of validation messages will be returned 
     425     */ 
     426    public function validate($return_messages=FALSE) 
    425427    { 
    426428        if (!$this->email_header_fields && 
     
    442444        $this->checkURLFields($messages); 
    443445         
     446        if ($return_messages) { 
     447            return $messages; 
     448        } 
     449         
    444450        if ($messages) { 
    445451            throw new fValidationException( 
    446                 sprintf( 
    447                     "<p>%1\$s</p>\n<ul>\n<li>%2\$s</li>\n</ul>", 
    448                     self::compose("The following problems were found:"), 
    449                     join("</li>\n<li>", $messages) 
    450                 ) 
     452                'The following problems were found:', 
     453                $messages 
    451454            ); 
    452455        } 
  • fValidationException.php

    r598 r701 Hide Line Numbers
    1010 * @link       http://flourishlib.com/fValidationException 
    1111 *  
    12  * @version    1.0.0b 
    13  * @changes    1.0.0b  The initial implementation [wb, 2007-06-14] 
     12 * @version    1.0.0b2 
     13 * @changes    1.0.0b2  Added a custom ::__construct() to handle arrays of messages [wb, 2009-09-17] 
     14 * @changes    1.0.0b   The initial implementation [wb, 2007-06-14] 
    1415 */ 
    1516class fValidationException extends fExpectedException 
     
    5960        self::$field_format = $format;       
    6061    } 
     62     
     63     
     64    /** 
     65     * Sets the message for the exception, allowing for custom formatting beyond fException 
     66     *  
     67     * If this method receives exactly two parameters, a string and an array, 
     68     * the string will be used as a message in a HTML `<p>` tag and the array 
     69     * will be turned into an unorder list `<ul>` tag with each element in the 
     70     * array being an `<li>` tag. It is possible to pass an optional exception 
     71     * code as a third parameter. 
     72     *  
     73     * The following PHP: 
     74     *  
     75     * {{{ 
     76     * #!php 
     77     * throw new fValidationException( 
     78     *     'The following problems were found:', 
     79     *     array( 
     80     *         'Please provide your name', 
     81     *         'Please provide your email address' 
     82     *     ) 
     83     * ); 
     84     * }}} 
     85     *  
     86     * Would create the message: 
     87     *  
     88     * {{{ 
     89     * #!text/html 
     90     * <p>The following problems were found:</p> 
     91     * <ul> 
     92     *     <li>Please provide your name</li> 
     93     *     <li>Please provide your email address</li> 
     94     * </ul> 
     95     * }}} 
     96     *  
     97     * If the parameters are anything else, they will be passed to 
     98     * fException::__construct(). 
     99     *  
     100     * @param  string $message       The beginning message for the exception. This will be placed in a `<p>` tag. 
     101     * @param  array  $sub_messages  An array of strings to place in a `<ul>` tag 
     102     * @param  mixed  $code          The optional exception code 
     103     * @return fException 
     104     */ 
     105    public function __construct($message='') 
     106    { 
     107        $params = func_get_args(); 
     108         
     109        if ((count($params) == 2 || count($params) == 3) && is_string($params[0]) && is_array($params[1])) { 
     110            $message = sprintf( 
     111                "<p>%1\$s</p>\n<ul>\n<li>%2\$s</li>\n</ul>", 
     112                self::compose($params[0]), 
     113                join("</li>\n<li>", $params[1]) 
     114            ); 
     115            $params = array_merge( 
     116                // This escapes % signs since fException is going to look for sprintf formatting codes 
     117                array(str_replace('%', '%%', $message)), 
     118                // This grabs the exception code if one is defined 
     119                array_slice($params, 2) 
     120            );       
     121        } 
     122         
     123        call_user_func_array( 
     124            array($this, 'fException::__construct'), 
     125            $params 
     126        );       
     127    } 
    61128} 
    62129