开发者

cakephp default values only using first character

I'm creating a cakephp app with a form that has generated default values (right now it's only placeholder data until this gets resolved) which get thrown into a table. The form works, it inserts the record fine, but for some reason the form is only using the first character of the default values. So for instance, "Title" only has the character "t" in the form, "Content" only has "c", etc.

When I pr($this->data), all the placeholder data is there. I can edit them and add more text which is saved fine, so it's not a form field length issue. Somewhere between $this->data and $this->Form->input, the defaults are getting truncated. I don't know where to begin troubleshooting this. I couldn't find anything here, and I could only find one mention of this problem by googling it, which wasn't resolved.

Cakephp 1.3.6, PHP 5.3.3, Linux

Thanks for your help

Results of pr($this->data):

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

View:

<? 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>

Controller:

<?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";


        }
    }
}
?>

Model:

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


In the following line:

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

The first parameter represents the model to which the form belongs. This formats the names of the fields as data[Generator][field_name]. So, while setting your data, you need to take care of this:

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

Let me know if this works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