2

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?

3 Answers 3

4

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'));
Sign up to request clarification or add additional context in comments.

Comments

4

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'));

1 Comment

Thanks, i thought it was about time to create an account so i can reply
1

As per your database structure I want to suggest something:

Your group table has group_id as primary key make it auto_increment from database and there is no need to put that id manually on add form, it will be saved automatically. Form fields generate as per model structure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.