Got server error trying to display image with Yii - php

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.

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.

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

Weird captcha in Yii popup

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.

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.

fill data to the database from comment form of post view in yii

i have already the comment form bellow my post view , but the problem is that it can not be store the data by comment form to the database which is bellow post view.
My code for comment form....
<h5>Add your Comment</h5>
<?php if(Yii::app()->user->hasFlash('commentSubmitted')): ?>
<div class="flash-success">
<?php echo Yii::app()->user->getFlash('commentSubmitted'); ?>
<?php else: ?>
<?php $comment= new Comment();
$this->renderPartial('/comment/_form',array('model'=>$comment,
)); ?>
and my code of _form is
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'comment-form',
'enableAjaxValidation'=>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,'content'); ?>
<?php echo $form->textArea($model,'content',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'content'); ?>
</div>
i want to store data from the comment form which is in the post view.
I'm not 100% sure what you are asking here, but I think you are asking how to save the data from the form submit? In you controller, in the action that renders the comment form, use:
$model = new Comment;
if(isset($_POST['Comment'])){
$model->attributes=$_POST['Comment'];
$model->save();
}
You should check out the gii tool (http://yiitutorials.net/easy/using-yiis-gii-tool), it will help you generate your models, forms and action to save the form data. Hope that answered your question, apologies if it didn't!
On the downloaded source code you can find blog demo, and it has great example howto use comments :D

Categories