contao icon indicating copy to clipboard operation
contao copied to clipboard

Fix missing template fields for forms

Open aschempp opened this issue 2 months ago • 15 comments

Stumbeled upon these while using Haste Form

aschempp avatar Dec 10 '25 21:12 aschempp

IMHO that's not a bug in the core. The Form class does set the attributes and novalidate parameter.

m-vo avatar Dec 10 '25 21:12 m-vo

Haste could always set these parameters for sure - except id.

fritzmg avatar Dec 10 '25 23:12 fritzmg

its a bug because the current HTML5 templates work just fine without these, but the Twig templates break it.

aschempp avatar Dec 11 '25 09:12 aschempp

No, it's not a bug only because the old template system suppressed an error?

m-vo avatar Dec 12 '25 09:12 m-vo

No, it's not a bug only because the old template system suppressed an error?

No that's not really an error within the old template system. In the old template system variables never have to be defined. It is specifically allowed to access variables that do not exist - otherwise we'd have to throw an exception within __get() for example.

fritzmg avatar Dec 12 '25 09:12 fritzmg

But not by design and simply because older PHP versions (or now not using strict types) did not complain. Or am I missing something?

m-vo avatar Dec 12 '25 10:12 m-vo

It is by design. Template::__get() returns the value in $this->arrData if set and null otherwise. There will never be an error or exception if you do $this->variableThatHasNotBeenSet.

fritzmg avatar Dec 12 '25 10:12 fritzmg

With that argumentation, you would need |default on every variable. This is just wrong.

m-vo avatar Dec 12 '25 11:12 m-vo

Just stating the status of the current PHP Template system in Contao.

fritzmg avatar Dec 12 '25 11:12 fritzmg

With that argumentation, you would need |default on every variable. This is just wrong.

yes, which will be necessary for all HTML5-to-Twig templates, otherwise people will get random errors now that they did not get with the HTML5-versions.

aschempp avatar Dec 12 '25 12:12 aschempp

…people will get random errors now…

I don’t think that is correct. The errors are not random but instead point to an error in the template or in the controller and should be fixed appropriately. As these errors are suppressed in prod I don’t see a BC issue here. And templates are not BC anyways.

I’d prefer to fix it this in Haste instead. However I’m fine with merging this PR as the added |defaults do not seem to break functionality (the affected variables can be interpreted as being optional IMO) and it improves BC for the legacy templates.

But I want to note that I’m against adding |default to any new (non-legacy) Twig templates as these undefined variable errors are actually an important feature of Twig that we should definitely not bypass everywhere.

ausi avatar Dec 12 '25 14:12 ausi

As these errors are suppressed in prod I don’t see a BC issue here

But still, it will be unexpected in dev, as the PHP template would not generate the same error.

But I want to note that I’m against adding |default to any new (non-legacy) Twig templates as these undefined variable errors are actually an important feature of Twig that we should definitely not bypass everywhere.

Of course, this is only about legacy templates. In regular Twig templates this issue cannot occur, as there was never this expectation / behaviour to begin with.

fritzmg avatar Dec 12 '25 14:12 fritzmg

As these errors are suppressed in prod I don’t see a BC issue here

But still, it will be unexpected in dev, as the PHP template would not generate the same error.

If required variables for the template are missing, the error is expected (and important) IMO. This can be seen as a new feature of 5.7.

ausi avatar Dec 12 '25 16:12 ausi

But the variables in this PR are not required variables.

fritzmg avatar Dec 12 '25 16:12 fritzmg

I agree it must not be added to every variable, but these are optional for sure, otherwise it would not have worked with Haste. There are a lot of cases of mandatory variables, e.g. a label for a button cannot be empty. I would just fix them as we find them, same as we did with the PHP8 array property issues.

aschempp avatar Dec 15 '25 12:12 aschempp