Yii alert widget - php

In view I have widget:
if ($content):
echo Alert::widget([
'options' => [
'class' => 'alert-info',
],
'body' => $content,
]);
endif;
That widget I want render not always, for example after save and atc. Now Now I have placed that widget between if condition, maybe exists some more clear way to render widget only in some cases.

I think Flash-Messages is what you want:
For Example:
In the controller you can do something like that:
<?php
Yii::app()->user->setFlash('success', "Data saved!");
$this->redirect(array('thing/view', 'id' => 1));
And in the view:
<?php if(Yii::app()->user->hasFlash('success')):?>
<div class="info">
<?php echo Yii::app()->user->getFlash('success'); ?>
</div>
<?php endif; ?>
And of course you can combine it with the alert-widget or with a custom-widget.
See full documentation: http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/

Related

Codeigniter: How to use form_open() in multiple view files

I am trying to get users to fill in much information so I need more than one form and page. I made a main_view.php which has a side bar on the left with links to sub1.php, sub2.php, sub3.php. On the right half of main_view.php, it displays the sub pages with corresponding forms. Part of the main_view.php looks like this:
<?php $view_path = "../../application/views/"?>
<span id="theFormChanger" >
<?php
?>
</span>
var currentPage = 0;
var subviews = ['sub1.php', 'sub2.php','sub3.php'];
$('#sub1').click(function(){
currentPage = 1;
$('#theFormChanger').load(viewpath + subviews[currentPage]);
});
Part of the code of sub view pages:
<?php echo form_open('v_controller'); ?>
<?php echo form_input(array( 'type' => 'text', 'id' => 'demail', 'name' =>'demail')); ?>
<?php echo form_input(array( 'type' => 'text', 'id' => 'dname', 'name' => 'dname')); ?>
<?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
<?php echo form_close(); ?>
For ../application/controllers/,there is a v_controller.php:
function __construct() {
parent::__construct();
}
public function index()
{
$this->load->helper('form');
$this->load->view('sub1');
$data = array(
'User_Name' => $this->input->post('dname'),
'User_Email' => $this->input->post('demail'));
}?>
Every time when I go to localhost:8000/main/main_view, the left part is fine but the right part says "Fatal error: Call to undefined function form_open() in main_view.php"
I searched around but couldn't find answers. I made sure everything is loaded in autoload.php.
Is this a routing problem? I can't directly go to view files? Please help me. Thank you!
You can load views on to a view file like so
application > views > default.php
<?php $this->load->view('template/common/header');?>
<?php $this->load->view('template/common/navbar');?>
<?php $this->load->view('template/' . $page);?>
<?php $this->load->view('template/common/footer');?>
And then on controller
<?php
class Example extends CI_Controller {
public function index() {
$data['page'] = 'common/example';
$this->load->view('default', $data);
}
}
you need to create NameOfController/NameOfMethod in the form open method in your view page.
replace our code by this one , it will be work for you.
<?php echo form_open('v_controller/index'); ?>

CHtml:Link in CListView

I am new to Yii. In one of my page I am using the below code to list company names as link.
<?php
$ads = Ads::model()->findAll();
foreach ($ads as $ad)
{
?>
<li>
<?php
echo CHtml::link($ad->company,array('/user/ads/view/id/'.$ad->id.'/')).'<br>'; ?>
</li>
<?php
}
?>
I want to change it to CListView. please somebody help me..
CListView expects an ActiveDataProvider, so you should change your code to something like this:
<?php
$ads = new Ads; // and then use the search() method to return an activedataprovider
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $ads->search(),
'itemView' => '_myview',
'id' => 'blogslistview',
));
?>
This wil render the _myview.php for each record. In the _myview.php file you can access the records attributes with $data->myattribute
So your _myview.php could look something like this:
<li>
<?php
echo CHtml::link($data->company, array('/user/ads/view/id/' . $data->id . '/'));
?>
</li>
More info:
http://www.yiiframework.com/doc/api/1.1/CListView
http://www.yiiframework.com/doc/api/1.1/CActiveDataProvider
Try below code:
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$ads,
'itemView'=>'_view',
'id'=>'blogslistview',
));
?>
Ref: http://www.yiiframework.com/wiki/229/filter-search-with-clistview/

placeholder attribute in zf2 problems

I cant get the value in the placeholder,the placeholder is empty
<div class="form_element">
<?php
//$name = $form->get('name');
$this->placeholder('name')->data="text value";
$name= $form->get('name');
echo $formLabel->openTag().$name->getOption('label')." ";
echo $this->formInput($name);
echo $formLabel->closeTag();
?>
</div>
In your code you used placeholder view helper (more about), and I don't see where you trying to get placeholder's value. It seems you asked about form input field placeholder attribute. If it true, then you must specify it as attribute. View helper placeholder is for different tasks.
Your form view helpers usage a little strange. May suggest my version of your code.
<div class="form_element">
<?php $name = $form->get('name'); ?>
<?php $name->setAttribute('placeholder', 'placeholder text'); ?>
<?php echo $formLabel($name); ?>
<?php echo $formInput($name); ?>
</div>
The better solution is to set placeholder in form element definition. For example:
<?php
use Zend\Form\Form;
class MyForm extends Form
{
public function __construct()
{
parent::__construct('<FORM_NAME>');
$this->add(array(
'name' => 'name',
'type' => 'Zend\Form\Element\Text',
'attributes' => array(
'placeholder' => '<PLACEHOLDER_TEXT>',
),
));
}
}

