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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5410491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:18:33  来源:igfitidea点击:

How do I set a default value in an input field with CakePHP?

phpcakephp

提问by Dave

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?

我尝试在我的控制器(ty @Jared)中进行设置,但它似乎不起作用。想想我做错了什么?

<?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';
}
}

回答by blivet

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.

查看手册中的此页面

回答by Jared

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

如上所示:Snook.ca