2011-11-10 125 views
0

我使用cake bake命令创建了一个应用程序,一个表格使用文本作为字段类型。为什么cakephp不能填充textarea?

所有的工作都很好,为该字段生成了一个textarea,并按预期工作。 我保存了一些条目,查看它们编辑了几个。

我意识到,使用编辑时,一个条目没有填充textarea。它在视图中正确显示,但编辑数据时不在textarea中,并且保存会导致所有信息丢失。

这发生在一个普通的烘焙桌上。

这里是我用它textarea的值:

上午克里斯托弗,

请你可以给我的密码,我们的电子邮件帐户在Citano因为我们需要它来恢复我的主管电子邮件帐户为他的黑莓。

·您的紧急回复在这件事上是必需的。

Jaques Kruger Citano Distributors。

是的,在那里有一个奇怪的字符,我认为从字的一个要点。

这是一个框架错误,稍后修复? 我在1.3.10和1.3.8上测试过。

任何帮助,将不胜感激。

编辑:

这里是标准的出炉视图 “edit.ctp”:

<div class="tickets form"> 
<?php echo $this->Form->create('Ticket', array('type'=>'file'));?> 
    <fieldset> 
     <legend><?php __('Edit Ticket'); ?></legend> 
    <?php 
     echo $this->Form->input('Ticket.id'); 
     echo $this->Form->input('Ticket.name', array('label' => 'Ticket name')); 
     echo $this->Form->input('Ticket.details'); # Problem textarea not populating 
     echo $this->Form->input('Ticket.user_id', array('label' => 'Assigned to')); 
     echo $this->Form->input('Ticket.queue_id'); 
     echo $this->Form->input('Ticket.client_id', array('options' => $clients,'default' => $defaultClient,'empty' => 'None')); 
     echo $this->Form->input('Ticket.status', array('options' => $ticketStatus)); 
     echo $this->Form->input('Ticket.accepted', array('type' => 'checkbox')); 
     echo $this->Form->input('Ticket.linkedticketid', array('options' => $linkTickets,'empty' => 'None','style' => 'width:100%;','label' => 'Linked ticket')); 
     echo $this->Form->input('Ticket.due_date'); 
     echo $this->Form->file('attachment'); 
     echo $this->Form->input('email', array('label' => 'Additional email to mailing list')); 
    ?> 
    </fieldset> 
<?php echo $this->Form->end(__('Submit', true));?> 
</div> 
<div class="actions"> 
    <h3><?php __('Actions'); ?></h3> 
    <ul> 
     <li><?php echo $this->Html->link(__('View Ticket', true), array('action' => 'view', $this->Form->value('Ticket.id'))); ?> </li> 
     <li><?php echo $this->Html->link(__('List Ticket', true), array('action' => 'index')); ?> </li> 
    </ul> 
</div> 

这里是控制器:正如我之前所说

function edit($id = null) { 
    if (!$id && empty($this->data)) { 
     $this->Session->setFlash(__('Invalid ticket', true)); 
     $this->redirect(array('action' => 'index')); 
    } 
    if (!empty($this->data)) { 
     if ($this->Ticket->save($this->data)) { 
      $this->Session->setFlash(__('The Ticket has been saved', true)); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The Ticket could not be saved. Please, try again.', true)); 
     } 
    } 
    if (empty($this->data)) { 
     $this->data = $this->Ticket->read(null, $id); 
    } 
} 

中,数据已被添加,并且数据没有任何问题,但编辑textarea时并不总是以现有数据填充,而其余的o f这些字段始终填充现有数据。

调试数据在edit.ctp

