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 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体