Weird captcha in Yii popup - php

I have a problem with captcha in my YII project. I included a captcha in popup form. It's showing correctly, but there's a problem with the validation. The validation is true sometimes, if we entered the correct word in first attempt, the validation says it is wrong, after generating another code the validation succeeds. Why?
In model-> rules:
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=> 'signup')
In controller -> calling the signup form via renderPartial
$this->renderPartial('signUp',array('model'=>$model),false,true);
In view :
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'sign-up-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
-----------------------
-----------------------
<?php if(CCaptcha::checkRequirements()): ?>
<?php echo $form->labelEx($model, 'verifyCode', array('for'=>'User_security_code')); ?>
<?php $this->widget('CCaptcha'); ?>
<?php echo $form->textField($model,'verifyCode',array('class'=>'txt-style width-289')); ?>
Please enter the letters as they are shown in the image above. Letters are not case-sensitive.
<?php echo $form->error($model,'verifyCode',array('class'=>'error_msg')); ?>
<?php endif; ?>

i added this in view
<?php
Yii::app()->clientScript->registerScript(
'initCaptcha',
'$("#yw0_button").trigger("click");',
CClientScript::POS_READY);
?>
and in
`CCaptchaAction.php` updated `$testLimit = 0;` //for unlimted test`
and the problem solved. I'm not sure about this is the right way, but for my current scenario it helped.

Related

Yii: from a view, how do I found out where a variable came from?

In a Yii project passed to me, there's a function that creates (or shows?) a textbox when the button/link Comment is clicked. From there, the user can create Comments, which will be displayed in a row.
I'm trying to see if I can create an edit comment function, so I thought I could go about this by copying the Comment function - it'll show a textbox, and the user can input the new text in there. And instead of adding a new comment, it will edit the existing one.
But I hit a snag, as apparently the view.php makes use of a variable that I couldn't for the life of me figure out how to use in _comments.php - the file responsible for displaying the individual comments, afaik.
Here's the code for view.php:
</script>
<?php
$this->breadcrumbs=array('Forums'=>array('index'),$model->title,);
?>
<?php
if(Yii::app()->user->hasFlash('message')):
echo '<script>alert("'.Yii::app()->user->getFlash('message').'");</script>';
endif;
?>
<?php $starter = Persons::model()->findByAttributes(array('party_id'=>$model->party_id));?>
<div id="forum_main_box">
<div class="comment-icon-textbox">
<?php echo CHtml::ajaxLink('Comment',array('forum/callcommentform'),
array(
'update' => '#render_div'.$model->id,
'data'=>array('id'=>$model->id),
)); ?>
</div>
<?php endif; ?>
<div id="forum_comment_headerbox">
</div>
<div>
<?php
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$dataProvider,
'itemView'=>'_comments',
'summaryText'=>'',
));
?>
<div id="render_div<?=$model->id?>" class="comment-form">
</div>
</div>
</div>
Of that code, below is the Comment link I spoke of:
<?php echo CHtml::ajaxLink('Comment',array('forum/callcommentform'),
array(
'update' => '#render_div'.$model->id,
'data'=>array('id'=>$model->id),
)); ?>
<?php } ?>
This block displays the list of comments, and what (I assume to be) the space where the textbox will pop up when the above Comment is clicked:
<?php
$this->widget('zii.widgets.CListView',
array(
'dataProvider'=>$dataProvider,
'itemView'=>'_comments',
'summaryText'=>'',
));
?>
<div id="render_div<?=$model->id?>" class="comment-form">
</div>
Notice that both makes use of $model. It first appeared in the code as $model->title.
And here's a shortened version of the _comments.php, which is used for the comment rows and the comment box.
<?php $comment = $data; ?>
<div class="other-member-comment-box">
<?php $person=Persons::model()->findByAttributes(array('party_id'=>$comment->party_id)); ?>
<?php
$country=Lookup_codes::model()->findByAttributes(array('id'=>$person->country));
$location = empty($country) ? '' : ' - '.$country->name;
// $model->title;
?>
<?php if (Yii::app()->user->id == $person->party_id || Yii::app()->partyroles->isAdmin()) {
?>
<p class="admin-commands">
<?php echo CHtml::link(CHtml::encode('Edit'),array('forum/editcomment','reply'=>$data->id,'topic'=>$data->content_id)); ?>
<?php echo CHtml::ajaxLink('EditTestComment',array('forum/callcommentform'),array('update' => '#render_div'.$model->id,'data'=>array('id'=>$model->content_id),)); ?>
<?php echo CHtml::link(CHtml::encode('Delete'),array('forum/delete','reply'=>$data->id,'topic'=>$data->content_id),array('confirm'=>'Are you sure you want to delete this item?')); ?>
<div id="render_div<?=$model->id?>" class="comment-form">
</div>
</p>
<?php } ?>
</div>
Under <p class="admin-commands">, there's a EditTestComment link which is a straight up copy of the Comment code from the view.php. This doesn't work, of course, because of this:
2016/04/07 10:24:03 [error] [php] Undefined variable: model
Where'd $model come from in view.php? Because putting in the same line ($model->title) anywhere in _comments.php just breaks it further.
EDIT: Here's the CallComment part of the controller:
public function actionCallCommentForm($id='')
{
$topic=Forum::model()->findByPk($id);
$this->renderPartial('_commentform', array(
'forum'=>$topic,
'model'=>new Comment,
//'view'=>array('view','id'=>$id),
'view'=>'view',
));
}
$model variable is coming from your controller initially. It is an instance of a Comment class which gets passed to the view via $this->render('view', array('model'=>$whatever)). This line will make $whatever available in the forum/view.php file under the name of $model. Now since you are dealing with partial views you have to track it because it is possible that the same $model variable will be passed to another partial view with $this->renderPartial('_comment', array('whatever'=>$model)) and now this will be accessible in partial view as $whatever.

