【问题标题】:cakephp default values only using first charactercakephp 默认值仅使用第一个字符
【发布时间】:2010-12-12 02:21:12
【问题描述】:

我正在创建一个 cakephp 应用程序,该应用程序的表单已生成默认值(现在它只是占位符数据,直到解决此问题)被扔到表中。该表单可以正常工作,它可以很好地插入记录,但由于某种原因,该表单仅使用默认值的第一个字符。例如,“标题”在表单中只有字符“t”,“内容”只有“c”等。

当我 pr($this->data) 时,所有占位符数据都在那里。我可以编辑它们并添加更多保存良好的文本,因此这不是表单字段长度问题。在 $this->data 和 $this->Form->input 之间的某个地方,默认值会被截断。我不知道从哪里开始解决这个问题。我在这里找不到任何东西,我只能通过谷歌搜索找到一个提到这个问题的内容,但没有解决。

Cakephp 1.3.6、PHP 5.3.3、Linux

感谢您的帮助

pr($this->data)的结果:

Array
(
    [title] => title
    [content] => content
    [media_url] => media_url
)

查看:

<? pr($this->data); ?>

<div class="generators form">
<?php echo $this->Form->create('Generator');?>
    <fieldset>
        <legend>Create New Post</legend>
    <?php
        echo $this->Form->input('title');
        echo $this->Form->input('content');
        echo $this->Form->input('publish_date');
        echo $this->Form->input('media_url');

    ?>
    </fieldset>
<?php echo $this->Form->end('Create Post');?>
</div>

控制器:

<?php
class GeneratorsController extends AppController {

    var $name = 'Generators';

    function posts() 
    {
        // save the post
        if (!empty($this->data)) {
            $this->Generator->create();
            if ($this->Generator->save($this->data)) {
                $this->Session->setFlash(__('The post has been created', true));
                $this->redirect(array('action' => 'posts'));


                // TODO: call posting app


            } else {
                $this->Session->setFlash(__('There was a problem. Please, try again.', true));
            }
        }
        else
        {
                // create post
                $this->data['title'] = "title";
                $this->data['content'] = "content";
                //$this->data['publish_date'] = "";
                $this->data['media_url'] = "media_url";


        }
    }
}
?>

型号:

<?php
class Generator extends AppModel {
    var $name = 'Generator';
    var $displayField = 'title';
}
?>

【问题讨论】:

    标签: cakephp default-value


    【解决方案1】:

    在下面一行:

    <?php echo $this->Form->create('Generator'); ?>
    

    第一个参数代表表单所属的模型。这会将字段名称格式化为data[Generator][field_name]。因此,在设置数据时,您需要注意以下事项:

    function posts() {
        if (!empty($this->data)) {
            ...
        } else {
            $this->data = array(
                'Generator' => array(
                    'title' => 'title',
                    'content' => 'content',
                    'media_url' => 'media_url'
                )
            );
        }
    }
    

    让我知道这是否有效。

    【讨论】:

    • 宾果游戏,感谢您的超快速响应。不会再犯这个错误
    猜你喜欢
    • 1970-01-01
    • 2015-05-20
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    相关资源
    最近更新 更多