Yii Captcha error - php

I am working in Yii and I am just a beginner and trying my best to learn the framework and here is where I am stuck at :
I have created a user model and the required forms that go with it, and I am trying to implement the Captcha for it :
This is my validation rules in the user model :
$public verifyCode
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('username, password, email', 'required'),
array('username','unique'),
array('email','email'),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
array('username, password', 'length', 'max'=>45),
array('email', 'length', 'max'=>100),
array('active', 'length', 'max'=>1),
array('created_on, updated_on', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, username, password, email, created_on, updated_on, active', 'safe', 'on'=>'search'),
);
}
And this is my overriden action() in my userController :
public function actions(){
return array(
'captcha'=>array(
'class' => 'CCaptchaAction',
)
);
}
And this is my view file :
<?php if(CCaptcha::checkRequirements()): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">Please enter the letters as they are shown in the image above.
<br/>Letters are not case-sensitive.</div>
<?php echo $form->error($model,'verifyCode'); ?>
</div>
<?php endif; ?>
According to me, I think that I am doing everything correctly however, the captcha image is not getting generated. Oh and yes the GD library is installed and if I navigate to the site/contact, there the captcha is generated fine.
I dont seem to understand, where am i getting it wrong.
This is the thing that I see :
The forms seems to be working fine however, I cant see the the captcha image.
Any help would be appreciated.
Regards,

I got the answer, it is because of the access rules that are defined in the controller, I had to modify the controller accessControl like so :
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','captcha'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform every action
'actions'=>array('create','update','admin','delete'),
'users'=>array('#'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}

Related

Yii 1.x - required field saves empty

I have a field set as required in the model, yet I have seen users saving it as an empty string (ie. ''). When I tested it, I do receive the "Cannot be blank" message properly, so don't know how to prevent this in the future. Do I have to specify all scenarios in the rule (eg, 'insert', 'update')? By the way, I tried updating the field, and it doesn't let me save it empty (I even tries spaces).
These are the rules applied on the field (model):
public function rules()
{
return array(
array('field', 'required'),
array('field', 'length', 'max'=>4096),
array('field', 'safe', 'on'=>'search'),
);
}
For #RiggsFolly :) the controller action:
public function actionUpdate($id)
{
$model = Model::model()->findByPk($id);
$formData = Yii::app()->request->getPost('Model');
if ($formData)
{
$model->attributes = $formData;
$model->save();
}
$this->render('update',array(
'model'=>$model
));
}
... and the view:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'form'
)); ?>
<?php echo $form->textArea($model,'text',array( 'rows'=>5 ')); ?>
<?php $this->endWidget(); ?>
Can you imagine any scenario this field could be saving an empty string in the database?

upload file in Yii not showing image name/path

im here again with a new problem. I am trying to upload a file using yii upload function.
Everything saves well, exept for the image. Here's my code:
Controller:
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$dir = Yii::getPathOfAlias('webroot.images.producten');
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Producten']))
{
$model->attributes=$_POST['Producten'];
$model->images=CUploadedFile::getInstance($model,'images');
$nf = $model->images;
if($model->save()){
$this->redirect(array('view','id'=>$model->id));
$model->images->saveAs($dir.'/'.$nf);
$model->images = $nf;
$model->save();
}
}
$this->render('update',array(
'model'=>$model,
));
}
Form:
<div class="row">
<?php echo $form->labelEx($model,'images'); ?>
<?php echo CHtml::activeFileField($model,'images'); ?>
<?php echo $form->error($model,'images'); ?>
</div>
Model:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('naam, beschrijving, prijs', 'required'),
array('images,', 'file', 'allowEmpty'=>true,
'safe' => true,
'types'=> 'jpg, jpeg, png, gif',
'maxSize' => (1024 * 300), // 300 Kb
),
array('aangepast_door', 'numerical', 'integerOnly'=>true),
array('naam', 'length', 'max'=>50),
array('prijs, actieprijs', 'length', 'max'=>10),
//array('toegevoegd, aangepast', 'safe'),
// The following rule is used by search().
// #todo Please remove those attributes that should not be searched.
array('naam, beschrijving, prijs, actieprijs', 'safe', 'on'=>'search'),
);
}
Please help me get this to work.
First of all add enctype= "multipart/form-data" to your form tag or add "enctype" option to associative array if you used form widget yo begin form.
If it will not helps you, please post var_dump($_POST) results here

Comapring rule between two variables in rules not working in Yii

