Hi. I made validator to check code entered by user is existing in database. But it doesn’t pass value of field! There are files:
controllers/SiteController.php
...
public function actionIndex()
{
$model=new CodeForm;
if(isset($_POST['CodeForm']))
{
if($model->validate())
{
$this->render('info',array('message'=>"Code is correct"));
} else {
$this->render('info',array('message'=>$model->getError('code') ));
}
} else {
$this->render('main',array('model'=>$model));
}
}
...
models/CodeForm.php
<?php
class CodeForm extends CFormModel
{
public $code;
public $referer;
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
array('code', 'required'),
array('code', 'checkCode'),
);
}
/**
* 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(
'code'=>'Code',
);
}
public function checkCode($attribute,$params)
{
var_dump($this->code);
$this->addError('code',$this->code);
}
}
views/main.php
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'site-code-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<p class="note">Pola z <span class="required">*</span> są wymagane.</p>
<div class="row">
<?php echo $form->labelEx($model,'code'); ?>
<?php echo $form->textField($model,'code'); ?>
<?php echo $form->error($model,'code'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Go!'); ?>
</div>
<?php $this->endWidget(); ?>
When I write code and i click sumbit, always I get error: "Field Code can not be blank" and in left top is writed NULL, which is generated by var_dump. What may be wrong?