I am using Yii 1.1.9 and have spent some time looking at different forum posts and reading the available documentation but I guess I am still missing something. At this point some explanation of what I am missing would be greatly appreciated.
Problem:
My ActiveForm Contact has a Country dropDownList that is populated using a separate table and model, but the value is to be stored in my Contact table which has a country column.
Tables:
Contact cols(contact_id pk, firstname, lastname, etc., country)
Country cols(code pk, name)
Model is as follows:
<?php
class Contact extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Contact the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'contact';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('title, firstname, lastname, address, city, state, zip, country, citizenship, phone, email, birthdate, password', 'required'),
//array('contact_id', 'numerical', 'integerOnly'=>true),
array('title, state, zip, phone, birthdate', 'length', 'max'=>20),
array('firstname, lastname, citizenship, email, password', 'length', 'max'=>50),
array('address, country', 'length', 'max'=>150),
array('city', 'length', 'max'=>100),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('contact_id, title, firstname, lastname, address, city, state, zip, country, citizenship, phone, email, birthdate, password', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'state' => array(self::BELONGS_TO, 'State', 'state_id'),
'country' => array(self::BELONGS_TO, 'Country', 'country_code'),
);
}
}
Here is my view from _form.php:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contact-form',
'enableAjaxValidation'=>false,
)); ?>
...
<?php
echo $form->dropDownList(new Country,'name',
CHtml::listData(Country::model()->findAll(array('order' => 'name')), 'name', 'name'),
array('empty'=> '--please select--'));
?>
This loads the countries into the dropDownList and sets the empty value as the default selection, however when I choose a different country submit it to the Controller to create the contact record the save fails, and the validation says ‘country cannot be blank’. In addition I noticed that the country I selected is not remembered by the form and the dropDownList returns to the default selected option. If I use a static array for the options the selection is remembered by the form.
Your help is greatly appreciated.
For completeness here is my actionCreate method in the Controller:
public function actionCreate()
{
$model=new Contact;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Contact']))
{
$model->attributes=$_POST['Contact'];
$model->state=$_POST['state'];
$model->country=$_POST['country'];
}
...