Answer the question
In order to leave comments, you need to log in
Does not display in Yii2 ActiveForm. What is the problem?
I wanted to make a mini-admin panel and ran into the fact that on the creation page it does not display the form field at all. Tried to do in another project but no change.
view:
<?
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
?>
<h1>Создать</h1>
<? $form = ActiveForm::begin(); ?>
<div class="row">
<div class="col-md-6">
<? $form->field($model,'title')->textInput()?>
</div>
<div class="col-md-6">
<? $form->field($model,'text')->textarea()?>
</div>
<div class="col-md-6">
<? $form->field($model,'image')->fileInput()?>
</div>
<div class="col-md-12">
<? Html::submitButton('Создать', ['class'=>'btn btn-success']) ?>
</div>
</div>
<? ActiveForm::end() ?>
namespace app\controllers;
use app\models\News;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
class SiteController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionIndex()
{
$news = News::getNews();
return $this->render('index', ['news'=>$news]);
}
public function actionNews($id)
{
$data = News::getOneNews($id);
return $this->render('news',['data'=>$data,'id'=>$id]);
}
public function actionAdmin()
{
$list = News::getNews();
return $this->render('admin',['list' => $list]);
}
public function actionCreate()
{
$model = new News();
if ($_POST['News'])
{
$model->title = $_POST['News']['title'];
$model->text = $_POST['News']['text'];
$model->image = $_POST['News']['image'];
if ($model->validate() && $model->save())
{
return $this->redirect(['admin']);
}
}
return $this->render('create',['model'=>$model]);
}
}
<?php
namespace app\models;
use yii\db\ActiveRecord;
class News extends ActiveRecord
{
public static function tableName()
{
return 'news';
}
public static function getNews()
{
$data = self::find()->all();
return $data;
}
public static function getOneNews($id)
{
$data = self::find()
->where(['id' => $id])
->one();
return $data;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question