Array 
(
    [Ticket] => Array 
     (
      [id] => 281 
      [linkedticketid] => 
      [user_id] => 1 
      [queue_id] => 4 
      [name] => password 
      [details] => Morning Christopher, 

Please can you send me the password for our email accounts at Citano as we need it to reset my directors email account for his blackberry. 

�  Your urgent response is required in this matter. 

Jaques Kruger 
Citano Distributors. 

      [status] => 0 
      [accepted] => 0 
      [due_date] => 2011-11-12 
      [mailedtech] => 1 
      [uuid] => 34b12707-0b87-11e1-97b4-001e90d15e95 
      [created] => 
      [modified] => 2011-11-10 12:48:51 
     ) 

    [User] => Array 
     (
      [id] => 1 
      [group_id] => 1 
      [name] => Christopher 
      [username] => Nightwolf 
      [password] => 8be1083780671a3b3541ff82r161d3d55a4e5d79 
      [email] => [email protected] 
      [created] => 2011-09-05 13:30:56 
      [modified] => 2011-09-05 13:31:34 
     ) 

    [Queue] => Array 
     (
      [id] => 4 
      [name] => Christopher 
      [rank] => 20 
      [created] => 2011-09-05 13:58:40 
      [modified] => 2011-09-05 13:58:40 
     ) 

    [Task] => Array 
     (
     ) 

    [Ticketforclient] => Array 
     (
      [0] => Array 
       (
        [id] => 95 
        [ticket_id] => 281 
        [client_id] => 
        [created] => 2011-11-10 12:34:45 
        [modified] => 2011-11-10 12:48:51 
       ) 

     ) 

    [Ticketnote] => Array 
     (
     ) 

    [Ticketsinqueue] => Array 
     (
      [0] => Array 
       (
        [id] => 67 
        [ticket_id] => 281 
        [queue_id] => 4 
        [created] => 
        [modified] => 2011-11-10 12:48:50 
       ) 

     ) 

    [Activitylog] => Array 
     (
     ) 

    [Billinginfo] => Array 
     (
     ) 

    [Attachment] => Array 
     (
     ) 

    [Ticketmailinglist] => Array 
     (
      [0] => Array 
       (
        [id] => 11 
        [ticket_id] => 281 
        [emailaddress] => [email protected] 
        [originalsender] => 0 
       ) 

     ) 

) 
+0

向我们展示代码。如果你执行'debug($ this-> data)'你会看到那里的文字吗? – JJJ

+0

@Juhana我已经添加了标准cakephp生成的代码,你问。 – Nightwolf

+0

因为这些字段是从'$ this-> data'填充的。如果文本不存在,则不会将其插入到字段中。 – JJJ

回答

7

确保~/app/config/core.php使用这样的:

Configure::write('App.encoding', 'UTF-8'); 

布局部分

<?php echo $html->charset(); ?> 

数据库配置

class DATABASE_CONFIG { 
    public $default = array(
     ... 
     'encoding' => 'utf8' 
    ); 
} 

阅读该

http://book.cakephp.org/view/1616/x1-3-improvements

+0

该链接没有说明你从哪里得到数据库编码提示。这是造成我的问题。你能发表一个链接吗? – Nightwolf

+0

我刚刚发布根据我的经验,但现在搜索,这里有一些提示http://lecterror.com/articles/view/cakephp-character-encoding-special-characters – Synxmax

+0

而我的朋友无论你在保存或检索特殊字符,从编码到解码(配置等)的所有步骤,确保所有使用相同的标准或更广泛的编码范围 – Synxmax

0
�  Your urgent response is required in this matter. 

如果它没有显示在调试过,那么人们会认为这是与您的数据有问题。将源文本粘贴到记事本或类似文件中,然后再次尝试查看是否清除了问题字符。 =)

编辑

如果你有超过输入的字符无法控制,那么你可能要考虑编码您的用户进入到一个已知的格式文本,然后用正则表达式白名单可接受的字符。这将防止古怪的角色永久偷偷摸摸。=)

+0

字符贴在记事本中完美(我不会猜到)。我无法控制数据到达数据库的哪些数据,也无法请求用户避开奇怪的字符。我可以通过输入cakephp应该用它的帮助器来使这些数据工作。 ''textarea name =“data [Ticket] [details]”><?php echo $ this-> data ['Ticket'] ['details']?>' – Nightwolf

+0

正则表达式,我在我的回答中解释过。 =) –

+0

我想保持原样。就像我说过的,如果我首先输入帮助者应该给我的东西,那么这个工作就完美了。 – Nightwolf

0

我怀疑这个奇怪的字符是在Web浏览器端打破HTML的呈现。

你看过生成的HTML吗?文本是否显示在源视图的textarea上(Firefox上的Ctrl + U)?

如果这样不能提供足够的信息,请尝试安装Wireshark,并记录请求和响应。它有一个十六进制视图,应该让你看到究竟是什么角色。

您可能需要过滤哪些字符可以插入到数据库中,以及可以在textarea中显示哪些符合ASCII的字符。

+0

我也这么认为,但事实并非如此,html没有textarea中的数据。我目前有一个工作的解决方法,这正是cakephp表单助手应该首先产生的。 – Nightwolf

+0

如果您找到解决方法,请将其作为答案发布。其他人将在未来出现同样的问题。 –

1

根据我的说法,这是一种糟糕的做法,但这是我知道如何做的唯一方法。

cakephp的textarea formhelper似乎排除了由于数据库中奇怪的值而导致的textarea内容。 这里是形式助手:

echo $this->Form->input('Ticket.details'); 

这里是它自己生成的html:

echo '<textarea name="data[Ticket][details]" cols="30" rows="6" id="TicketDetails">'.$ticketDetail.'</textarea>'; 

$ ticketDetail是从数据库中的内容。

通过使用写出的HTML完美的作品。