1

ユーザーが自分の情報を編集できるページがあります。このページには、生年月日 (日付、月、年) の 3 つのドロップダウン リストもあります。しかし、「ddmmyyyy」形式の値を受け入れるテーブルには生年月日用の列が 1 つしかありません。CActiveForm でこれら 3 つのフィールドをレンダリングして、「ddmmyyyy」のような 1 つの完全な値を一緒に入力するにはどうすればよいですか? ありがとうございました。 更新:
モデル コード:

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 つのプロパティを手動で作成します。フォームでは、これら 3 つのプロパティのみを参照します。イベントを利用して、データベースの列の値を次の 3 つのフィールドにラップします。

ではafterFind()、データベースの列プロパティをこれら 3 つすべてに分割するコードがあることを確認してください。

これらbeforeValidate()3 つのプロパティを受け取り、データベースの列プロパティで結合するコードがあることを確認してください。

に必要な検証を追加し、rules()ラベルをに追加しattributes()ます。

アップデート

まず、いくつかの変更

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

日、月、年の属性を「セーフ」リストに追加する場合、すべての割り当ては必要ありません。詳しくは、安全な検証ルールについてをご覧ください。

このコードをモデル クラスに移動します。

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

フォームを検証するために検証を呼び出すようにしてください。

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

beforeValidate() の逆のコードを afterFind() に追加します。

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

これらすべての getMonth() 関数は必要ありません。PHP のrangeまたはarray_fill関数を使用できます。

于 2012-07-21T07:56:09.443 に答える
2

つまり、3 つの変数値を取得します。

3 つの変数を宣言する必要があります。

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

他の変数があります。

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

そして、この $birth_date変数データをデータベース テーブル フィールドに送信します。

于 2012-07-21T08:15:01.153 に答える