Hello all,
I’m trying to create a registration widget for the yii user management so i can display it with a modal dialog. So far i managed to create and display the dialog, but when i submit the form it shows me an error saying that the captcha code is wrong. Here’s My code:
Myregistrationcontroller.php
<?php
Yii::import('application.modules.registration.controllers.YumRegistrationController');
class MyregistrationController extends YumRegistrationController {
public function actionRegistration() {
// When we override the registrationUrl, this one is not valid anymore!
if(Yum::module('registration')->registrationUrl != array(
'//registration/registration/registration'))
throw new CHttpException(403);
Yii::import('application.modules.profile.models.*');
$form = new YumRegistrationForm;
$profile = new YumProfile;
$this->performAjaxValidation('YumRegistrationForm', $form);
if (isset($_POST['YumRegistrationForm'])) {
$form->attributes = $_POST['YumRegistrationForm'];
$profile->attributes = $_POST['YumProfile'];
$form->validate();
$profile->validate();
if(!$form->hasErrors() && !$profile->hasErrors()) {
$user = new YumUser;
$user->register($form->username, $form->password, $profile);
$this->sendRegistrationEmail($user);
Yum::setFlash('Thank you for your registration. Please check your email.');
$this->redirect(Yum::module()->loginUrl);
}
}
$this->render('myregistration', array(
'form' => $form,
'profile' => $profile,
)
);
}
}
myregistration.php
<h2> <?php echo Yum::t('Registration'); ?> </h2>
<?php $this->breadcrumbs = array(Yum::t('Registration')); ?>
<div class="form">
<?php $activeform = $this->beginWidget('CActiveForm', array(
'id'=>'registration-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($form,'username'),
));
?>
<?php echo Yum::requiredFieldNote(); ?>
<?php echo CHtml::errorSummary(array($form, $profile)); ?>
<div class="row"> <?
echo $activeform->labelEx($form,'username');
echo $activeform->textField($form,'username');
?> </div>
<div class="row"> <?
echo $activeform->labelEx($profile,'email');
echo $activeform->textField($profile,'email');
?> </div>
<div class="row"> <?
echo $activeform->labelEx($profile,'firstname');
echo $activeform->textField($profile,'firstname');
?> </div>
<div class="row"> <?
echo $activeform->labelEx($profile,'lastname');
echo $activeform->textField($profile,'lastname');
?> </div>
<div class="row">
<?php echo $activeform->labelEx($form,'password'); ?>
<?php echo $activeform->passwordField($form,'password'); ?>
</div>
<div class="row">
<?php echo $activeform->labelEx($form,'verifyPassword'); ?>
<?php echo $activeform->passwordField($form,'verifyPassword'); ?>
</div>
<?php if(extension_loaded('gd')
&& Yum::module('registration')->enableCaptcha): ?>
<div class="row">
<?php echo CHtml::activeLabelEx($form,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo CHtml::activeTextField($form,'verifyCode'); ?>
</div>
<p class="hint">
<?php echo Yum::t('Please enter the letters as they are shown in the image above.'); ?>
<br/><?php echo Yum::t('Letters are not case-sensitive.'); ?></p>
</div>
<?php endif; ?>
<div class="row submit">
<?php echo CHtml::submitButton(Yum::t('Registration')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
RegistrationWidget.php
<?php
class RegistrationWidget extends CWidget
{
public $title='Registration';
public $visible=true;
public function init()
{
if($this->visible)
{
}
}
public function run()
{
if($this->visible)
{
$this->renderContent();
}
}
protected function renderContent()
{
Yii::import('application.modules.profile.models.*');
$form = new YumRegistrationForm;
$profile = new YumProfile;
if (isset($_POST['YumRegistrationForm'])) {
$profile->attributes = $_POST['Profile'];
$profile->validate();
if(!$profile->hasErrors()) {
$user = new YumUser;
$user->register($profile->username, $profile->password, $profile);
$this->redirect(Yum::module()->loginUrl);
}
}
$this->render('Registration', array('form' => $form,'profile' => $profile));
}
}
registration.php
<?php if(Yii::app()->user->isGuest) : ?>
<?php
if($form->getErrors() != null) {
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'userloginwidget',
'cssFile'=>'jquery-ui-1.8.7.custom.css',
'theme'=>'redmond',
'themeUrl'=>Yii::app()->request->baseUrl.'/css/ui',
'options'=>array(
'title'=>'Registration Errors',
'autoOpen'=>true,
'modal'=>true,
'width'=>350,
),
));
}else{
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'userloginwidget',
'cssFile'=>'jquery-ui-1.8.7.custom.css',
'theme'=>'redmond',
'themeUrl'=>Yii::app()->request->baseUrl.'/css/ui',
'options'=>array(
'title'=>'Registration',
'autoOpen'=>false,
'modal'=>true,
'width'=>300,
),
));
}
?>
<div class="form">
<?php echo CHtml::beginForm(Yii::app()->baseUrl.'/index.php/myregistration'); ?>
<div class="row"> <?
echo CHtml::activeLabel($form,'username');
echo CHtml::activeTextField($form,'username');
?> </div>
<div class="row"> <?
echo CHtml::activeLabel($profile,'email');
echo CHtml::activeTextField($profile,'email');
?> </div>
<div class="row"> <?
echo CHtml::activeLabel($profile,'firstname');
echo CHtml::activeTextField($profile,'firstname');
?> </div>
<div class="row"> <?
echo CHtml::activeLabel($profile,'lastname');
echo CHtml::activeTextField($profile,'lastname');
?> </div>
<div class="row">
<?php echo CHtml::activeLabel($form,'password'); ?>
<?php echo CHtml::activeTextField($form,'password'); ?>
</div>
<div class="row">
<?php echo CHtml::activeLabel($form,'verifyPassword'); ?>
<?php echo CHtml::activeTextField($form,'verifyPassword'); ?>
</div>
<?php if(extension_loaded('gd') && Yum::module('registration')->enableCaptcha): ?>
<div class="row">
<?php echo CHtml::activeLabelEx($form,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo CHtml::activeTextField($form,'verifyCode'); ?>
</div>
<p class="hint">
<?php echo Yum::t('Please enter the letters as they are shown in the image above.'); ?>
<br/><?php echo Yum::t('Letters are not case-sensitive.'); ?></p>
</div>
<?php endif; ?>
<div class="row submit">
<?php echo CHtml::submitButton(Yum::t('Registration')); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div>
<?php $this->endWidget('zii.widgets.jui.CJuiDialog'); ?>
<?php endif; ?>
main.php
...
<body>
<script type="text/javascript">
function doStuff()
{
$("#yw1_button").trigger("click");
$("#userloginwidget").dialog("open");
return false;
}
</script>
<div class="container" id="page">
<div id="header">
<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>
</div><!-- header -->
<div id="mainmenu">
<?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('/user/auth'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest),
array('label'=>'Registration', 'url'=>'#','linkOptions'=>array( 'onclick'=>'doStuff();'), 'visible'=>Yii::app()->user->isGuest),
),
)); ?>
</div><!-- mainmenu -->
<?php $this->widget('RegistrationWidget',array('visible'=>Yii::app()->user->isGuest)); ?>
...
The doStuff() is used for both opening the dialog and to refresh the captcha every opening.
Do you have any hint?
Thanks