Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you should develop another class: a ValidatorChain, which takes an arbitrary amount of validators, and that aggregates the errormessages of all validators that it has tested</p> <p>For reference see the <a href="http://framework.zend.com/manual/en/zend.validate.validator_chains.html" rel="nofollow noreferrer">docs on Zend Framework's Validator Chain</a></p> <p><strong>EDIT</strong></p> <p>Now that I re-evaluate your question (thanks to Bryan M's comment); why do you want each individual Validator to have access to other Validators' error messages? I would say that collecting each individual Validators' error messages is the responsibility of an object higher in the hierarchy.</p> <p>If, however, you want individual Validators to be able to act based on context, in other words, based on what the results of other Validators are, then I suppose you could add a $context parameter to the <code>isCorrect</code> method. This could for instance accept an arbitrary amount of Validators or something similar.</p> <p>Something like:</p> <pre><code>interface ValidatorInterface { public function isCorrect( array $context ); public function getMessages(); } abstract class ValidatorContextOptions { const SHOULD_BE_PRESENT = 'shouldBePresent'; const SHOULD_NOT_BE_PRESENT = 'shouldNotBePresent'; const SHOULD_BE_VALID = 'shouldBeValid'; } class EmailValidator implements ValidatorInterface { protected $_field; protected $_contextOptions = array(); protected $_messages = array(); public function __construct( $field, array $contextOptions ) { $this-&gt;_field = $field; $this-&gt;_contextOptions = $contextOptions; } public function isCorrect( array $context = null ) { foreach( $this-&gt;_contextOptions as $field =&gt; $options ) { foreach( $options as $option ) { switch( $option ) { case ValidatorContextOptions::SHOULD_NOT_BE_PRESENT: if( isset( $context[ $field ] ) &amp;&amp; $context[ $field ] instanceof ValidatorInterface ) { $this-&gt;_messages[] = $field . ' should not be present'; return false; } break; case ValidatorContextOptions::SHOULD_BE_PRESENT: if( !isset( $context[ $field ] ) || !$context[ $field ] instanceof ValidatorInterface ) { $this-&gt;_messages[] = $field . ' should be present'; return false; } break; case ValidatorContextOptions::SHOULD_BE_VALID: if( !isset( $context[ $field ] ) || !$context[ $field ] instanceof ValidatorInterface || !$context[ $field ]-&gt;isCorrect() ) { $this-&gt;_messages[] = $field . ' should be valid'; return false; } break; } } } // some dummy function which you should replace with real validation return isAnEmailAddress( $this-&gt;_field ); } public function getMessages() { return $this-&gt;_messages; } } </code></pre> <p>Usage:</p> <pre><code>$emailValidatorContextOptions = array ( 'phone' =&gt; array( ValidatorContextOptions::SHOULD_BE_PRESENT, ValidatorContextOptions::SHOULD_BE_VALID ) ); $phoneValidator = new PhoneValidator( $phoneString ); $emailValidator = new EmailValidator( $emailString, $emailValidatorContextOptions ); if( !$emailValidator-&gt;isCorrect( array( 'phone' =&gt; $phoneValidator ) ) ) { print_r( $emailValidator-&gt;getMessages() ); } </code></pre> <p>What I've shown here, needs a lot more thinking (and I really mean A LOT), is buggy as hell and definately not bulletproof. But I hope you catch my drift of where I'm going with this.</p> <p>Moreover, where do you insert the values in your validator that need to be validated anyway?</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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