We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

how validate on field when the other field value is eq 1

I wan't Validate filed irritate_history only when the field have_irritate's value eq 1. and now i defind DependValidator,

 public function validate( Validation $validator, $attribute )
    {
        $data = $validator ->getData();
        $depend = $this ->getOption( 'depend' )[0];
        $other_value = $data[$depend[0]];
        $depend_bool = false;

        $operator = $depend[1];
        $depend_value = $depend[2];
        switch ( $operator ){
            case 'eq':
                $depend_bool = $depend_value == $other_value;
                break;

        }
        if( $depend_bool === true ){
            $native_ruel = $this ->getOption( 'nativeRule' );
            unset( $native_ruel[1], $native_ruel[2] );
            $native_ruel = array_values( $native_ruel );
            $validator ->preRule( $native_ruel );

        }
        return true;

    }
public function preRule( $rule ){
        $rule = ['member_name', 'McstringValidator',  'member_name:成员名长度必须大于1|:member_name成员名长度必须小于等于50', [ 1, 50 ], 1 ];
        $types = self::$types;
        $field = $rule[0];
        $type = empty($rule[1]) ? 'regex' : $rule[1];

        $options = $this ->parseRule( $type, $rule );
        //$this->add( $field, new $types[$type]['validator']($options));
        //echo $types[$type]['validator'];exit;
        //$this->add( $field, new $types[$type]['validator']($options));
        $r = $this ->preChecking( $field, new $types[$type]['validator']($options) );
        return $r;
        var_dump( $m );exit;
        var_dump( $types[$type]['validator'] );exit;
    }

but the function preChecking is always return false. this solution way is right? and preChecking should how use

your validator always return true



387

yes,but preChecking function always return false,I don't know how and when use preChecking

your validator always return true

Where are you doing the validation? Is this in the model?

edited Jul '19

You can use Phalcon\Validation\Validator\Callback you can simply return any other validator you want to use if value is what you want, and otherwise true(so it's valid) for this. https://docs.phalcon.io/3.4/en/validation#callback-validator



387

in model

Where are you doing the validation? Is this in the model?



387

yes,I have use callback meet my need. but I aslo wan't know how use preChecking .

You can use Phalcon\Validation\Validator\Callback you can simply return any other validator you want to use if value is what you want, and otherwise true(so it's valid) for this. https://docs.phalcon.io/3.4/en/validation#callback-validator

It's not clear to me why you're overriding validate instead of defining your own validation method - which is intended to be defined to enable custom validation. In that method, only add validation for irritate_history if have_irritates is 1:

public function validation(){
    $validator = new Validation();

    if($this->have_irritates == 1){
        // Add the validator for `irritate_history`
        // $validator->add...
    }

    return $this->validate($validator);
}

https://docs.phalcon.io/3.4/en/db-models-validation