stay on the same page after error validation in yii

I would like to avoid redirecting after error validation.
I display a list of post and for each post the user can give a comment after clicking on a button.
This display the comment form under the choosen post.
If there is an error in validation of the comment I want to stay on the page and display errors beside the field in error (as default).
Here is My controller action for the comment
public function actionCreate()
{
$model=new Comment;
if(isset($_POST['Comment']))
{
$model->attributes=$_POST['Comment'];
if($model->save()) {
$this->redirect(Yii::app()->request->urlReferrer);
} else {
Yii::app()->end();
}
}
$this->render('create',array(
'model'=>$model,
));
}
and the view
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'comment-form',
'enableClientValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'comment'); ?>
<?php echo $form->textField($model,'comment',array('size'=>60,'maxlength'=>140)); ?>
<?php echo $form->error($model,'comment'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
For the moment, with Yii::app()->end(); it shows a blank page, if I dont do nothing in the else part , then it continues and displays the create view (loosing some decoration)
---Adding some more information
Actually, what I have in my page is this
Post 1
(Comment form)
Commment : ........
Save
Post 2
Post 3
when I click on save without comment, I want to stay in this page, giving the user the possibility to see where the error is (comment is missing) and save it again.
Can you tell me where is my mistake?
Thank you for your help
in the else block you have to tell yii to render the same page again. After calling $model->save() you get errors back in the model. So before Yii::app()->end(); call rendering of your page $this->render('create',array('model'=>$model,));

Set Field Empty When Change Password in Yii

I want to set empty this field:
This is my code from _form.php
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'password'); ?>
</div>
how to that, please teach me. Thanks all
You can explicitly set the value to be an empty string like this:
<?php echo $form->passwordField($model,'password',array(
'size'=>50,
'maxlength'=>50,
'value'=>'',
)); ?>
Where you are starting your form, you can add htmlOptions like this;
'htmlOptions' => array(
'autocomplete' => 'off'
)
This will disable autocomplete for the whole form. If you just want to do it for a single field, add it to the htmlOptions for that file instead.
This strange behaviour is happen for me too.
I fix it with javascript code to set the password field value to empty.
Yii code:
<div class="row">
<?php echo $form->labelEx($model,'newpassword'); ?>
<?php echo $form->passwordField($model,'newpassword',array('size'=>60,'maxlength'=>128,'value'=>'')); ?>
<?php echo $form->error($model,'newpassword'); ?>
</div>
Javascript code (I am using jQuery) :
<script>
$(document).ready(function()
{
$("#ChangePasswordForm_newpassword").val("");
});
</script>
echo CHtml::passwordField('User[password]','')

