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(....));

Need Your Help

Auto.Arima() in R Forecast package behaving erratically

r time-series forecasting jri

I'm using R with the forecast version 5.4 plugin by Rob Hyndman. It's a really nice package, but it seems to be acting oddly, predicting wildly different results for similar data. I'm pretty sure...

Json.net slow serialization and deserialization

c# .net windows-phone-7.1 json.net

I have a problem - Json.Net serializing my objects realy slow. I have some basic class: