我反复犯同样的错误。下拉数据不是从表单中获取的。我的代码在这里
这是我的控制器:ProductController
{<?php
class ProductController extends Controller
{
public function actionCreate()
{
$model=new CreateForm;
// collect user input data
if(isset($_POST['CreateForm']))
{
$model->attributes=$_POST['CreateForm'];
$model->setAttributes($_POST['CreateForm']);
// validate user input and redirect to the previous page if valid
if($model->validate())
{
$product=new Product;
$product->save();
}
else {
echo "Hi";
}
}
// display the login form
$this->render('create',array('model'=>$model));
}
}
?>}
我的模特:CreateForm
{<?php
class CreateForm extends CFormModel
{
public $product_name;
public $category_name;
public $description;
public function rules()
{
return array(
array('product_name, category_name, description', 'required'),
array('product_name', 'unique', 'className' => 'Product', 'attributeName' => 'product_name', 'message'=>'This product name is already in use'),
);
}
public function attributeLabels()
{
return array(
'product_name'=>'PRODUCT NAME',
'category_name'=>'CATEGORY',
'description'=>'DESCRIPTION'
);
}
}
?>
}
分类
{
<?php
class Category extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'category';
}
public function attributeLabels()
{
return array(
'category_id'=>'CATEGORY ID',
'category_name'=>'CATEGORY NAME',
);
}
}
?>}
我的观点:create.php
{<?php
$this->pageTitle=Yii::app()->name . ' - Create';
$this->breadcrumbs=array(
'Create',
);
?>
<h1>CREATE</h1>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'create-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
));
?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'product_name'); ?>
<?php echo $form->textField($model,'product_name'); ?>
<?php echo $form->error($model,'product_name'); ?>
</div>
<div class="row">
<?php
echo $form->labelEx($model,'category_name');
$records = Category::model()->findAll();
$list = CHtml::listData($records, 'category_id', 'category_name');
echo CHtml::DropDownList('category_name', null, $list, array('prompt'=>'select '));
echo $form->error($model,'category_name');
?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?></br>
<?php echo $form->textArea($model,'description',array('style' => 'height:100px;width:500px;','maxlength'=>500)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row submit">
<?php echo CHtml::submitButton('CREATE'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
}
如果我输入所有字段,则会再次出现错误,指出“PLS输入类别”
答案 0 :(得分:0)
你所做的就是大规模的作业
$model->attributes=$_POST['CreateForm'];
这不会分配所有值,也不安全处理数据
仅当数据在模型中处于安全状态时才会保存数据
您将重新声明发布的数据
$model->attributes=$_POST['CreateForm'];
$model->dropdownname=$_POST['CreateForm']['dropdownname'];
希望这会对你有所帮助
http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/#hh2
答案 1 :(得分:0)
我认为你改变了下拉代码。
<?php
echo $form->labelEx($model,'category_name');
$records = Category::model()->findAll();
$list = CHtml::listData($records, 'category_id', 'category_name');
echo CHtml::DropDownList('category_name', null, $list, array('prompt'=>'select '));
echo $form->error($model,'category_name');
?>
到
<?php
echo $form->labelEx($model,'category_name');
$records = Category::model()->findAll();
$result = array();
foreach($records as $p) {
$result[p->category_id] = p->category_name ;
}
echo CHtml::activeDropDownList($model, 'category_name', $result);
echo $form->error($model,'category_name');
?>
现在您需要检查,您的类别ID或名称是否来了。 然后调试控制器文件,如下所示。
public function actionCreate()
{
$model=new CreateForm;
// collect user input data
if(isset($_POST['CreateForm']))
{
$model->attributes=$_POST['CreateForm'];
echo $model->category_id ;
exit ; // Finish program here, and drop down will be printed.
$model->setAttributes($_POST['CreateForm']);
// validate user input and redirect to the previous page if valid
if($model->validate())
{
$product=new Product;
$product->save();
}
else {
echo "Hi";
}
} // display the login form
$this->render('create',array('model'=>$model));
}
希望对你有所帮助。
由于