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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?