1

I have a datetime control in CakePHP and I am trying to make it readonly (readonly attribute works fine with textfields).

So this is what I have tried:

CakePHP way:

echo $this->Form->control('date_start', ['empty' => true, 'readonly' => 'readonly']);

OR

echo $this->Form->control('date_start', ['empty' => true, 'readonly' => 'true']);

JavaScript:

$this->Html->scriptBlock("document.getElementById('#datestart').readonly = true;");

echo $this->Form->control('date_start', ['id' => 'datestart', 'empty' => true]);

But none of them works. How I can achieve what I want.

dvn22
  • 151
  • 8

1 Answers1

1

You are looking for the disabled attribute: https://www.w3schools.com/TAGS/att_input_disabled.asp Beware, if the input is disabled, it is not submitted (see below).

CakePHP :

echo $this->Form->control('date_start', ['empty' => true, 'disabled' => 'true']);

Jquery, to get or set the value (sorry, I never tried with vanilla Javascript) :

$( elem ).prop( "disabled" )

$( elem ).prop( "disabled", true )

If the input is disabled it is not submitted. Maybe that's not exactly what you are expecting. See this question for (many) possible workarounds: HTML form readonly SELECT tag/input

proprit
  • 938
  • 7
  • 13
  • But if it is disabled the value won't be posted to controller... Right? I want to save the value. For example I load the datetime value that is already stored in the database, the user can see this value but cannot change it. User can edit other fields and then save the changes – dvn22 Feb 19 '19 at 12:41