Set Field Empty When Change Password in Yii - php

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]','')

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.

Passing combobox selected value to a dialog using ajaxlink

I' trying to pass the dropDownList selected value to this Dialog.
Any ideas on how I can do this?
I tried adding another parameter to the ajaxlink, using array('id'=>'showEventoDialog','tipoaux'=>$data["tipo"]), or only $data->tipo but can't seem to do what I want.
I'm also trying to get the value via $_GET from the Dialog form.
Here's my form and the Dialog link within the form
<?php echo $form->labelEx($model,'tipo'); ?>
<?php echo $form->dropDownList($model,'tipo',Lookup::items('Teste')); ?>
<?php echo $form->error($model,'tipo'); ?>
...
<?php echo $form->labelEx($model,'eventoid'); ?>
<div id="evento">
<?php echo $form->dropDownList($model,'eventoid',CHtml::listData(Evento::model()->findAll(),'id', 'designacao'),array('prompt'=>'Escolha','class'=>'required')); ?>
<?php echo CHtml::ajaxLink(Yii::t('evento','Novo Evento'),$this->createUrl('evento/addnewcom'),array(
'onclick'=>'$("#eventoDialog").dialog("open"); return false;',
'update'=>'#eventoDialog'
),array('id'=>'showEventoDialog'));?>
<div id="eventoDialog"></div>
</div>
Any ideas on how to do this?
Plus will the solution work with any other type of value, like textfield or something else on my form, so I can pass the values to dialogs BEFORE the parent form is submitted.
You can hook up some code to the open event of the dialog that will fire right before the dialog appears. In this code you can query the selected option and write it to the dialog:
<?php echo $form->labelEx($model,'eventoid'); ?>
<div id="evento">
<?php echo $form->dropDownList($model,'eventoid',CHtml::listData(Evento::model()->findAll(),'id', 'designacao'),array('prompt'=>'Escolha','class'=>'required')); ?>
<?php echo CHtml::ajaxLink(Yii::t('evento','Novo Evento'),$this->createUrl('evento/addnewcom'),array(
'onclick'=>'$("#eventoDialog").dialog({open: function(){ $("#selectedvalue").text($("#eventoid").val()); }}) .dialog("open"); return false;',
'update'=>'#eventoDialog'
),array('id'=>'showEventoDialog'));?>
<div id="eventoDialog">
<span>Selected value: </span><span id="selectedvalue" />
</div>
</div>

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')));?>

Yii framework radiobuttonlist

Can anyone shine a light on how to determine which radio button has been selected when the form has been submitted?
I am using CActiveForm::radioButtonList?
You don't need to determine it. Client will transmit its value in POST data.
For example such code
<?=$form->radioButtonList($person,'gender_code',array('m'=>'Male','f'=>'Female')); ?>
will form POST[gender_code]=m or POST[gender_code]=f
Radio List Reflects simple form Submitting process. If you have following list implementation for example
<div class="form">
<?php echo CHtml::beginForm(); ?>
<div class="row">
<?php
echo CHtml::radioButtonList(
'registerMode',
'consumer',
array(
'consumer'=>'I am a FOODIE ',
'staff'=>'I want to give Services ',
),
array('template'=>'<div class="rb">{input}</div><div class="rb">{label}</div><div class="clear"> </div>')
);
?>
</div>
<div class="row">
<?php echo CHtml::submitButton('Register',array('class'=>'submit')); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->
when submitted following input is generated
array
(
'registerMode' => 'consumer'
'yt0' => 'Register'
)
it represents name or Index of the option Selected
following code can get values
if(isset($_POST['registerMode']))
CVarDumper::Dump($_POST['registerMode'],100,true);
Good Luck

Yii client side validation doesn't work

There is a page with a link inside the "body" tag.
When the link is inserted the following view is rendered in fancybox.
<div class="form">
<?php
$model = new User;
$model->scenario='registration';
$form=$this->beginWidget('CActiveForm', array(
'id'=>'signup-form',
'action'=>'/signup/',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('sign up',array('name'=>'submit')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
When the above code is inserted without fancybox the client validation script is inserted correctly, but when in fancybox there are no jQuery validation functions inserted to the page.
What can be the problem?
(siteController code is inserted here on purpose)
Your problem is most likely how you send the HTML to Fancybox. If you do a renderPartial (as alluded to by user1248203), you need to make sure you have renderPartial postprocess the view that is sent back (post-processing includes javascript/css files that you want rendered along with the view itself).
More info on the CController page
One other thing to note: when you do the post-processing, you will need to make sure that the Javascript for your Fancybox (and possibly jQuery, etc) isn't sent back again. It can cause some really confusing problems. To keep the files from being re-sent over Ajax, use this:
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;
Yii::app()->clientscript->scriptMap['jquery-ui.min.js'] = false;
etc.
Also note that jquery.js is sent when you're debugging on a dev server (with YII_DEBUG set to true), but jquery.min.js is sent on production servers (where YII_DEBUG is set to false). That's bitten me :-) You can see it defined in framework/web/js/packages.php
Is the code called from an Ajax call (renderPartial) or is it called direct? Ajax calls (renderPartial) do not include the javascript validation function, only the whole page does.

Categories