CHtml submit button is not working when 'enter' key press in Yii - yii

In my Yii web application, CHtml submit button is not working when 'enter' key press in Login form. My login form is,
<?php echo CHtml::beginForm(); ?>
<h3 class="form-title">Login to your account</h3>
<div class="alert alert-danger display-hide">
<button class="close" data-close="alert"></button>
<span> Enter any username and password. </span>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Username</label>
<div class="input-icon">
<i class="fa fa-user"></i>
<?php echo CHtml::activeTextField($model, 'username', array("class" => "form-control placeholder-no-fix", 'placeholder' => "Username")) ?>
</div>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Password</label>
<div class="input-icon">
<i class="fa fa-lock"></i>
<?php echo CHtml::activePasswordField($model, 'password', array("class" => "form-control placeholder-no-fix", 'placeholder' => "Password")) ?>
</div>
</div><br>
<div class="form-group">
<?php echo CHtml::submitButton(UserModule::t("Login "), array('class' => 'btn green pull-right')); ?>
</div>
<div class="text-danger"><?php echo CHtml::errorSummary($model); ?></div>
<?php echo CHtml::endForm(); ?><br><br><br>
<?php
$form = new CForm(array(
'elements' => array(
'username' => array(
'type' => 'text',
'maxlength' => 32,
),
'password' => array(
'type' => 'password',
'maxlength' => 32,
),
'rememberMe' => array(
'type' => 'checkbox',
)
),
'buttons' => array(
'login' => array(
'type' => 'submit',
'label' => 'Login',
),
),
), $model);
?>
In this login form using chtml submit button. I want to submit this login form by press enter key.
How to resolve this problem.
Please help me.

If you want to use onclick:
<?php echo CHtml::submitbutton(UserModule::t("Login "), array('class ' => 'btn green pull-right', 'onclick' => ' '));?>

Related

Yii 1.1.21 : is it possible to create two dropdown buttons in the same button group?

