Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What I normally do with classes and their errors is have a variable specifically for the errors. So something like:</p> <pre><code>class User { private $_ValidationErrors = array(); function validate_password($password) { $this-&gt;_ValidationErrors = array(); $valid = TRUE; if ($password == '') { $this-&gt;_ValidationErrors[] = "Please enter a value for the password"; $valid = FALSE; } elseif($_POST['pwd'] !== $_POST['pwd2']) { $this-&gt;_ValidationErrors[] = "The passwords do not match please try again"; $valid = FALSE; } return $valid; } function ValidationErrors () { if (count($this-&gt;_ValidationErrors) == 0) return FALSE; return $this-&gt;_ValidationErrors; } } </code></pre> <p>Then to use:</p> <pre><code>$user = new User(); if (!$user-&gt;validate_password('somepass')) echo implode('&lt;BR&gt;',$user-&gt;ValidationErrors ()); </code></pre> <p>EDIT: To display errors by the user something like:</p> <pre><code>&lt;?php if (isset($_POST["submit"])) { $user = new User(); $passwordErrors = (!$user-&gt;validate_password($_POST["pass1"]))?$user-&gt;ValidationErrors():array(); } ?&gt; &lt;form action="&lt;?php echo $_SERVER["php_self"]; ?&gt;" method="post"&gt; &lt;input type="text" name="pass1" value="&lt;?php echo htmlspecialchars($_POST["pass1"]); ?&gt;" /&gt;&lt;BR/&gt; &lt;?php echo ((isset($passwordErrors) &amp;&amp; count($passwordErrors) == 0)?"":implode("&lt;BR/&gt;", $passwordErrors)); ?&gt; &lt;input type="submit" name="submit" value="Register" /&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload