Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A simple way is to implement a static member in an abstract validator class. All your concrete Validator extends this, and will add automaticly errors to this member. Finally you can get this static member with one call. But this is an anti-pattern and you have to reset this member after getting errors.</p> <p>A better practice is to use the composite pattern. A class where you can add one or more elements with assigned validators. This composite class will execute all validators on your assigned elements and collect all error messages from each validator. Then you can retrieve all collected error messages from your composite, with one call.</p> <p>For more information about composite pattern see <a href="http://en.wikipedia.org/wiki/Composite_pattern" rel="nofollow">here on wiki</a></p> <p>Here an example..</p> <pre><code>$elementA = new ElementA; // implements Validable $elementA-&gt;addValidator(new ValidatorA)-&gt;addValidator(new ValidatorB); $elementB = new ElementB; // implements Validable $elementB-&gt;addValidator(new ValidatorC); $elementA-&gt;setValue('any_posted_value_to_validate'); $elementB-&gt;setValue('another_any_posted_value_to_validate'); $composite = new Composite; // implements Validable $composite-&gt;addElement($elementA)-&gt;addElement($elementB); if (!composite-&gt;isValid()) { // will execute all validators on all elements $errorMessages = $composite-&gt;getErrors(); } </code></pre> <p>Within your composite..</p> <pre><code>public function isValid() { $isValid = true; foreach ($this-&gt;elements as $element) { if (!$element-&gt;isValid()) { // will execute all assigned validators to this element $this-&gt;addErrors($element-&gt;getErrors()); $isValid = false; } } return $isValid; } </code></pre> <p>The Validable Interface</p> <pre><code>interface Validable { public function isValid(); public function getErrors(); } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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