Cakephp Forms - is there a way to make a field read-only (in view)

I have a Cakephp 1.3 form that allows users to edit profile data. But some data in forms should be read only (sometimes).

I am the only option for echoing and formatting the contents of a field in a read-only case or there is a flag in the form of Cake that allows only read-only fields. Ideally, read-only fields will be grayed out like other interfaces.

    echo $this->Form->create('User', array('url' => array('controller' => 'User', 'action'=>'editUser')));

    echo $this->Form->input('id', array('type'=>'hidden'));

If (!isset($IsAdmin)) {
    // Only display username - read only! Add code here
    echo $this->Form->input('username', array('label' => __d('users', 'User',true)));
} else {
    // Admins can edit user names
    echo $this->Form->input('username', array('label' => __d('users', 'User',true)));
}           

 ... more fields here

    echo $this->Form->end(__d('users', 'Submit',true));
+5
source share
5 answers

You can add a “disabled” key to the parameter array, however, understand that this is only the external interface / presentation of the form, people will be able to override the “disabled” property of the input field and change its value.

, " "

,

echo $this->Form->input('fieldname', array('type'=>'hidden', 'disabled' => 'disabled'));

(: http://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Models.html#saving-your-data)

$this->MyModel->save($this->data, true, array('field1', 'field2'));

,

+22

, " ". , , - , "" , .

, (, , " " ):

echo $this->Form->input('fieldname', array('readonly' => 'readonly'));

, jquery, readonly, , . datepicker bootstrap

WC3 : http://www.w3schools.com/tags/att_input_readonly.asp

+17

, , , , disable readonly javascript firebug.

+5

, , .

1) readonly ( "", , ):

echo $this->Form->input('email', array('readonly' => 'readonly'));

2) "readonly" , beforeSave :

if(isset($this->data[$this->alias]['id'])) // id is only set if we update
{
    unset($this->data[$this->alias]['email']);
}

. , ?

CakePHP . , , () , . HTML, , .

+3

:

( echo $this->Form->input ('username, array ('type' => 'hidden'));

  1. Reset , , , beforeSave.
+1

All Articles