Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation Class with fluent interface
    primarykey
    data
    text
    <p>I'm currently struggling with some form validation. I'm working with the class below, which is intended to be a <em>fluent interface</em>.</p> <pre><code>class Validator implements ValidatorInterface { protected $_count_validators = 0; protected $_validators; protected $errorMsg; public function __construct($errorMsg = '') { $this-&gt;errorMsg = $errorMsg; } public function addValidator(ValidatorInterface $validator) { $this-&gt;_count_validators++; $this-&gt;_validators[] = $validator; return $this; } public function validate($value) { foreach($this-&gt;_validators as $validator) { if ($validator-&gt;validate($value) === false) { return false; } } return true; } public function getError() { return $this-&gt;errorMsg; } } </code></pre> <p>It actually works 75 % - and I can add validators like this:</p> <pre><code>$postalcodeValidator = new \Framework\Formular\Validator\Validator(); $validatePostalcode= $postalcodeValidator-&gt;addValidator(new \Framework\Formular\Validator\NotEmpty) -&gt;addValidator(new \Framework\Formular\IsNumeric); $cityValidator = new \Framework\Formular\Validator\Validator(); $validateCity = $lastnameValidator-&gt;addValidator(new \Framework\Formular\Validator\NotEmpty); </code></pre> <p>Now I can just write:</p> <pre><code>$result = $postalcodeValidator-&gt;validate('00000'); - or - $result = $cityValidator-&gt;validate('London'); </code></pre> <p>And I will have a boolean. My problem is, that I need to make it easy to set some errors. In the above example - if I just added a getErrors()-function in the class - I had to get the errors for every new instantiation of the class. I want to make a function for getting all errors.</p> <p>Can you help me on a solution for that? </p> <p>Thanks in advance,</p> <p>denlau</p>
    singulars
    1. This table or related slice is empty.
    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