1

我有一个页面,用户可以在其中编辑自己的信息。在此页面上,还有 3 个出生日期(日期、月份、年份)的下拉列表。但是我的表中只有一列出生日期,它接受格式为“ddmmyyyy”的值 如何在 CActiveForm 中呈现这 3 个字段,以便它们一起填充一个完整的值,例如“ddmmyyyy”?谢谢你。 更新:
型号代码:

public $day;
public $month;
public $year;

public function getDates()
{
     return array(
       20=>20,
       21=>21,
    );
}
public function getMonth()
{
     return array(
       01=>01,
       02=>02,
    );
}
public function getYears()
{
     return array(
       1990=>1990,
       1991=>1991,
    );
}

控制器代码:

if(isset($_POST['User']))
            {
                $user->attributes=$_POST['User'];
                $user->day = $_POST['User']['day'];
                $user->month = $_POST['User']['month'];
                $user->year = $_POST['User']['year'];
                $date = $user->day . $user->month . $user->year;
                $user->birthday = $date;
                if($user->save())
                    $this->redirect('/user/index');
            }

查看代码:

                <div class="day">
                    <?php echo $form->dropDownList($user,'day', $user->getDates()); ?>
                </div>
                <div class="month">
                    <?php echo $form->dropDownList($user,'month', $user->getMonth()); ?>
                </div>
                <div class="year">
                    <?php echo $form->dropDownList($user,'year', $user->getYears()); ?>
                </div>
4

2 回答 2

4

在您的模型类中手动创建这 3 个属性。在您的表单上,您将仅引用这三个属性。您将使用事件将数据库列值包装到这三个字段中,如下所示:

afterFind()中,确保您有将数据库列属性拆分为所有这三个的代码。

In beforeValidate() make sure you have code that will take these three properties and join together in the database column property.

Add the necessary validation to the rules() and labels to attributes().

Update

First of all some changes

$user->attributes=$_POST['User'];
$user->day = $_POST['User']['day'];
$user->month = $_POST['User']['month'];
$user->year = $_POST['User']['year'];

you don't need all the assignments if you add the day, month, year attribute to the 'safe' list. Read more at Understanding safe validation rules.

Move this code to the model class:

protected function beforeValidate()
{
   $this->birthday = $this->day . $this->month . $this->year;
   return parent::beforeValidate();
}

Make sure you call validation to validate the form:

if($user->validate() && $user->save())
         $this->redirect('/user/index');

add code to afterFind() that is reverse to beforeValidate() as

protected function afterFind()
{
    $this->day= ...get day part from $this->birthday...
    $this->month=
    $this->year=
    return parent::afterFind();
}

You don't need all those getMonth() functions. You can use the range or the array_fill functions from PHP.

于 2012-07-21T07:56:09.443 回答
2

That means you get three variable value

you have to declare three variable.

 $date=$_REQUEST['date'];
 $month=$_REQUEST['month'];
 $year=$_REQUEST['year'];

and you have other variable.

 $birth_date=$date."/".$month."/".$year;

And then you will send this $birth_date variable data to your database table fild

于 2012-07-21T08:15:01.153 回答