hopefully a quick question here, have not been able to find out the answer on google. Anyway I have the following File upload field.
<div class="row">
<?php echo CHtml::form('', 'post', array('enctype' => 'multipart/form-data')); ?>
<?php echo $form->labelEx($model, 'doc'); ?>
<?php echo CHtml::activeFileField($model, 'doc'); ?>
<?php echo $form->error($model, 'doc'); ?>
</div>
The question I have is how to change the default text? On the page in question the upload field comes up with "No file chosen" beside the button when the page loads. I wish to change this text, anyone tell me how? Thanks.
I quick & dirty fix is this:
<?php echo CHtml::activeFileField($model, 'type',array('style'=>'width:100px;', 'onchange' => 'this.style.width = "100%";')); ?>
Related
I am using YII2 advanced and I am trying from some hour but not any code working for me. I want to create dropdown from user table in my post page. I have found this error Class 'yii\models\user' not found. i have created this post with GII. my _form.php code
<?php
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\models\user;
use yii\widgets\ActiveForm;
/* #var $this yii\web\View */
/* #var $model app\models\Posts */
/* #var $form yii\widgets\ActiveForm
<?php/* <?= $form->field($model, 'id')->textInput(['value' => \Yii::$app->user->identity->id]) ?>
use yii\models\user;
*/
?>
<div class="posts-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'post_title')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'post_description')->textarea(['rows' => 6]) ?>
<?php
echo Html::activeDropDownList($model, 'author_id',ArrayHelper::map(User::find()->all(),'id','username'),['prompt'=>'Select User']);
?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Why not working please anyone help me.
Thanks in advence
User::find() shows that you would use a User model, so really in uppercase as scaisEdge has answered to you.
However you have to know, in what namespace your User model is sitting. Yii2 doesn't have a User model, you have to create it. You mentioned, that you are using the advanced template. In that case you should create a User model in 'common/models/User.php' instead of 'models/User.php'. Follow the description in the Yii2 Guide.
In php typically a class name begin with an Uppercase char so in a unix like env (with file system based on a case sensitive constrain ) this must match try
use yii\models\User;
I have the following piece of code which I use to display a downloadable link only if the user is logged in. The code used to work.
After the user is logged in, the link tells me I still need to login. I expect it has to do with formkey.
How can I go about fixing this?
<?php if(!Mage::getSingleton('customer/session')->isLoggedIn()): /*If user is not logged in*/ ?>
<?php $registerUrl = $this->getData('registerUrl'); /** generate your register URL*/ ?>
<div>
<?php echo "<a href='$registerUrl'>Please login to download, gelieve eerst in te loggen, Bitte erst Anmelden </a>"; /*Ask user to Register*/ ?>
</div>
<?php else: /*If user logged in*/ ?>
<?php $downloadUrl = $this->getData('downloadUrl');/**generate your download URL */ ?>
<div>
<?php echo "<a href='$downloadUrl'>Link</a>"; /* Allow user to download */ ?>
</div>
<?php endif; ?>
I found the solution. I added
<?php Mage::getSingleton('core/session')->getFormKey() ?>
Just before
<?php if(!Mage::getSingleton('customer/session')->isLoggedIn()): /*If user is not logged in*/ ?>
I tried to make a dropdown list in yii2 using this link : How to make a drop down list in yii2?
my code is :
<?php use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>
<?= $form->field($model, 'parent_id')-> dropDownList($listdata); ?>
but I have a problem in line of using ArrayHelper
the problem is:
PHP Notice – yii\base\ErrorException
Array to string conversion.......! I tested the below code :
$listData=ArrayHelper::map(Product::find()->asArray()->all(),'id','name');
but it dos not solved and has the same error!
whats the problem? can somebody help me?
You are trying to echo an array, change <?= to <?php in:
<?= $listdata=ArrayHelper::map(Product::find()->all(),'id','name'); ?>
Try like this
<?php
use yii\helpers\ArrayHelper;
use app\models\Product;
?>
<?= $form->field($model, 'parent_id')->dropDownList(
ArrayHelper::map(Product::find()->all(),'id','name'),
['prompt'=>'Select '])
?>
How do I change the header after login, or is using another header even the right way to do it? Meaning there would be two different headers(guest/user). I've searched for it, mostly about redirects of the entire page, not what I am looking for.
Sorry for the noob question. :/
Make two files in your view with guest_header.php and user_header.php when user is logged in user user_header.php as your header file in your layout or simply use guest_header.php
and in your column layout use it like below
when user is logged in
<?php if(Yii::app()->user->id): ?>
<?php $this->beginContent('//layouts/user_header.php'); ?>
when user is guest
<?php else: ?>
<?php $this->beginContent('//layouts/guest_header.php'); ?>
<?php endif; ?>
Added this line
<?php $this->endContent(); ?>
<?php $this->beginContent('//layouts/main'); ?>
<?php echo $content; ?>
</div><!-- content -->
<?php $this->endContent(); ?>
I have 10 pages, 4 of which should be accessible by logged in users.
Is there a plugin that exists to password protect these pages? Ideally, you can login once and then subsequently view all these protected pages.
I have Googled a bit, but haven't been able to find something that lets you protect individual pages, only the entire WordPress platform.
Does something like this exist, and if there are multiple, which is the best in your experience?
Thanks
You could use a Membership Plugin. Most of them cost money. A Google search turned up a free one called MemberWing, but I haven't tried it and can't speak to how good it is.
Generally a membership plugin will handle registration and access control. If you decide that's overkill, you could focus just on content protection with a Custom Page Template. Below, I've attached a generic WordPress Page Template that will hide content from guests. It's based off of WordPress' old default theme. If the user is not logged in, it'll display a message telling them they can't access the content as a guest.
<?php
/*
Template Name: Protected Content
*/
get_header(); ?>
<div id="content" class="narrowcolumn" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php
if(is_user_logged_in()) {
the_content('<p class="serif">Read the rest of this page »</p>');
}
else {
echo "You must be logged in to access this content!";
}
?>
<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
</div>
</div>
<?php endwhile; endif; ?>
<?php edit_post_link('Edit this entry.', '<p>', '</p>'); ?>
<?php comments_template(); ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Private posts should do what you want. You shouldn't need a plugin.