problems with custom validators

about a year ago

alnux
Member | 106
+
0
-

i trying to make a custom validator of two ways

1 way
writing the method ‘divisibilityValidator’ on the same class, it apears to work on server side but when i try to add netteforms.js validator does not work, becouse the generate input does not put the ‘divisibilityValidator’ validator on data-nette-rules

<?php namespace App\Front\Users\Empresa\Forms\Src;

use Nette\Application\UI\Form;
use App\Common\Forms\BaseForm;

class EmpresaForms extends BaseForm
{
$this->form->addText('observaciones', 'Observaciones')
            ->setAttribute('rows', 4)
            ->setAttribute('placeholder', 'observaciones del producto vendido')
            ->addRule(Form::MAX_LENGTH, 'Maximo de caracteres %d',250)
 ->addRule([$this, 'divisibilityValidator'], 'First number must be %d multiple', 8);

public function divisibilityValidator($item, $arg)
    {
        return $item->value % $arg === 0;
    }

}

here is the input generated, if you saw the divisibilityValidator does not appears on data-nette-rules

<input type="text" name="observaciones[]" maxlength="250" rows="4" placeholder="observaciones del producto vendido" id="observaciones" data-nette-rules="[{&quot;op&quot;:&quot;:maxLength&quot;,&quot;msg&quot;:&quot;Maximo de caracteres 250&quot;,&quot;arg&quot;:250}]" autocomplete="off" class="form-control">

2 the other way, is creating the class MyValidators with the static class divisibilityValidator like Nette Forms custom validator example (custom-validator.php)

<?php namespace App\Front\Users\Empresa\Forms\Src;

class MyValidators
{
    static function divisibilityValidator($item, $arg)
    {
        return $item->value % $arg === 0;
    }
}

her my form class

<?php namespace App\Front\Users\Empresa\Forms\Src;

use Nette\Application\UI\Form;
use App\Common\Forms\BaseForm;
use App\Front\Users\Empresa\Forms\Src\MyValidators;
class EmpresaForms extends BaseForm
{
$this->form->addText('observaciones', 'Observaciones')
            ->setAttribute('rows', 4)
            ->setAttribute('placeholder', 'observaciones del producto vendido')
            ->addRule(Form::MAX_LENGTH, 'Maximo de caracteres %d',250)
 ->addRule('MyValidators::divisibilityValidator', 'First number must be %d multiple', 8);

But at this way tracy showme the error
**Nette\InvalidArgumentException

Unknown validator ‘MyValidators::divisibilityValidator’ for control ‘observaciones’**

i will appreciate the solution

thanks

about a year ago

duke
Member | 612
+
0
-

I think you have to use fully qualified name for the validator in addRule method. I.e.:

->addRule('App\Front\Users\Empresa\Forms\Src\MyValidators::divisibilityValidator', 'First number must be %d multiple', 8);

about a year ago

alnux
Member | 106
+
0
-

duke wrote:

I think you have to use fully qualified name for the validator in addRule method. I.e.:

->addRule('App\Front\Users\Empresa\Forms\Src\MyValidators::divisibilityValidator', 'First number must be %d multiple', 8);

but the problem is how javascript interprets it??

by example

‘MyValidators::divisibilityValidator’ on data-nette-rules

but to call it is:

<script>
    Nette.validators.MyValidators_divisibilityValidator = function(elem, args, val) {
        return val % args === 0;
    };
</script>

in my case

App\\Front\\Users\\Empresa\\Forms\\Src\\MyValidators::divisibilityValidator

how can i interpret \\ on javascript if MyValidators::divisibilityValidator is MyValidators_divisibilityValidator

thanks for help

about a year ago

David Matějka
Moderator | 5518
+
0
-

see the doc

If our PHP validation callback is a static method in a class, we create the name for JavaScript by removing all backslashes \ and by replacing the double colon by single underscore _, e.g. App\MyValidator::divisibilityValidator is converted to AppMyValidator_divisibilityValidator.

so in your case it will be

AppFrontUsersEmpresaFormsSrcMyValidators_divisibilityValidator

7 months ago

namphan1998
Member | 3
+
0
-

Im having the same problem as well.
Thank you so much for the answer.