Yiiframework Range Slider input

I have a user settings page where users can change the settings of the application. The users can input a number between 0 and 10. See screenshot: http://oi43.tinypic.com/2uerazp.jpg
Now I would like to transform the input fields into a range slider using the CJuiSliderInput widget of the yiiframework: http://www.yiiframework.com/doc/api/1.1/CJuiSliderInput#maxAttribute-detail
I cannot seem to get the code working, this is the current code in view:
<?php foreach($settings as $i=>$value): ?>
<?php $setting=UserSetting::model()->findByPk($value->setting_id); ?>
<h4><?php echo CHtml::encode($setting->label); ?></h3>
<?php echo CHtml::activeTextField($value,"[$i]value"); ?>
<?php endforeach; ?>
I would like to replace the activeTextField with
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'model'=>$model,
'attribute'=>'[$i]value',
'maxAttribute'=>'timeMax',
// additional javascript options for the slider plugin
'options'=>array(
'range'=>true,
'min'=>0,
'max'=>10,
),
));
What values do I need to fill in to the widget to get it to work? Each textField input is from a different model btw.
The controller looks something like this(don't know if you need it):
$settingsvalues = UserSettingValue::model()->findAll('client_id=:id', array(
':id' => $id,
));
if(isset($_POST['UserSettingValue'])){
$valid = true;
foreach($settingsvalues as $i=>$value){
if(isset($_POST['UserSettingValue'][$i]))
$value->attributes = $_POST['UserSettingValue'][$i];
$value->save();
$valid = $value->validate() && $valid;
}
if($valid)
$value->save();
}
$this->render('settings',array(
'model' => $model,
'settings' => $settingsvalues,
));
Thanks a lot!
I fixed the problem by putting a javascript function in the slider. It is not exactly as I intended in the beginning, but it'll do. The slider now changes the value in the input fields.
<?Php
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'name' => $i,
'model'=>$value,
'attribute'=>"value",
'event'=>'change',
//'value'=>'$value',
'options'=>array(
'min'=>0,
'max'=>10,
'animate' => true,
'slide'=>'js:function(event,ui){$("#UserSettingValue_'.$i.'_value").val(ui.value);}',
),
)); ?>
This was a little big to be a comment. But please try the following
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'name'=>'$setting->label',
'attribute'=>'[$i]value',
'value'=>$value,
'options'=>array(
'min'=>0,
'max'=>10,
),
));
Also please try this
$this->widget('zii.widgets.jui.CJuiSliderInput',array(
'name'=>'$setting->label',
'attribute'=>'[$i]value',
'value'=>$value,
'options'=>array(
'min'=>0,
'max'=>10,
),
'htmlOptions'=>array(
'style'=>'height:20px;',
),
));

Why won't my flash message show? (Cake PHP 2.0)

In AppController:
public $helpers=array("Session","Html","Form");
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'MainPages', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'MainPages', 'action' => 'front')
)
);
In MainPagesController:
public function front()
{
$this->Session->setFlash('Your stuff has been saved.');
debug($this->Session->read('Message'));
//etc...
In default layout (default.ctp)
<div id="content">
<?php echo "Flash:" ?>
<?php echo $this->Session->flash(); ?>
The correct flash message shows in the debug but not on the view. What am I missing? PS It's not because of space after the ?>.
Edit: I have discovered that CakePHP is calling the session helper before the session component for some reason. Still trying to figure out how to fix it.
Simple way to create flash messages is to create their ctp file in app/view/element dir
Try
<div id="content">
<?php echo "Flash:" ?>
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Session->flash(); ?>
You need to define flash('auth') in your view to see authentication session flash messages.
My setflash works in this way..
Keep this in the App controller
function setFlash($message, $type = 'blue') {
//what ever the color you need..
$arr = array('red' => 'red', 'green' => 'green', 'yellow' => 'yellow', 'gray' => 'gray', 'blue' => 'blue');
$this->Session->setFlash($message, 'default', compact('class'), $arr[$type]);
}
and then call it from the controller action where you need to make the flash message as below..
$this -> setFlash("This is flash message","red");
now you need to make the flash in the layout(if you are using) or in your ctp file..
<?php echo $this->Session->flash() ?>
<?php echo $this->Session->flash('red') ?>
I think it is because you have an error here:
<?php echo "Flash:" ?>
You are missing the semi-colon
<?php echo "Flash:"; ?>

Categories