Changeset 5300

Show
Ignore:
Timestamp:
12/17/08 21:59:39 (7 months ago)
Author:
jwage
Message:

[1.0, 1.1] Improving error message when validation fails. (closes #1763)

Location:
branches
Files:
2 added
4 modified

Legend:

Unmodified
Added
Removed
  • branches/1.0/lib/Doctrine/Record.php

    r5264 r5300  
    454454    public function preDqlDelete($event) 
    455455    { } 
     456 
     457    /** 
     458     * Get the record error stack as a human readable string. 
     459     * Useful for outputting errors to user via web browser 
     460     * 
     461     * @return string $message 
     462     */ 
     463    public function getErrorStackAsString() 
     464    { 
     465        $errorStack = $this->getErrorStack(); 
     466 
     467        if (count($errorStack)) { 
     468            $message = sprintf("Validation failed in class %s\n\n", get_class($this)); 
     469 
     470            $message .= "  " . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " had validation error" . (count($errorStack) > 1 ?  's' : null) . ":\n\n"; 
     471            foreach ($errorStack as $field => $errors) { 
     472                $message .= "    * " . count($errors) . " validator" . (count($errors) > 1 ?  's' : null) . " failed on $field (" . implode(", ", $errors) . ")\n"; 
     473            } 
     474            return $message; 
     475        } else { 
     476            return false; 
     477        } 
     478    } 
    456479 
    457480    /** 
  • branches/1.0/lib/Doctrine/Validator/Exception.php

    r5132 r5300  
    6363 
    6464    /** 
    65      * __toString 
    66      * 
    67      * @return string 
    68      */ 
    69     public function __toString() 
    70     { 
    71  
    72         return parent::__toString(); 
    73     } 
    74  
    75     /** 
    7665     * Generate a message with all classes that have exceptions 
    7766     */ 
    7867    private function generateMessage() 
    7968    { 
    80         $message = ""; 
     69        $message = ''; 
    8170        foreach ($this->invalid as $record) { 
    82            $message .= "Validation error in class " . get_class($record) . " "; 
     71            $message .= $record->getErrorStackAsString(); 
    8372        } 
    8473        return $message; 
  • branches/1.1/lib/Doctrine/Record.php

    r5264 r5300  
    505505    public function postHydrate($event) 
    506506    { } 
     507 
     508    /** 
     509     * Get the record error stack as a human readable string. 
     510     * Useful for outputting errors to user via web browser 
     511     * 
     512     * @return string $message 
     513     */ 
     514    public function getErrorStackAsString() 
     515    { 
     516        $errorStack = $this->getErrorStack(); 
     517 
     518        if (count($errorStack)) { 
     519            $message = sprintf("Validation failed in class %s\n\n", get_class($this)); 
     520 
     521            $message .= "  " . count($errorStack) . " field" . (count($errorStack) > 1 ?  's' : null) . " had validation error" . (count($errorStack) > 1 ?  's' : null) . ":\n\n"; 
     522            foreach ($errorStack as $field => $errors) { 
     523                $message .= "    * " . count($errors) . " validator" . (count($errors) > 1 ?  's' : null) . " failed on $field (" . implode(", ", $errors) . ")\n"; 
     524            } 
     525            return $message; 
     526        } else { 
     527            return false; 
     528        } 
     529    } 
    507530 
    508531    /** 
  • branches/1.1/lib/Doctrine/Validator/Exception.php

    r5132 r5300  
    6363 
    6464    /** 
    65      * __toString 
    66      * 
    67      * @return string 
    68      */ 
    69     public function __toString() 
    70     { 
    71  
    72         return parent::__toString(); 
    73     } 
    74  
    75     /** 
    7665     * Generate a message with all classes that have exceptions 
    7766     */ 
    7867    private function generateMessage() 
    7968    { 
    80         $message = ""; 
     69        $message = ''; 
    8170        foreach ($this->invalid as $record) { 
    82            $message .= "Validation error in class " . get_class($record) . " "; 
     71            $message .= $record->getErrorStackAsString(); 
    8372        } 
    8473        return $message;