今度はユーザ情報を登録するフォームなんかを作ってみたいと思います
Giiを使うと、だいたいの骨子を作ってくれるので楽です(たぶん)
CRUDを実装するコードを生成する
Giiを使ってUserProfileモデルに対してCRUD(作成(Create)、読み出し(Read)、更新(Update)、削除(Delete))操作を実装するコードを自動生成する。
http://www.example.com/ec_site/index.php?r=gii
にアクセスして、Crud Generatorのリンクをクリックする。
Crud Generatorのページで、Model ClassフィールドにUserProfileを入力し、Previewボタンを押す。
下に生成されるファイルがリストされるので、確認したらGenerateボタンを押して生成する。
http://www.example.com/ec_site/index.php?r=userProfile
にアクセスすると、何かそれっぽいものが見える
UserProfileのラベルを日本語に書き換えておく
...
public function attributeLabels()
{
return array(
'lastname' => '姓',
'firstname' => '名',
'kana_lastname' => 'セイ',
'kana_firstname' => 'メイ',
'corporation' => '法人名',
'kana_corporation' => 'ホウジンメイ',
'post' => '部署',
'zipcode' => '郵便番号',
'address' => '住所',
'phone1' => '電話番号1',
'phone2' => '電話番号2',
'fax' => 'Fax',
'email2' => '連絡用メールアドレス',
'deliverable_zipcode' => 'お届け先郵便番号',
'deliverable_address' => 'お届け先住所',
'point' => 'ポイント',
);
}
...
UserProfileController.phpの編集
##accessRules()の編集
ゲストユーザが新規会員登録を、ログインユーザが会員情報更新をできるように、accessRules()を書きかえる
actionCreate()とactionUpdate()の編集
確認画面をを挟んでからデータベースへ書き込むように書きかえる
参考にしたのはこちらの記事→確認画面を挟んだフォームを作る
...
public function actionCreate()
{
$model=new UserProfile;
$model->id=Yii::app()->user->id;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['confirm']))
{
$model->attributes=$_POST['User'];
if($model->validate())
{
$this->setPageState('create', $_POST['UserProfile']);
$this->render('confirm', compact('model'));
return;
}
} else if(isset($_POST['back']))
{
$model->attributes = $this->getPageState('create');
}
else if(isset($_POST['finish']))
{
$model->attributes = $this->getPageState('create');
if($model->save()) {
$this->redirect(Yii::app()->baseUrl);
}
}
$this->render('create', compact('model'));
}
...
public function actionUpdate()
{
$model=$this->loadModel(Yii::app()->user->id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['confirm']))
{
$model->attributes=$_POST['Userprofile'];
if($model->validate())
{
$this->setPageState('update', $_POST['Userprofile']);
$this->render('confirm', compact('model'));
return;
}
} else if(isset($_POST['back']))
{
$model->attributes = $this->getPageState('update');
}
else if(isset($_POST['finish']))
{
$model->attributes = $this->getPageState('update');
if($model->save()) {
$this->redirect(Yii::app()->baseUrl);
}
}
$this->render('update', compact('model'));
}
フォームの編集
_form.phpの編集
updateもcreateも_form.phpを呼び出しているのでこれを編集する
submitボタンを作り、あと、ところどころ日本語化
...
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user-form',
// Please note: When you enable ajax validation, make sure the corresponding
// controller action is handling ajax validation correctly.
// There is a call to performAjaxValidation() commented in generated controller code.
// See class documentation of CActiveForm for details on this.
'enableAjaxValidation'=>false,
)); ?>
<p class="note"><span class="required">*</span>は必須事項です</p>
<?php echo $form->errorSummary($model); ?>
...
<div class="row buttons">
<?php echo CHtml::submitButton('確認画面へ', array('name' => 'confirm')); ?>
<?php //echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
...
confirm.phpの作成
記入内容確認画面はViewとほとんど同じなので、view.phpをコピーして、submitボタンを追加する。更新のときはログイン用メールアドレスとパスワードを表示しないので、$attrを変えている
<?php
/* @var $this UserController */
/* @var $model User */
$this->breadcrumbs=array(
'Users'=>array('index'),
$model->id,
);
$this->menu=array(
array('label'=>'List User', 'url'=>array('index')),
array('label'=>'Create User', 'url'=>array('create')),
array('label'=>'Update User', 'url'=>array('update', 'id'=>$model->id)),
array('label'=>'Delete User', 'url'=>'#', 'linkOptions'=>array('submit'=>array('delete','id'=>$model->id),'confirm'=>'Are you sure you want to delete this item?')),
array('label'=>'Manage User', 'url'=>array('admin')),
);
?>
<?php
$this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'lastname',
'firstname',
'kana_lastname',
'kana_firstname',
'corporation',
'kana_corporation',
'post',
'zipcode',
'address',
'phone1',
'phone2',
'fax',
'email2',
'deliverable_zipcode',
'deliverable_address',
),
)); ?>
<div class="form">
<?php
// 以下を追加
echo CHtml::statefulForm();
echo CHtml::submitButton('戻る', array('name' => 'back'));
echo CHtml::submitButton('次へ', array('name' => 'finish'));
?>
</form>
</div>
メニューバーに新規会員登録と会員情報更新を加える
何度もアドレス入力するのが大変なので、メニューバーの中に新規会員登録と会員情報更新を加えておく
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'新規会員登録', 'url'=>array('/user/create'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'会員情報更新', 'url'=>array('/userProfile/update'), 'visible'=>!Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
)); ?>
#パスワードにハッシュをかける
パスワードはデータベースに登録する前にハッシュをかけるので、beforeSave()をオーバーライドする
これで一応のユーザ登録ができるようになりました。次は検証ルールのカスタマイズなどをしていきたいと思います。