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

Form validation where one filed depends on another filed value?

Is there any way that I can make something like:

    pseude code
    if element value == "some value"
        element2 is required ....

I have

    $refund_method = new Select("method",array(
            ""=>"Select payment method",
            "virtual terminal" => "Virtual terminal",
            "cash" => "Cash",
            "agent" => "Agent",
            "invoice" => "Invoice",
         ),
            array(
               'class'=>"form-control"
          )
        );

        $ref = new Text('ref', array(
        'placeholder' => 'Ref',
        'class'=>"form-control"
    ));

This is not working:

    if($refund_method->getValue() == "virtual terminal" || $refund_method->getValue() == "agent" || $refund_method->getValue() == "invoice"){
        $ref->addValidator(new PresenceOf(array(
            'message' => 'Ref must be present'
        )));
    }

becasue $refund_method->getValue() is null.



7.8k
Accepted
answer

I was able to solve this with:

protected $ref;

public function initialize{

    $ref = new Text('ref', array(
        'placeholder' => 'Ref',
        'class'=>"form-control"
    ));

    $this->ref = $ref;

    ///other fileds here

}

public function beforeValidation(){

    $forvalues = array("virtual terminal","agent","invoice");
    if(in_array($this->request->getPost("method"), $forvalues)) {
        $this->ref->addValidator(new PresenceOf(array(
            'message' => 'Ref must be present'
        )));
    }
}

Phalcon rocks!



26.3k
edited Aug '14

Hi! Another way is to define your own validator for this and add it to the form. Example of a validator which communicates with other fields and generate error messages based on values of other field is this one:

Phalcon\Validation\Validator\Confirmation. Look at https://docs.phalcon.io/en/latest/api/Phalcon_Validation_Validator_Confirmation.html.

So what I suggest you is to create your own validator, e.g. RefValidator. You will simply add it to each Ref field in all your forms. The validator will do all the work, it will check values of the targeted field and generate messages if needed.

So the usage of your new validator will look like this:


public function initialize{

    $ref = new Text('ref', array(
        'placeholder' => 'Ref',
        'class'=>"form-control"
    ));

    $ref->addValidator(new RefValidator(array(
        'message' => 'Ref must be present',
        `with` => 'method',
    )));

    $this->ref = $ref;

    ///other fileds here

}

And that's all, you will do NOTHING else. I think it is very simple to read such code, and to maintain it later.

So the last issue is how to create your own validator. Below is example of my StrictValidator. It is extension of Phalcon's Phalcon\Validation\Validator\Confirmation.


<?php
namespace Omcs\Common\Library\Validation\Validator;

use Phalcon\Validation\Validator,
    Phalcon\Validation\ValidatorInterface,
    Phalcon\Validation\Validator\Confirmation as ConfirmationValidator,
    Phalcon\Validation\Message;

class StrictConfirmation extends ConfirmationValidator implements ValidatorInterface {

    public function validate($validator,$attribute) {

        //obtain the name of the field 
        $with = $this->getOption("with");

        //obtain field value
        $with_value = $validator->getValue($with);

        $value = $validator->getValue($attribute);

        //check if the value is valid
        if($with_value === $value){
            //value is valid
            return true;//end of validation
        }

        //try to obtain message defined in a validator
        $message = $this->getOption('message');

        //if the message is not defined takes a standard one
        if (!isset($message) OR !$message) {
            $message = "The values are not the same.";
        }

        $validator->appendMessage(new Message($message, $attribute, 'StrictConfirmation'));

        return false;
    }

}

Suggest to read:

Phalcon rocks! ;)