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 to get data for Custom Validator?

below is code for my Custom Validator to see Atleast one option is selected in drop down menu....but when i try get the value of $attribute i am getting null Value but whereas the $validator has the value (of selected values)...Can anyone help me?

            <?php

use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;
use Phalcon\Validation\Message;

class MultiSelectValidator extends Validator implements ValidatorInterface {

public function __construct($options = null) {
    parent::__construct($options);
    $this->setOption("cancelOnFail", true);
}

public function validate(Phalcon\Validation $validator, $attribute) {

    $array = $validator->getValue($attribute);//getting empty value
    var_dump($validator)
    if (empty($array)) {
        $message = $this->getOption('message');
        if (!$message) {
            $message = "Select Atleast One Wallet";
        }
        $validator->appendMessage(new Message($message, $attribute, 'IsSelected'));
        return FALSE;
    }
    return TRUE;
}
}

I had defined the attibute as an array()....

        $corporate_wallets = new Select('wallets_subscribed[]', $wallets, array(
            'multiple' => 'multiple',
            'useEmpty' => TRUE,
            'emptyText' => 'Select Wallets',
            'emptyValue' => '',
        ));
        $corporate_wallets->addValidators(array(
            new MultiSelectValidator(array(
                'message' => 'Choose atleast one  Wallet Types',
                    ))
        ));
        $corporate_wallets->setAttribute('id', 'wallettypes');
        $corporate_wallets->setLabel('Select Wallet Types');
        $this->add($corporate_wallets);

Hi @jaikumar002 Can you try with this please and tell me, use a generic name and set the real name in attribs

$corporate_wallets = new Select('walletsElement', $wallets, array(
            'multiple' => 'multiple',
            'useEmpty' => TRUE,
            'emptyText' => 'Select Wallets',
            'emptyValue' => '',
            'name' => 'wallets_subscribed[]'
        ));

How do you validate the form? $form->isValid($this->request->getPost())???