This is the API documentation for concrete5 version 5.6 and earlier. View Current API Documentation
 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 
<?php
/**
 * @package Helpers
 * @subpackage Validation
 * @author Andrew Embler <[email protected]>
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 */

/**
 * Helper elements for dealing with errors in Concrete
 * @package Helpers
 * @subpackage Validation
 * @author Andrew Embler <[email protected]>
 * @copyright  Copyright (c) 2003-2008 Concrete5. (http://www.concrete5.org)
 * @license    http://www.concrete5.org/license/     MIT License
 */
    defined('C5_EXECUTE') or die("Access Denied.");
    class Concrete5_Helper_Validation_Error {
    
        protected $error = array();
        public $helperAlwaysCreateNewInstance = true;

        /** 
         * Adds an error object or exception to the internal error array
         * @param Exception | string $e
         * @return void
         */
        public function add($e) {
            if ($e instanceof ValidationErrorHelper) {
                foreach($e->getList() as $errorString) {
                    $this->add($errorString);
                }
            } else if (is_object($e) && ($e instanceof Exception)) {
                $this->error[] = $e->getMessage();
            } else {
                $this->error[] = $e;
            }
        }
        
        /** 
         * Returns a list of errors in the error helper
         * @return array
         */
        public function getList() {
            return $this->error;
        }
        
        /** 
         * Returns whether or not this error helper has more than one error registered within it.
         * @return bool
         */
        public function has() {
            return (count($this->error) > 0);
        }

        /** 
         * Outputs the HTML of an error list, with the correct style attributes/classes. This is a convenience method.
         */
        public function output() {
            if ($this->has()) {
                print '<ul class="ccm-error">';
                foreach($this->getList() as $error) {
                    print '<li>' . $error . '</li>';
                }
                print '</ul>';
            }
        }

        /** 
         * Outputs the the error as a JSON object.
         */
        public function outputJSON() {
            if ($this->has()) {
                $js = Loader::helper('json');
                $obj = new stdClass;
                $obj->error = true;
                $obj->messages = array();
                foreach($this->getList() as $error) {
                    $obj->messages[] = $error;
                }
                print $js->encode($obj);
            }
        }

    }
    
?>