如何使用CakePHP在输入字段中设置默认值?

如何使用CakePHP在输入字段中设置默认值?

问题描述:

I thought I could just use "value" like below, but it doesn't seem to work. Thanks in advance for any help.

<?php 
                echo $this->Form->create('Email', array('url'=>'/emails/add', 'value'=>'enter your email address', 'inputDefaults'=>array('label'=>false))); 
                echo $this->Form->input('Email.email');
                echo $this->Form->end('SIGN-UP');
            ?>

I tried setting in my controller (ty @Jared), but it doesn't seem to work. Thoughts on what I'm doing wrong?

<?php
class EmailsController extends AppController {

    var $name = 'Emails';

    function beforeFilter() {
        parent::beforeFilter(); 
        $this->Auth->allowedActions = array('add');
    }


function create() {
    $this->data['Email']['email'] = 'enter your email address';
}
}

You're setting the value in the wrong place. You want to set the default value of the input element like this:

echo $form->input('email',array('default'=>'enter your email address'));

Look at this page in the manual.

Use a controller to set defaults. It prevents any issues that might happen and you get your default value returned, not the intended user input value:

class ProjectsController extends AppController
{
  function create()
  {
    $this->data['Project']['name'] = 'Default Project Name';
  }
}

As seen on: Snook.ca