douxiluan6555 2014-11-07 17:55
浏览 81
已采纳

为什么我的activerecord表单会丢失数据? Yii2框架

I am starting with Yii framework, and it is my first framework... to the point:

I have a form to a CMS to enter a new blog post, an article. I also created a debug view to see the data being passed before I save it in the database, the thing is that when I use the form 2 of the fields dont pass any data to the debug view... I hope that more experienced people might help see what I am doing wrong here.

my code:

-Form view(new.php)

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>

<?php $form = ActiveForm::begin(); ?>

  <?= $form->field($model, 'Title') ?>
  <?= $form->field($model, 'PublicationDate')->input('date') ?>

  <?= $form->field($model, 'Content')->textarea(['rows' => 5]) ?>

  <?= $form->field($model, 'tags') ?>
<div class="form-group">
  <?= Html::submitInput('Submint', ['class' => 'btn-primary']) ?>
</div>

Article model(Article.php):

<?php

namespace app\models;
use yii\db\ActiveRecord;

class Article extends ActiveRecord{

  public $tags;

  public static function tableName()
  {
    return 'Article';
  }

  public function rules()
  {
    return[
      [['Title', 'Content'], 'required'],
    ];
  }
}

debug view:

<?php
use yii\helpers\Html;
?>
<p>You have entered the following information:</p>

<ul>
  <li><label>Title</label>: <?= Html::encode($model->Title) ?></li>
  <li><label>PublicationDate</label>: <?= Html::encode($model->PublicationDate) ?></li>
  <li><label>Content</label>: <?= Html::encode($model->Content) ?></li>
  <li><label>tags</label>: <?= Html::encode($model->tags) ?></li>
</ul>

thank you in advance for you time :)

  • 写回答

1条回答 默认 最新

  • dongxiangxie8181 2014-11-07 18:10
    关注

    Only safe attributes can receive user input. An attribute is considered safe when there is at least one validator defined for that attribute in rules(). If you simply want to declare an attribute as safe without doing any further validation, you can use the "safe" validator, like this:

    public function rules()
    {
    return[
      [['Title', 'Content'], 'required'],
      [['PublicationDate', 'tags'], 'safe'],
    ];
    }
    

    Check http://www.yiiframework.com/doc-2.0/guide-input-validation.html and http://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html for more info.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题