Ass a noob i am having difficulties creating a registration form:
in sitecontroller:
public function actionRegister()
{
$model =new RegisterForm;
$visitor = new Visitor;
if(isset($_POST['RegisterForm']))
{
$model->attributes=$_POST['RegisterForm'];
if($model->validate())
{
$visitor->attributes = $model->attributes;
$visitor->save();
if($visitor->save())
{
//success
Yii::app()->user->setFlash('register','Takk för din påmelding, en e-post er nå sent som en kvittering');
$this->refresh();
}
else
{
//fail
Yii::app()->user->setFlash('register','Beklager, det uppsto en feil vennligst forsok på nytt.');
$this->refresh();
}
//$this->redirect(Yii::app()->user->returnUrl);
}
}
$this->render('register',array('model'=>$model));
//print_r($modelForm->attributes);
}
the form model:
RegisterForm.php
<?php
/**
* ContactForm class.
* ContactForm is the data structure for keeping
* contact form data. It is used by the 'contact' action of 'SiteController'.
*/
class RegisterForm extends CFormModel
{
public $transport;
public $hotel;
public $allergy;
public $needs;
public $firstname;
public $lastname;
//public $companyname;
public $email;
public $mobilephone;
public $confirm;
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
// name, email, subject and body are required
array('transport', 'required', 'message'=> '{attribute} må velges.'),
array('hotel', 'required', 'message'=> 'Hotel må velges.'),
array('firstname', 'required', 'message'=> 'Fornavn må fylles in.'),
array('lastname', 'required', 'message'=> 'Etternavn må fylles in.'),
array('mobilephone', 'required', 'message'=> 'Mobiltelefon må fylles in.'),
array('confirm', 'required','requiredValue'=>'1', 'message'=> 'Betingelser må akepteres.'),
// email has to be a valid email address
array('email', 'email', 'message'=> 'E-post er ikke en gyldig e-post.'),
array('mobilephone', 'numerical', 'message'=> 'Mobiltelefon er ikke ett gyldigt nummer.'),
// verifyCode needs to be entered correctly
//array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),
);
}
/**
* Declares customized attribute labels.
* If not declared here, an attribute would have a label that is
* the same as its name with the first letter in upper case.
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'firstname' => 'Fornavn',
'lastname' => 'Etternavn',
'email' => 'E-post',
'mobilephone' => 'Mobiletelefon',
'timeregistered' => 'Tid Registrert',
'transport' => 'Transport',
'allergy' => 'Alleriger',
'needs' => 'Andre behov',
'hotel' => 'Hotel',
);
}
}
the view for the form:
register.php
<?php
$this->pageTitle=Yii::app()->name . ' - Register';
$this->breadcrumbs=array(
'Register',
);
?>
<h1>Påmelding</h1>
<?php if(Yii::app()->user->hasFlash('register')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('register'); ?>
</div>
<?php else: ?>
<p>
Fyll in din information her.
</p>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm'); ?>
<p class="note">Felt med <span class="required">*</span> må fylles in.</p>
<?php echo $form->errorSummary($model, 'Vennligst endre føljende feil:'); ?>
<div class="row">
<?php echo $form->labelEx($model,'transport'); ?>
<?php echo $form->dropDownList($model,'transport', array(''=>'-- Velg by --', 'Oslo'=>'Oslo','Bergen'=>'Bergen','Hamar'=>'Hamar')); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'hotel'); ?>
<?php echo $form->dropDownList($model,'hotel', array('Nej, takk'=>'Nej, takk','Enkeltrom'=>'Enkeltrom','Dobbeltrom'=>'Dobbeltrom')); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'allergy'); ?>
<?php echo $form->textField($model,'allergy',array('size'=>60,'maxlength'=>128)); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'needs'); ?>
<?php echo $form->textArea($model,'needs',array('rows'=>6, 'cols'=>50)); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'firstname'); ?>
<?php echo $form->textField($model,'firstname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastname'); ?>
<?php echo $form->textField($model,'lastname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'mobilephone'); ?>
<?php echo $form->textField($model,'mobilephone'); ?>
</div>
<div class="row">
<?php echo CHtml::activeCheckBox($model,'confirm'); ?>
<?php echo 'Jeg har lest og akseptert betingelsene.' ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
<?php endif; ?>
the model for the entries:
Visitor.php
<?php
/**
* This is the model class for table "tbl_visitor".
*
* The followings are the available columns in table 'tbl_visitor':
* @property integer $id
* @property string $firstname
* @property string $lastname
* @property string $email
* @property string $mobilephone
* @property string $timeregistered
* @property string $transport
* @property string $allergy
* @property string $needs
*/
class Visitor extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @return Visitor 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 'tbl_visitor';
}
/**
* @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('firstname, lastname, email, mobilephone, timeregistered, transport, allergy, needs', 'required'),
array('firstname, lastname, email, mobilephone, transport', 'length', 'max'=>255),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, firstname, lastname, email, mobilephone, timeregistered, transport, allergy, needs', '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(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'firstname' => 'Fornavn',
'lastname' => 'Etternavn',
'email' => 'E-post',
'mobilephone' => 'Mobiletelefon',
'timeregistered' => 'Tid Registrert',
'transport' => 'Transport',
'allergy' => 'allergi',
'needs' => 'Behov',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('firstname',$this->firstname,true);
$criteria->compare('lastname',$this->lastname,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('mobilephone',$this->mobilephone,true);
$criteria->compare('timeregistered',$this->timeregistered,true);
$criteria->compare('transport',$this->transport,true);
$criteria->compare('allergy',$this->allergy,true);
$criteria->compare('needs',$this->needs,true);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
}
what am i doing wrong? the form isnt saving to the database… is it the "public $confirm;"? how to i remove this from the post array for the database?
thanks