【问题标题】:Why does my activerecord form lose the data? Yii2 framework为什么我的 activerecord 表单会丢失数据? Yii2 框架
【发布时间】:2014-11-07 17:55:02
【问题描述】:

我是从 Yii 框架开始的,它是我的第一个框架……直言不讳:

我有一个 CMS 的表单来输入新的博客文章、文章。我还创建了一个调试视图,以便在将数据保存到数据库之前查看正在传递的数据,问题是当我使用字段的表单 2 时,不会将任何数据传递给调试视图......我希望更有经验的人可能有助于了解我在这里做错了什么。

我的代码:

-表单视图(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.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'],
    ];
  }
}

调试视图:

<?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>

提前感谢您的时间:)

【问题讨论】:

    标签: php activerecord yii2


    【解决方案1】:

    只有 safe 属性可以接收用户输入。当rules() 中为该属性定义了至少一个验证器时,该属性被认为是安全。如果您只是想将属性声明为安全而不进行任何进一步验证,则可以使用“安全”验证器,如下所示:

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

    查看http://www.yiiframework.com/doc-2.0/guide-input-validation.htmlhttp://www.yiiframework.com/doc-2.0/guide-tutorial-core-validators.html 了解更多信息。

    【讨论】:

    • 非常感谢!那行得通,现在我有一些材料可以再次阅读 thx :)
    猜你喜欢
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    相关资源
    最近更新 更多