How to use the IS_NOT_IN rule?

2 years ago

netteman
Member | 68
+
0
-

Hi

I want to use JavaScript to check that some values are not entered.

If I leave the input empty, JavaScripts warns me but the array of “banned” values is ignored and I can still submit the form even if the values x or y are entered

protected function createComponentUrlForm()
{
    $url[] = "x";
    $url[] = "y";

    $form = new Form;

    $form->addText('url', 'URL:')
            ->setRequired('Enter URL')
            ->addRule(Form::IS_NOT_IN, $url);

    $form->addSubmit('save', 'Save');
    $form->onSuccess[] = [$this, 'urlFormSucceeded'];
    return $form;
}

Could you tell me how to use the IS_NOT_IN feature? Thx :)

2 years ago

Darkling
Member | 34
+
+3
-

Hi,

method addRule has got three parameters, $validator, $errorMessage and $args. So in your case you should use:

$form->addText('url', 'URL:')
    ->setRequired('Enter URL')
    ->addRule(Form::IS_NOT_IN, 'Please enter correct value...', $url);

Last edited by Darkling (2017-09-09 11:13)

2 years ago

netteman
Member | 68
+
0
-

Thanks!