Question

I am using concrete to render my site, it uses the following error system;

if (isset($error) && $error != '') {
    if ($error instanceof Exception) {
        $_error[] = $error->getMessage();
    } else if ($error instanceof ValidationErrorHelper) { 
        $_error = $error->getList();
    } else if (is_array($error)) {
        $_error = $error;
    } else if (is_string($error)) {
        $_error[] = $error;
    }
    ?>
    <?php  if ($format == 'block') { ?>
    <div class="alert-message error">
    <?php  foreach($_error as $e): ?>
        <?php  echo $e?><br/>
    <?php  endforeach; ?>
    </div>

    <?php  } else { ?>

    <ul class="ccm-error">
    <?php  foreach($_error as $e): ?>
        <li><?php  echo $e?></li>
    <?php  endforeach; ?>
    </ul>
    <?php  } ?>


<?php  } ?>

This is used to detect errors in the system, or validation errors.

My problem is that the error div is being displayed but there is no errors, e.g.:

Error DIV

I have tried dumping the variables and this is the result (in this case):

Array    object(ValidationErrorHelper)#89 (1) { ["error:protected"]=> array(0) { } }

This is the ValidationErrorHelper system:

<?php
    defined('C5_EXECUTE') or die("Access Denied.");
    class ValidationErrorHelper {

        protected $error = array();


        public function reset() {
            $this->error = array();
        }


        public function add($e) {
            if ($e instanceof ValidationErrorHelper) {
                $this->error = array_merge($e->getList(), $this->error);            
            } else if (is_object($e) && ($e instanceof Exception)) {
                $this->error[] = $e->getMessage();
            } else {
                $this->error[] = $e;
            }
        }


        public function getList() {
            return $this->error;
        }


        public function has() {
            return (count($this->error) > 0);
        }


        public function output() {
            if ($this->has()) {
                print '<ul class="ccm-error">';
                foreach($this->getList() as $error) {
                    print '<li>' . $error . '</li>';
                }
                print '</ul>';
            }
        }
    }

?>

Is there any way to remove this? or diagnose it?

Was it helpful?

Solution

Sometimes the error object is set, but doesn't have any errors in it. If you look at the very top "if" statement, you'll see this:

if (isset($error) && $error != '') {

So what's happening is the $error variable is an object or an array and has been set by the system, but it doesn't have any errors in it. But your code is always outputting the container <div> as long as the $error variable is set (or not an empty string). What you need to do is add an additional check on the $_error variable (the one that is created in the first dozen or so lines of the code) to see if it's empty or not.

Try this:

if (isset($error) && $error != '') {
    if ($error instanceof Exception) {
        $_error[] = $error->getMessage();
    } else if ($error instanceof ValidationErrorHelper) { 
        $_error = $error->getList();
    } else if (is_array($error)) {
        $_error = $error;
    } else if (is_string($error)) {
        $_error[] = $error;
    }

    if (!empty($_error)) {
        if ($format == 'block') { ?>
            <div class="alert-message error">
            <?php  foreach($_error as $e): ?>
                <?php  echo $e?><br/>
            <?php  endforeach; ?>
            </div>
        <?php  } else { ?>
            <ul class="ccm-error">
            <?php  foreach($_error as $e): ?>
                <li><?php  echo $e?></li>
            <?php  endforeach; ?>
            </ul>
        <?php }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top