Can someone please tell what I am doing wrong here? The form validation just doesn’t work. For example bob*+ is not valid username but the form accepts it with no errors…
Here’s my model class:
<?php
class newUserForm extends CFormModel {
public $username;
public $fullName;
public $email;
public $phonenumber;
public function rules() {
return array(
array('username, fullName, email', 'required'),
array('fullName', 'length', 'max' => 30),
array('username', 'length', 'max' => 20),
array('username', 'match', 'pattern' => '/^([A-Za-z0-9_\-\.])/'),
array('email', 'email', 'checkMX' => TRUE),
);
}
}
?>
And here’s Controller:
<?php
class UserManagementController extends Controller {
public function filters() {
return array(
'accessControl' // required to enable accessRules
);
}
public function accessRules() {
return array(
array('allow', // allow readers only access to the view file
'users' => array('*')
),
);
}
// Called before each action
protected function beforeAction() {
if (Yii::app()->user->checkAccess(ucfirst($this->getAction()->getId())))
return true;
else
throw new CHttpException(400, Yii::t('err', 'You are not authorized to view this page'));
}
/**
* Declares class-based actions.
*/
public function actions() {
return array(
// captcha action renders the CAPTCHA image displayed on the contact page
'captcha' => array(
'class' => 'CCaptchaAction',
'backColor' => 0xFFFFFF,
),
// page action renders "static" pages stored under 'protected/views/site/pages'
// They can be accessed via: index.php?r=site/page&view=FileName
'page' => array(
'class' => 'CViewAction',
),
);
}
public function actionUserManagement() {
$model = new newUserForm;
$this->performAjaxValidation($model);
if (isset($_POST['newUserForm'])){
$model->attributes = $_POST['newUserForm'];
if($model->validate()){
Yii::app()->getController()->redirect('userSubmit');
}
}
$this->render('userManagement/userManagementView', array(
'model' => $model,
));
}
protected function performAjaxValidation($model){
if (isset($_POST['ajax']) && $_POST['ajax'] === 'add_user_form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
public function actionUserSubmit() {
$this->render('userManagement/submitSuccessful');
}
}
?>
And finally the view:
<?php
$this->pageTitle = Yii::app()->name . ' - ' . Yii::t('translation', 'User management');
echo CHtml::tag('h2') . Yii::t('translation', 'User management') . CHtml::closeTag('h2');
echo "<b>" . Yii::t('translation', 'Create user by hand') . "</b>\n";
echo CHtml::tag('div', array('class'=>'form')) . "\n";
$form = $this->beginWidget('CActiveForm', array(
'id' => 'add_user_form',
'method' => 'POST',
'action' => 'userManagement',
'focus'=>array($model, 'loginname'),
'enableClientValidation' => TRUE,
'enableAjaxValidation' => TRUE,
'clientOptions' => array(
'validateOnSubmit' => TRUE,
'validateOnChange' => TRUE,
'validateOnType' => TRUE,
),
));
?>
<div class="view">
<p class="note">
<?php echo Yii::t('translation', 'Fields with'); ?>
<span class="required">*</span>
<?php echo Yii::t('translation', 'are required.'); ?>
</p>
<div class="row">
<?php $form->labelEx($model, 'username'); ?>
<?php echo Yii::t('translation', 'Username')."<span class=\"required\"> *</span><br>\n"; ?>
<?php echo $form->textField($model, 'username', array('autocomplete' => 'on')); ?>
<?php echo $form->error($model, 'username') ?>
</div>
<div class="row">
<?php $form->labelEx($model, 'fullName'); ?>
<?php echo Yii::t('translation', 'Full name')."<span class=\"required\"> *</span><br>\n"; ?>
<?php echo $form->textField($model, 'fullName', array('autocomplete' => 'on')); ?>
<?php echo $form->error($model, 'fullName') ?>
</div>
<div class="row">
<?php $form->labelEx($model, 'email'); ?>
<?php echo Yii::t('translation', 'Email')."<span class=\"required\"> *</span><br>\n"; ?>
<?php echo $form->textField($model, 'email', array('autocomplete' => 'on')); ?>
<?php echo $form->error($model, 'email'); ?>
</div>
<div class="row">
<?php $form->labelEx($model, 'phonenumber'); ?>
<?php echo Yii::t('translation', 'Phone number') . "<br>\n"; ?>
<?php echo $form->textField($model, 'phonenumber', array('autocomplete' => 'off')); ?>
<?php echo $form->error($model, 'phonenumber'); ?>
</div>
</div>
<div class="row submit">
<?php
echo CHtml::submitButton(Yii::t('translation', 'Submit'));
?>
</div>
<?php $this->endWidget();
echo CHtml::closeTag('div') . "<br/>\n";
?>
Best Wishes
Juha Ruokolainen