I am doing a function to change the users password. Inside of the Users model I have create three variable like the following.
public $oldPassword;
public $newPassword;
public $repatePassword;
Now I need to compare newPassword and repare password together and i added the followinf rule inside of the model.
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('username, password', 'required'),
array('username', 'length', 'max'=>20),
array('password', 'length', 'max'=>255),
array('oldPassword', 'findPassword', 'on' => 'changePwd'),
//array('repatePassword','compare','compareAttribute'=>'newPassword', 'on'=>'changePwd'),
// The following rule is used by search().
// #todo Please remove those attributes that should not be searched.
array('id, username, password', 'safe', 'on'=>'search'),
);
}
I have the following form.
<div class="row">
<?php echo $form->labelEx($model,'Old Password'); ?>
<?php echo $form->passwordField($model,'oldPassword',array('size'=>20,'maxlength'=>20)); ?>
<?php echo $form->error($model,'oldPassword'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'New Password'); ?>
<?php echo $form->passwordField($model,'newPassword',array('maxlength'=>255)); ?>
<?php echo $form->error($model,'newPassword'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Repate Password'); ?>
<?php echo $form->passwordField($model,'repatePassword',array('maxlength'=>255)); ?>
<?php echo $form->error($model,'repatePassword'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Update'); ?>
</div>
Now when I exactly put the exc same values in both of the fields it still gives me that the values are not the same. Why is this?
Add the following rule to your rules() method.
array('oldPassword,newPassword,repatePassword','required','on'=>'changePwd')
Without a rule for an attribute, that attribute will NOT be massively assigned to the model and so will not be available for any validation.
Alternately, you can change required to be safe if you just want to allow assignment, and not make the attributes actually required, although in this case required makes sense to me.
Based on your rules, the reason it says they do not match is because newPassword is not being assigned, and so is an empty string being compared to your intended password.

Passing temporary data back to hidden field in Yii

I'm trying to pass a temporary data from my form to my model. My form has a hidden data in it, but it does not get any values from my model.
My form is
<?php echo $form->hiddenField($model, 'fieldName'); ?>
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name'); ?>
<?php echo $form->error($model,'name'); ?>
I threw in the data for my hidden field using JQuery, and then I proceeed to validate my form.
public function rules()
{
return array(
array('name, email, subject, phone, body', 'required','message' => '{attribute} Required'),
array('resume', 'file', 'types'=>'txt,pdf,doc,docx', 'maxSize'=>2097152, 'tooLarge'=>'File has to be smaller than 2MB','allowEmpty'=>false),
array('email', 'email'),
);
}
Whenever the validation hits an exception, it returns all of the $_POST data I inputted except the data I sent from the hidden form.
How do I get the $_POST so it gets back to the hidden form?
First:
The attribute must be declared as public in the model.
public $fieldName;
Add in rules() of Model the field with a safe mode array('fieldName','safe') :
public function rules()
{
return array(
array('name, email, subject, phone, body', 'required','message' => '{attribute} Required'),
array('resume', 'file', 'types'=>'txt,pdf,doc,docx', 'maxSize'=>2097152, 'tooLarge'=>'File has to be smaller than 2MB','allowEmpty'=>false),
array('email', 'email'),
array('fieldName','safe'), // Add the custom field to 'safe' mode.
);
}
Is 'fieldName' part of the model/table? If not and it is just a custom property declared in the model, then try setting it as 'safe'

Yii captcha picture Broken link

I just wondering how it is possible to copy exact code of Yii Framework demo-blog-contact , downloaded from Yii website to other yii application with same version library and it don't show captcha.
When i go to controller/action page ,it show broken link instead of captcha and when i open broken link image in new page it show another broken link.
I saw this link for common problem of CCaptcha and i checked gd library is up and running and i have function actions() with content of exact same as the link said and i don't define any access control filter in the application.
I saw this link and i don't have permission problem and this link and i don't ajax it.
I did exact same as this link said but no success. any help to show these captcha would be appreciated.
Controller :
public function actions()
{
return array(
'captcha'=>array(
'class'=>'CCaptchaAction',
'backColor'=>0xFFFFFF,
),
'page'=>array(
'class'=>'CViewAction',
),
);
}
public function accessRules()
{
return array(
array('allow',
'actions'=>array('create', 'captcha'),
'users'=>array('*'),
));
}
Model :
<?php
class ContactsM extends CFormModel {
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
// name, email, subject and body are required
array('name, email, subject, body', 'required'),
// email has to be a valid email address
array('email', 'email'),
// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
);
}
public function attributeLabels()
{
return array(
'verifyCode'=>'Verification Code',
);
}
}
View :
<?php $form=$this->beginWidget('CActiveForm'); ?>
<?php echo $form->errorSummary($model); ?>
<?php if(extension_loaded('gd')): ?>
<div class="row">
<?php echo $form->labelEx($model,'verifyCode'); ?>
<div>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode'); ?>
</div>
<div class="hint">Please enter the letters as they are shown in the image above.
<br/>Letters are not case-sensitive.</div>
</div>
<?php endif; ?>
<div class="row submit">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
As i understand from your comment You should Extend your Controller class from CController.
I want to share the fact i have understand :
My problem was :
I used inheritances and father class have some issue , the son class extend it but it do not show any error and i was thinking it works fine but in fact it didn't , i understand it till i used some of father function which in this case was CCaptcha.

Categories