Skip to content

fix: take maxlength from $maxLength instead of $cols - #130

Merged
dakorpar merged 1 commit into
masterfrom
fix/104-maxlength
Aug 14, 2026
Merged

fix: take maxlength from $maxLength instead of $cols#130
dakorpar merged 1 commit into
masterfrom
fix/104-maxlength

Conversation

@dakorpar

Copy link
Copy Markdown
Member

Closes #104.

The bug

BootstrapContainerTrait::addText() guarded on $maxLength but then wrote $cols into the attribute:

if ($maxLength !== null) {
    $comp->setHtmlAttribute('maxlength', $cols);
}

So addText('name', 'Name', 10, 50) rendered maxlength="10" — the maximum length of the value was decided by the visual width of the input, and the $maxLength that was actually asked for never reached the HTML. Passing $maxLength without $cols rendered maxlength="".

addPassword() delegates to addText(), so it carried the same bug.

The fix

$maxLength now goes through the TextInput constructor, which is how Nette\Forms\Container::addText() applies it, and $cols keeps setting the cols attribute on its own.

Also in here

addEmail() never applied its $maxLength. It takes int $maxLength = 255 and then called addText($name, $label) with the label only, so the argument was silently dropped — same defect, same two lines of code. It now passes the value on.

This one is a visible output change: every field built with addEmail() starts rendering maxlength="255" (or whatever was passed) where it previously rendered no maxlength at all. That is what Nette\Forms\Container::addEmail() has always produced, and the alternative is leaving a documented parameter that does nothing — flagging it since it lands in rendered HTML for existing users, and this branch is aimed at the next major anyway.

The @param int|null $cols ignored / $maxLength ignored notes on addText() and addTextArea() described neither the old nor the new behaviour — both arguments have always been written to attributes — so they now say what the arguments do.

Not changed: addText() renders $cols as a cols attribute, while Nette renders it as size (cols is a <textarea> attribute, not an <input> one). That is a separate pre-existing divergence, it is not what #104 reports, and correcting it would silently rewrite the HTML of every field that passes $cols — worth its own issue if you want it aligned.

Tests

Five tests in tests/Traits/BootstrapContainerTraitTest.php covering maxlength from $maxLength alongside cols, $maxLength without $cols, $cols alone rendering no maxlength, addPassword() passing it through, and addEmail() applying both its default and an explicit value. Four of the five fail on master.

make tests (163 tests, 250 assertions), make phpstan and make cs all pass. No rendering fixture under tests/data/ changed — none of them build an email field.

🤖 Generated with Claude Code

addText() rendered maxlength="$cols" whenever $maxLength was passed, so
the maximum length of the value was decided by the visual width of the
input and $maxLength itself never reached the HTML. addPassword()
delegates to addText() and inherited the same bug.

The control now gets $maxLength through the TextInput constructor, which
is how nette/forms itself applies it, and $cols keeps setting the cols
attribute on its own.

addEmail() dropped its $maxLength too — it called addText() with the
label only — so its documented default of 255 was never applied. It now
passes the value through, matching Nette\Forms\Container::addEmail().

The "ignored" @PARAM notes on addText()/addTextArea() described neither
the old nor the new behaviour and are replaced with what the arguments
actually do.

Closes #104

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.25%. Comparing base (87fa3b3) to head (74fbec8).

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #130      +/-   ##
============================================
+ Coverage     97.05%   97.25%   +0.19%     
+ Complexity      309      308       -1     
============================================
  Files            25       25              
  Lines           986      984       -2     
============================================
  Hits            957      957              
+ Misses           29       27       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dakorpar
dakorpar merged commit 0a87aa2 into master Aug 14, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Variable Mismatch for Maximum Length of Content

1 participant