addCondition – own validation

Notice: This thread is very old.
Roman Kubica
Member | 2
+
0
-

Hi,

I have this form. If I use check box then toggle works. If I use my condition, nothing happens. I have also try to add third parameter as TRUE, but same result: no toggling. There is no error in debug mode.

Would anyone be able to help me please what am I doing wrong? Am I passing parameter country incorrectly? I used this tutorial "":https://doc.nette.org/…s/validation

$form->addSelect('country', 'Country:')
    ->setPrompt('Choose nationality')
    ->setItems($this->countries, FALSE)
    ->addCondition(TaxFormRules::COUNTRY,$form['country']->value)
    ->toggle('frm-taxReturnZivnoForm-healthInsurance-pair');

$form->addCheckbox('mail_me')
    ->addCondition($form::EQUAL, TRUE)
    ->toggle('frm-taxReturnZivnoForm-healthInsurance-pair');
class TaxFormRules
{

    const COUNTRY = 'App\Model\TaxFormRules::validateCountry';


    public static function validateCountry(IControl $control, $country)
    {
        // validace, zda se jedné o e-mail z domény $domain
        if (substr($country, 0, 1) == "A") {
            return true;
        }else {
            return false;
        }
    }
}
$this->countries = array(
            'AF' => 'Afghanistan',
            'AL' => 'Albania',
            'DZ' => 'Algeria',
            'YE' => 'Yemen',
            'ZM' => 'Zambia',
            'ZW' => 'Zimbabwe',
            'AX' => 'Åland Islands',
        );
David Matějka
Moderator | 6445
+
0
-

To make toggle working, you have to write client-side validation in js:

Nette.validators.AppModelTaxFormRules_validateCountry = function (control) {
	...
};

btw, remove $form['country']->value in the addCondition. it cannot work.