Development

Changeset 10439

You must first sign up to be able to contribute.

Changeset 10439

Show
Ignore:
Timestamp:
07/23/08 14:36:55 (5 years ago)
Author:
nicolas
Message:

[1.1] fixed exception cannot be thrown in the sfFormField::__toString method

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/filter/sfRenderingFilter.class.php

    r9944 r10439  
    4141    $response = $this->context->getResponse(); 
    4242 
    43     // hack to rethrow sfForm __toString() exception (see sfForm
     43    // hack to rethrow sfForm and|or sfFormField __toString() exceptions (see sfForm and sfFormField
    4444    if (sfForm::hasToStringException()) 
    4545    { 
    4646      throw sfForm::getToStringException(); 
     47    } 
     48    else if (sfFormField::hasToStringException()) 
     49    { 
     50      throw sfFormField::getToStringException(); 
    4751    } 
    4852 
  • branches/1.1/lib/form/sfFormField.class.php

    r9238 r10439  
    1919class sfFormField 
    2020{ 
     21  protected static 
     22    $toStringException = null; 
     23   
    2124  protected 
    2225    $widget = null, 
     
    5154  public function __toString() 
    5255  { 
    53     return $this->render(); 
     56    try 
     57    { 
     58      return $this->render(); 
     59    } 
     60    catch (Exception $e) 
     61    { 
     62      self::setToStringException($e); 
     63 
     64      // we return a simple Exception message in case the form framework is used out of symfony. 
     65      return 'Exception: '.$e->getMessage(); 
     66    } 
     67  } 
     68   
     69  /** 
     70   * Returns true if a form thrown an exception in the __toString() method 
     71   * 
     72   * This is a hack needed because PHP does not allow to throw exceptions in __toString() magic method. 
     73   * 
     74   * @return boolean 
     75   */ 
     76  static public function hasToStringException() 
     77  { 
     78    return !is_null(self::$toStringException); 
     79  } 
     80 
     81  /** 
     82   * Gets the exception if one was thrown in the __toString() method. 
     83   * 
     84   * This is a hack needed because PHP does not allow to throw exceptions in __toString() magic method. 
     85   * 
     86   * @return Exception 
     87   */ 
     88  static public function getToStringException() 
     89  { 
     90    return self::$toStringException; 
     91  } 
     92   
     93  /** 
     94   * Sets an exception thrown by the __toString() method. 
     95   * 
     96   * This is a hack needed because PHP does not allow to throw exceptions in __toString() magic method. 
     97   * 
     98   * @param Exception $e The exception thrown by __toString() 
     99   */ 
     100  static public function setToStringException(Exception $e) 
     101  { 
     102    if (is_null(self::$toStringException)) 
     103    { 
     104      self::$toStringException = $e; 
     105    } 
    54106  } 
    55107