douxiluan6555 2014-11-07 09: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 10: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部