Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP 2.0 $this->Form->input()

Tags:

view

cakephp

This is my add.tcp ...

<?php

echo $this->Form->create('Group');
echo $this->Form->input('group_id', array('label' => 'ID'));
echo $this->Form->input('group_desc', array('label' => 'Group Description'));
echo $this->Form->end('Save');

?>

Table name: groups Table fields: group_id, group_desc PK: group_id

This is my controller ...

class GroupsController extends AppController {
  public $helper = array('Html', 'Form', 'Session');
  public $components = array('Session');

  public function add() {
    if ($this->request->is('post')) {
      if ($this->Group->save($this->request->data)) {
        $this->session.setFlash('');
        $this->redirect(array('action' => 'index'));
      }
    }
  }  
}

When I display this view on the browser, there was nothing for the field group_id but there was for the group_desc, the HTML source for the look like this ...

<form action="/cakephp/index.php/groups/add" id="GroupAddForm" method="post" accept-charset="utf-8" name="GroupAddForm">
    <div style="display:none;">
        <input type="hidden" name="_method" value="POST">
    </div><input type="hidden" name="data[Group][group_id]" id="GroupGroupId">
    <div class="input text">
        <label for="GroupGroupDesc">Group Description</label><input name="data[Group][group_desc]" maxlength="15" type="text" id="GroupGroupDesc">
    </div>
    <div class="submit">
        <input type="submit" value="บันทึก">
    </div>
</form>

Why it was hidden?

like image 945
Artisan Avatar asked Jul 13 '26 14:07

Artisan


2 Answers

CakePHP automagically determines that you do not want a user to manually enter an ID, as such hides it for you.

If you would like to force ID field to be shown, set the type to text:

echo $this->Form->input('group_id', array('type' => 'text', 'label' => 'ID'));
like image 146
uzyn Avatar answered Jul 15 '26 13:07

uzyn


Because primary key inputs are hidden by default. CakePHP creates the primary key for you on add as auto increment INT or uuid CHAR Manually creating your primary keys is not recommended.

you can change the hidden type to text:

echo $this->Form->input('group_id', array('label' => 'ID', 'type' => 'text'));
like image 32
Ceeram Avatar answered Jul 15 '26 13:07

Ceeram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!