Form validation: only one validator of the chain should check against empty/null values, the rest of the validators should not

I have this id_role input that validates dependent on value of another input internet_access. id_role's Validator Chain have one Callback validator that must check against empty/null values, others validators from this chain must check against filled values only.

I already made possible checking against empty/null values by $id_role->setContinueIfEmpty(true) but this applies to every validator in the chain. I need it to apply to only Callback validator of the chain.

This is the actual id_role input:

$id_role = new Input('id_role');
$id_role->setContinueIfEmpty(true); //this allows to check against empty/null values
$id_role->getFilterChain()
        ->attach($FilterInt);

$id_role->getValidatorChain()
        ->attach(new Validator\Callback(function($value, $context=array()){
            return isset($context['internet_access']) && $context['internet_access'] == 1 && $value === 0 ? false : true;
        }))
        ->attach(new Validator\Db\RecordExists(...);

So my problem is that Callback validator works fine but it fails on DbRecordExists because it tries to find a record that is empty. DbRecordExists must try to find a record only when id_role is actually filled.

Is there a way to do what i want in a elegant way (inside Input Filter and/or Input)?

Answers


The second parameter of ValidatorChain::attach method is $breakChainOnFailure, default value is false.

Check out docs at http://framework.zend.com/manual/2.2/en/modules/zend.validator.validator-chains.html

You should modify your code as:

$id_role->getValidatorChain()
    ->attach(
        new Validator\Callback(
            function($value, $context=array()){
                return isset($context['internet_access']) && $context['internet_access'] == 1 && $value === 0 ? false : true;
            }
        ),
        true //$breakChainOnFailure
    )
    ->attach(new Validator\Db\RecordExists(....));

Well i think there is no way of checking for empty/null values only in specific validators of the chain, because when i do $id_role->setContinueIfEmpty(true) it affects the whole Validator Chain, not just one specific validator, wich is the correct behavior.

So to accomplish what i needed, i had to put DbRecordExists Validator inside the Callback Validator and validate it manually only when values aren't empty/null:

$id_role = new Input('id_role');
$id_role->setContinueIfEmpty(true);
$id_role->getFilterChain()
        ->attach($FilterInt);

$id_role->getValidatorChain()
        ->attach(new Validator\Callback(function($value, $context=array()){
            if (isset($context['internet_access']) && $context['internet_access'] == 1 && $value === 0) {
                return false;
            }

            if ($value !== 0) {
                $dbRecordExists = new Validator\Db\RecordExists(...);

                if (!$dbRecordExists->isValid($value)) {
                    return false;   
                }
            }

            return true;
        }));

I don't know if it is the best solution but well, it worked. I hope this can be of some help to others that have the same problem.


Need Your Help

How to set a visibility of TextBlock bound to a ListBox?

windows-phone-7 textblock listboxitem

How can I set a TextBlock visibility inside a ListBox. For example