Form::setDisabled clear value

Notice: This thread is very old.
h4kuna
Backer | 740
+
0
-

Hi, I created form with disabled input and with default value.

<?php
$form = new Nette\Forms\Form;
$form->addText('name')
	->setDefaultValue('Milan')
	->setDisabled();

echo $form;
?>

Output:

<input name="name" type="text" disabled="disabled" />

But I want

<input name="name" type="text" disabled="disabled" value="Milan" />

When I change order, because setDisabled() clear value. Why if exists flag omitted?

This get right output.

<?php
$form = new Nette\Forms\Form;
$form->addText('name')
	->setDisabled()
	->setDefaultValue('Milan');

echo $form;
?>
Michal Vyšinský
Member | 608
+
0
-

Hi,
IMO it is not bug. I think it is for security reason. When someone change HTML in developer tools and remove disabled + add value POST will send that value. Thanks to clearing value of the field the value will be removed from POSTed data.

Last edited by Michal Vyšinský (2014-11-18 12:02)

h4kuna
Backer | 740
+
0
-

I understand, this case works fine like protection for developer's tools and this does what i want.

<?php
$form = new Nette\Forms\Form;
$form->addText('name', 'Name')
	->setDisabled() // try this call move end
	->setDefaultValue('Milan');

$form->addSubmit('send');

if($form->isSubmitted()) {
	dump($form->getValues(), $_POST);
	exit;
}

echo $form;
?>

I describe render input type text with attribute disabled not if you send data.

Last edited by h4kuna (2014-11-18 13:43)

h4kuna
Backer | 740
+
0
-

Let's try run this code:

<?php
$form = new Nette\Forms\Form;
$form->addText('name')
    ->setDefaultValue('Milan')
    ->setDisabled();

echo $form;
// another form
$form2 = new Nette\Forms\Form;
$form2->addText('name')
    ->setDisabled()
    ->setDefaultValue('Milan');

echo $form2;
?>

$form and $form2 haven't same output.

Michal Vyšinský
Member | 608
+
0
-

Sure it does not. First form calls ‘setDisabled’ after setDefaultValue so it will clear its value. Second solution is ok. I do not see a bug in this – the behaviour is logically correct. (Also trying to fix it [remove clearance of the value] would be BC break)

h4kuna
Backer | 740
+
0
-

In Nette 2.0.6 $form and $form2 have same output. After update any forms not rendered correctly.

Forms are working fine.

Last edited by h4kuna (2014-11-18 14:12)

David Grudl
Nette Core | 8116
+
0
-

Disabled controls are simply problematic.

There are a lot of quirks (see this), so current behavior is not ideal, but the best one.