Got server error trying to display image with Yii

I have a problem that I can't get displayed picture with yii. Add 'enctype' form, but that doesn't helped.
This is in view:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'activity_form',
'enableAjaxValidation'=>true,
'stateful'=>true,
'htmlOptions'=>array('enctype' => 'multipart/form-data')
)); ?>
<div class="row">
<?php echo $form->labelEx($model,'pic'); ?>
<?php echo CHtml::activeFileField($model, 'pic'); ?>
<?php echo $form->error($model,'pic'); ?>
</div>
I got this error
Here I want to get picture displayed
<?php echo CHtml::image(Yii::app()->request->baseUrl.'/protected/prod_pictures/'.$model->pic,"pics",array("width"=>200)); ?>
<?php $this->endWidget(); ?>
I would like to have this picture visible alredy before the form is submitted (now I need to reload page), but I will try to solve it after this problem.
Any suggestions?
The folder in which you are storing images should be outside the protected folder and not in it. The protected folder is named as such since one cannot directly access its contents through a webserver by default.

Yii Submit Form in View page

I just started working with yii and I stumbled upon a problem.
I have a table called "users" and a table called "messages".
I basically want to have a page where I can view a user's details but also send him a message that would be saved in the message table.
I have a view called "user/view.php" which consists of:
<h1>View User #<?php echo $model->id; ?></h1>
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'id',
'username',
'first_name',
'last_name',
),
)); ?>
<?php $message=new Message;
echo $this->renderPartial('_messagesend', array('model'=>$message)); ?>
the _messagesend form (created using gii) looks like:
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'message-_messagesend-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
//for the sake of simplicity lets just insert the id's manually for now
<div class="row">
<?php echo $form->labelEx($model,'idFrom'); ?>
<?php echo $form->textField($model,'idFrom'); ?>
<?php echo $form->error($model,'idFrom'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'idTo'); ?>
<?php echo $form->textField($model,'idTo'); ?>
<?php echo $form->error($model,'idTo'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'msg'); ?>
<?php echo $form->textField($model,'msg'); ?>
<?php echo $form->error($model,'msg'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Send'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I have a simple view in my UserController to display the details info:
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id)
));
}
And now I want to figure out how i can add the controller to save the message. After gii creation I get a code which I tried to use and modify a little bit:
public function actionMessagesend()
{
$model=new Message;
if(isset($_POST['Message']))
{
$model->attributes=$_POST['Message'];
if($model->save()){
$this->redirect(array('admin'));
}
}
$this->render('_messagesend',array('model'=>$model));
}
I tried to add this controller function in the UserController.php but it doesn't seem to work, I tried to add the same function to MessageController.php but it also doesn't seem to work. I tried to remove all the code and only add a redirect to show if the controllercode actually hits but it doesn't redirect (i tried it both in usercontroller and messagecontroller). So I have a feeling my code isn't reached. You guys have any idea what I'm missing here?
Maybe a little extra question: Is there a better suggestion to do what I want to do?
Thanks a lot!
Best regards,
WtFudgE
Your message sending action isn't reached because by default the CActiveForm submits to the current page, so you will have to add the message processing to the view action, or you will need to set the action property for your CActiveForm.
Here is the link to the property documentation: http://www.yiiframework.com/doc/api/1.1/CActiveForm#action-detail
So the form widget in the view should be something like:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'message-_messagesend-form',
'enableAjaxValidation'=>false,
'action' => array( '/message/messageSend' ), // change depending on your project
)); ?>
You can also achieve this by editing the submit button in the view page like this:
<?php echo CHtml::button('Send', array('submit' => array('message/messageSend')));?>

Categories