addRule following addTextArea is ignored?

shuseisajdi
Member | 8
+
0
-

I want to verify that the string length of the textarea is greater than or equal to 1.
However, the addRule following addTextArea is ignored.
(addRule for addText etc. works!)

Forms Component:

$form->addTextArea('message')
    ->addRule($form::MinLength, 'Enter some', 1);

(For test, It was dare to remove setRequired().)

Latte:

<label for="message" n:name=message>
<span>Message:</span></label>
<textarea id="message" n:name=message></textarea>
<span class=error n:ifcontent>{inputError message}</span>

Even if textarea(‘message’) is empty, no error is displayed.

This is the only workaround I try…

$form->onSubmit[] = function (Form $form) {
    $message = $form['message']->getValue();
    if (empty($message)) {
        $form['message']->addError('Message required');
        return;
    }
};

Is there any way for addRule($form::MinLength) to work?

Rick Strafy
Nette Blogger | 81
+
+1
-

I want to verify that the string length of the textarea is greater than or equal to 1.

That's what setRequired() does, rules are not checked if input is empty, because empty value is allowed when input has no setRequired().

shuseisajdi
Member | 8
+
0
-

I may have been mistaken.
So you are saying that setRequired() is not only add the required attribute on the HTML element, but also verifying that it is empty in the server app?

Šaman
Member | 2658
+
+1
-

shuseisajdi wrote:

I may have been mistaken.
So you are saying that setRequired() is not only add the required attribute on the HTML element, but also verifying that it is empty in the server app?

Quite the opposite: setRequired() primarily defines the server validation (like all other validation rules), and additionally the default form renderer adds the required attribute to the HTML element.

Last edited by Šaman (2024-07-24 11:02)