I am new to Yii, and I have been LOOKING for documentation on Yii and CMenu. I have used Phalcon and various other frameworks with similar options, but Yii's menu engine is new to me.
I am trying to create a button menu with two drop down menu buttons, each with sub menu items, like this:
Drop Down Button Group
But what is being rendered by the Yii CMenu engine is two drop down menus overlayed on each other and both are being triggered by the same buttons. Like this:
enter image description here
Looking at the rendered code, it looks like the two dropdown menus are being assigned the "dropdown-menu" class by CMenu, (or whatever the bootstrap enabled lib is) and becasue they are in the same button group, when the "open" class is assigned, it's opening BOTH dropdowns at the same time.
So my question is simple, is is even possible, using the CMenu menu arrays, to have two dropdowns in the same. Is there a menu "Item Option" or "HTML Option" I can add to the menu item properties that will all this to reference two different css tags? I know I gotta be missing something.
Here is how the menu's are being built in the view.
$this->menu = array_merge($this->menu, array(
array(
'label' => '<span class="hidden-xs hidden-sm">' . Yii::t('app', 'Export') . '</span>',
'encodeLabel' => false,
'htmlOptions' => array('id' => 'export-or-email-btn', 'class' => 'navbar-btn btn-sm',),
'items' => array(
array(
'label' => Yii::t('app', 'Export'),
'icon' => 'fa fa-file-excel-o',
'visible' => true,
'itemOptions' => array('class' => 'work-order-export-btn'),
),
array(
'label' => Yii::t('app', 'Email Export'),
'icon' => 'fa fa-envelope-o',
'visible' => true,
'itemOptions' => array('id' => $model->getClassName(), 'class' => 'email-export-btn', 'data-grid-id' => 'work-order-grid'),
),
array(
'label' => Yii::t('app', 'Export as Import Template'),
'icon' => 'fa fa-file-excel-o fa-lg',
'visible' => true,
'itemOptions' => array('class' => 'work-order-export-import-btn'),
),),),);
$this->menu = array_merge($this->menu, array(
array(
'label' => '<span class="hidden-xs hidden-sm">' . Yii::t('app', 'Actions') . '</span>',
'encodeLabel' => false,
'htmlOptions' => array(
'id' => 'work-order-actions-btn work-order-actions',
'class' => 'navbar-btn btn-sm',
'style' => 'margin: 0 0 0 15px;',
),
'items' => array(
array(
'icon' => 'fa fa-print fa-lg',
'label' => Yii::t('app', 'Print to PDF'),
'visible' => true,
'itemOptions' => array(
'class' => 'work-order-print-pdf',
),),
array(
'icon' => 'fa fa-print fa-lg',
'label' => Yii::t('app', 'Print'),
'visible' => true,
'itemOptions' => array(
'class' => 'work-order-print-selected',
),),))));
and here is the rendered code snippet:
<div class="btn-toolbar">
<div class="operations btn-group-sm btn-group open">
<button id="export-or-email-btn" class="navbar-btn btn-sm btn btn-primary dropdown-toggle" data-toggle="dropdown" name="yt7" type="button">
<span class="hidden-xs hidden-sm">Export</span>
<span class="caret"></span>
</button>
<ul id="yw6" class="dropdown-menu">
<li class="work-order-export-btn nav-header" data-ol-has-click-handler="">
<i class="fa fa-file-excel-o"></i> Export
</li>
<li id="WorkOrder" class="email-export-btn nav-header" data-grid-id="work-order-grid" data-ol-has-click-handler="">
<i class="fa fa-envelope-o"></i> Email Export
</li>
<li class="work-order-export-import-btn nav-header" data-ol-has-click-handler="">
<i class="fa fa-file-excel-o fa-lg"></i> Export as Import Template
</li>
</ul>
<button id="work-order-actions-btn work-order-actions" class="navbar-btn btn-sm btn btn-primary dropdown-toggle" style="margin: 0 0 0 15px;" data-toggle="dropdown" name="yt8" type="button">
<span class="hidden-xs hidden-sm">Actions</span>
<span class="caret"></span>
</button>
<ul id="yw7" class="dropdown-menu">
<li class="work-order-print-pdf nav-header">
<i class="fa fa-print fa-lg"></i> Print PDF
</li>
<li class="work-order-print-selected nav-header">
<i class="fa fa-print fa-lg"></i> Print Selected
</li>
</ul>
</div>
</div>
I think your problem is you are merging both arrays in the same $this->menu attribute.
Maybe you should use CMenu as widget like in the documentation
$this->widget('zii.widgets.CMenu', array(
'items'=>array(
// Important: you need to specify url as 'controller/action',
// not just as 'controller' even if default action is used.
array('label'=>'Home', 'url'=>array('site/index')),
// 'Products' menu item will be selected no matter which tag parameter value is since it's not specified.
array('label'=>'Products', 'url'=>array('product/index'), 'items'=>array(
array('label'=>'New Arrivals', 'url'=>array('product/new', 'tag'=>'new')),
array('label'=>'Most Popular', 'url'=>array('product/index', 'tag'=>'popular')),
)),
array('label'=>'Login', 'url'=>array('site/login'), 'visible'=>Yii::app()->user->isGuest),
),
));
For more information and attributes check out the official documentation here.

Post inside controller not loading into model in Yii2

When I want to get the variable from the form the post action doesn't load .
This is my view:
<?php
$form = ActiveForm::begin();
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?= Html::a(Yii::t('app', 'Start'), ['start', 'link' => $model->link], ['type' => 'button','class' => 'btn btn-primary btn-round']) ?>
</div>
<?php ActiveForm::end(); ?>
This is my controller:
if ($model->load(Yii::$app->request->post())){
exit(var_dump('everything is ok'));
}else {
exit(var_dump('nothing is right'));
}
The result is 'nothing is right'.
Apart from using the anchor link instead of a submit button, you are not using model to create active input hence the field names are without model names or the standard array format that Yii accepts, you should pass empty string to the load method as second parameter which is formName like below
$model->load(Yii::$app->request->post(),'');
So your complete form should look like
<?php
$form = ActiveForm::begin(
[
'action' => 'start',
]
);
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?php echo Html::submitButton(Yii::t('app', 'Start'), ['class' => 'btn btn-primary btn-round']) ?>
</div>
<?php ActiveForm::end();?>
EDIT
and your controller code should look like below, mind the first check it needs to be there so that the code is run when you submit only not on page load
if (Yii::$app->request->isPost) { //this should be here before the rest of the code
if ($model->load(Yii::$app->request->post(), '')) {
exit(var_dump('everything is ok'));
} else {
exit(var_dump('nothing is right'));
}
}
This is because load() method looks for post data inside model name property and you are writing the input yourself instead of using the Yii method for forms.
So your post Yii::$app->request->post() returns:
array(
'username' => 'value of username',
)
And your $model->load looks for
array(
'modelName' => array(
'username' => 'value of username',
)
)
To make your post data too look like that you could do it the right way that is, delete your Input and use this method inside the form:
<?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?>
Or the wrong way, modify your input and inside username use:
<input type="text" name="modelName[username]" placeholder="FullName">
Of course where I put username you must put your real model name.
Finally I find the solution
<?php
$form = ActiveForm::begin(
[
'action' => 'start',
]
);
?>
<div class="form-group">
<input type="text" name="username" placeholder="FullName">
<?= Html::a('submit', Url::to(['start', 'link' => $model->link]), ['data-method' => 'POST']) ?>
</div>
<?php ActiveForm::end();?>
thank you for all

yii2: how to add if for checked or unchecked checkbox in _form.php

i want add if or else for checkbox in _formCreate.php
this is my checkbox:
<div class="col-lg-6 height3">
<p class="col-lg-8">سرگرمی</p>
<?= $form->field($model, 'c2')->input('checkbox', ['id' => '', 'value'=> '2', 'class' => 'col-lg-4 chb'])->label('') ?>
</div>
<div class="col-lg-6 height3">
<p class="col-lg-8">خدماتی</p>
<?= $form->field($model, 'c3')->input('checkbox', ['id' => '', 'value' => '3', 'class' => 'col-lg-4 chb'])->label('') ?>
</div>
i have this variable in this form $checkBox with this data:
array(3) { [0]=> array(1) { ["project_type_id"]=> string(1) "2" } [1]=> array(1) { ["project_type_id"]=> string(1) "3" } [2]=> array(1) { ["project_type_id"]=> string(1) "5" } }
how to add if for <?= $form->field... and check that with my array and checked the checkbox!
If I get you right, then you can write logic for checkbox (model's attribute) in the beginning of the View.
For example, if your model's name is 'Yii', then checkbox should be automatically checked, in code I would write it this way:
<?php
/* #var $this yii\web\View */
/* #var $model SomeModel */
if($model->name == 'Yii') $model->c2 = true;
?>
Then in Form:
<div class="col-lg-6 height3">
<p class="col-lg-8">سرگرمی</p>
<?= $form->field($model, 'c2')->checkbox()->label('') ?>
</div>
<div class="col-lg-6 height3">
<p class="col-lg-8">خدماتی</p>
<?= $form->field($model, 'c3')->checkbox()->label('') ?>
</div>
If model attribute set and model attribute value == checkbox value, then checkbox will checked
<?php
if(in_array('2', ArrayHelper::map($checkBox, 'project_type_id', 'project_type_id'))) $model->c2 = '2';
if(in_array('3', ArrayHelper::map($checkBox, 'project_type_id', 'project_type_id'))) $model->c3 = '3';
?>
<?= $form->field($model, 'c2')->input('checkbox', [
'id' => '',
'value' => '2',
'class' => 'col-lg-4 chb'
])->label('') ?>
<?= $form->field($model, 'c3')->input('checkbox', [
'id' => '',
'value' => '3',
'class' => 'col-lg-4 chb'
])->label('') ?>

How to do integration of Kartik-v Export widget in yii2 basic to export table data?

I am a newbie to yii2 and learning it. I am trying to integrate the export widget developed by kartik. I have installed it using composer. But when I am trying to use it in my project, I am not able to export any data. It just shows two buttons one is for list of columns and second is for export as excel, pdf,csv. when I am clicking on export as csv nothing is happening. I have spend around 4-5 hours to integrate it but nothing is in my favor. can anyone provide the best tutorial or any suggestion how to integrate it in my project.
Link of the widget is -: https://github.com/kartik-v/yii2-export
<?php
use yii\helpers\Html;
use yii\grid\GridView;
use yii\bootstrap\ActiveForm;
//use kartik\grid\GridView;
use kartik\export\ExportMenu;
/* #var $this yii\web\View */
/* #var $searchModel app\models\CattleDataSearch */
/* #var $dataProvider yii\data\ActiveDataProvider */
$this->title = 'Cattle Profile';
$this->params['breadcrumbs'][] = $this->title;
$gridColumns = [
['class' => 'yii\grid\SerialColumn'],
'cattleform_id',
'farmer_id',
'cattle_owner',
'cattle_id',
'type_of_cattle',
'origin',
['class' => 'yii\grid\ActionColumn'],
];
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'columns' => $gridColumns,
'columnSelectorOptions'=>[
'label' => 'Columns',
'class' => 'btn btn-danger'
],
'fontAwesome' => true,
'dropdownOptions' => [
'label' => 'Export All',
'class' => 'btn btn-success'
]
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
]);
?>
<div class="cattle-data-index">
<h1><?= Html::encode($this->title) ?></h1>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<hr style="background: #C0C0C0; height: 2px;"/>
</div>
<div class="cattle-form-index" style="border: #FFFFFF solid 1px; border-radius: 10px; padding: 20px; box-shadow: 0px 0px 20px #888888; margin-bottom: 20px; background-color: white; ">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<div class="row">
<div class="col-lg-6">
<?= $form->field($model, 'cattleform_id') ?>
<?= $form->field($model, 'cattle_id') ?>
<?= $form->field($model,'type_of_cattle')- >dropDownList(array('Cow'=>'Cow','Buffalo'=>'Buffalo'), ['prompt'=>'Select....']) ?>
</div>
<div class="col-lg-6">
<?= $form->field($model,'origin')->dropDownList(array('Gifted'=>'Gifted','Home'=>'Home','Same Village'=>'Same Village','Outside'=>'Outside','Other'=>'Other'),['prompt'=>'Select....']) ?>
<?= $form->field($model,'natural_of_ai')->dropDownList(array('Natural'=>'Natural','AI'=>'AI','No Information'=>'No Information','NA'=>'NA','Other'=>'Other'),['prompt'=>'Select....']) ?>
<?= $form->field($model,'delivery')->dropDownList(array('Home'=>'Home','Outside'=>'Outside','No Information'=>'No Information','NA'=>'NA'),['prompt'=>'Select....']) ?>
<div class="form-group">
<?= Html::submitButton('Search', ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton('Reset', ['class' => 'btn btn-default']) ?>
<?php
if(isset ($_GET['farmer_id']))
{
echo Html::a('AddNew', ['create','farmer_id'=>$_GET['farmer_id']], ['class' => 'btn btn-success']);
}
else
{
echo Html::a('AddNew', ['create'], ['class' => 'btn btn-success']);
}
?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
</div>
</div>
On fresh yii installation I am getting the following error-:
Have you checked main.php in config,can you try the following code there
'modules'=>['gridview'=>['class'=>'\kartik\grid\module',],],
and do check whether the pop up's are blocked in your Browser.

AJAX validation in search

I am new to Yii, I have two tables. master_customer,master_client. In the client model, in the view I have a search box, If I enter a few letters in the search box, it must automatically get the data and give results from master_customers(like google suggestions). I have made relations in the client model with the master_customer.
please help me with the code. Thanks in advance
CONTROLLER action:
public function actionIndex()
{
$criteria = new CDbCriteria();
if(isset($_GET['q']))
{
$q = "%".$_GET['q']."%";
$criteria->condition = 'cust_name='.$q;
$arrTier3 = MasterCustomers::model()->findAll($criteria);
//$criteria->compare(MasterCustomers::model()->cust_name,$q, true);
//$criteria->compare('$data->customers->cust_name', $q, true, 'OR');
print_r($arrTier3);
die();
}
$dataProvider=new CActiveDataProvider('Host', array('criteria'=>$criteria));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
view :
<!--Content-->
<div id="content">
<div style="padding: 10px;">
<a href="<?php echo $this->createUrl('/Controller/create');?>" title="Create New Host" class="btn btn-primary circle_ok" style="text-decoration: none;" >Add New Host to Customer</a>
<div style="float:right">
<?php
echo CHtml::link('Upload Customer CSV', array('/Controller/uploadCustomers'), array(
'onclick'=>'return hs.htmlExpand(this, { objectType: "iframe", wrapperClassName: "full-size", align: "center" } )',
'class'=>'btn btn-primary',
'id'=>'upload_link',
));
?>
</div>
</div>
<h3><?php echo $title; ?></h3>
<div class="innerLR">
<div class="row-fluid">
<?php
$obj=$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
//'afterAjaxUpdate'=>'\'changeTRColor()\'',
//'itemView'=>'_view',
'columns'=>array(
array( // display 'create_time' using an expression
'name'=>'name',
'value'=>'$data->host_name',
),
array(
'name'=>'serviceId',
'value'=>'$data->host_serviceid',
),
array(
'name'=>'customer',
'value'=>'$data->customers->cust_name',
),
array(
'class'=>'CButtonColumn',
'template'=>'{delete}{update}',)
),
));
?>
<form method="get">
<input type="search" placeholder="search" name="q" value="<?=isset($_GET['q']) ? CHtml::encode($_GET['q']) : '' ; ?>" />
<input type="submit" value="search" />
</form>
</div>
<div class="separator bottom"></div>
</div>
</div>
<!-- // Content END -->
<div class="clearfix"></div>
<!-- // Sidebar menu & content wrapper END -->
<div id="footer" class="hidden-print">
<?php $this->renderPartial('application.views.layouts._footer_inc'); ?>
</div>
If you need a search like google that searches on each key press and update GridView then you can try like this. In your javascript write
<script type="text/javascript">
$(document).ready(function () {
$('#id of your search box').keyup(function () {
$.fn.yiiGridView.update('id of your grid to be updated', {
data: $(this).serialize()
});
});
});
</script>
In your controller check the $_POST variable that what value it has, so you can grab that value and search in master_customers for that specific value and render the view again.