Skip to content

Instantly share code, notes, and snippets.

@jamband
Last active September 25, 2024 00:16
Show Gist options
  • Save jamband/2191329 to your computer and use it in GitHub Desktop.
Save jamband/2191329 to your computer and use it in GitHub Desktop.
Yii Framework: How to save radioButton, checkBox values.
<div class="form">
<?php echo CHtml::form(); ?>
<?php echo CHtml::errorSummary($model); ?>
<div class="row">
<?php echo CHtml::activeLabel($model, 'sex'); ?>
<?php echo CHtml::activeRadioButtonList($model, 'sex', $model->sexOptions, array(
'separator' => '',
)); ?>
</div><!-- /.row -->
<div class="row">
<?php echo CHtml::activeLabel($model, 'birth'); ?>
<?php $this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model' => $model,
'attribute' => 'birth',
)); ?>
</div><!-- /.row -->
<div class="row">
<?php echo CHtml::activeLabel($model, 'hobby'); ?>
<?php echo CHtml::activeCheckBoxList($model, 'hobby', $model->hobbyOptions); ?>
</div><!-- /.row -->
<div class="row">
<?php echo CHtml::activeLabel($model, 'body'); ?>
<?php echo CHtml::activeTextArea($model, 'body', array('rows' => 10, 'cols' => 70)); ?>
</div><!-- /.row -->
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div><!-- /.row buttons-->
<?php echo CHtml::endForm(); ?>
</div><!-- /.form -->
<?php
class Profile extends CActiveRecord
{
...
/**
* @see Cmodel::rules()
*/
public function rules()
{
return array(
// sex
array('sex', 'required', 'message'=>'{attribute} が未選択です。'),
array('sex', 'in', 'range'=>array(0, 1)),
// birth
array('birth', 'required'),
array('birth', 'date', 'format'=>'yyyy-MM-dd'),
// hobby
array('hobby', 'required', 'message'=>'{attribute} が未選択です。'),
array('hobby', 'checkHobby'),
// body
array('body', 'required'),
array('body', 'length', 'max'=>1000),
);
}
/**
* Hobby validation
*/
public function checkHobby($attribute, $params)
{
if (is_array($this->hobby)) {
$pattern = '/^(' . implode('|', $this->hobbyOptions) . ')$/u';
foreach ($this->hobby as $hobby) {
if (!preg_match($pattern, $hobby)) {
$this->addError($attribute, '趣味の値が不正です');
break;
}
}
}
}
/**
* @see CActiveRecord::attributeLabels()
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'sex' => '性別',
'birth' => '生年月日',
'hobby' => '趣味',
'body' => '自己紹介文',
);
}
/**
* Gets sex options.
* @return array sex options
*/
public function getSexOptions()
{
return array(
'男',
'女',
);
}
/**
* Gets hobby options.
* @return array hobby options
*/
public function getHobbyOptions()
{
return array(
'買い物' => '買い物',
'スポーツ' => 'スポーツ',
'旅行' => '旅行',
'インターネット' => 'インターネット',
'読書' => '読書',
'映画鑑賞' => '映画鑑賞',
);
}
/**
* @see CActiveRecord::beforeSave()
*/
protected function beforeSave()
{
$this->hobby = implode(', ', $this->hobby);
return parent::beforeSave();
}
}
<?php
class ProfileController extends Controller
{
...
/**
* Creates a new model.
*/
public function actionCreate()
{
$model = new Profile;
$model->sex = 0;
$this->save($model);
}
/**
* Updates a particular model.
* @param integer $id a particular model.
*/
public function actionUpdate($id)
{
$model = $this->loadModel($id);
$model->hobby = explode(', ', $model->hobby);
$this->save($model);
}
/**
* Saves a model.
*/
protected function save($model)
{
if (isset($_POST['Profile'])) {
$model->attributes = $_POST['Profile'];
if ($model->save()) {
$this->redirect(array('index'));
}
}
$this->render('_form', compact('model'));
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment