Yii checkbox in dropdown - php

I'm developing app with Yii framework. I need from dropdown list with checkboxes as values of this list. I've been searching about this but didn't find anything. Can someone help me with this task?

Here is the link to a Yii extension which does exactly what you want!
Yii Framework Extension: echmultiselect
On top of that, there are other Yii extensions for multiselect features that are also quite interesting! you might also want to take a look at these as well!
Yii Framework Extension: Select2
Yii Framework Extension: emuliselect

You can do it like this (in this example your model must have a public field $availableLanguages):
<?= $yourActiveForm->field($model, 'availableLanguages', ['template' => "
<div class='dropdown'>
<button
class='btn btn-default dropdown-toggle'
data-toggle='dropdown'
type='button'>
<span>Select languages</span>
<span class='caret'></span>
</button>
{input}
</div>"])->checkboxList(
[
'FR'=>'France',
'DE'=>'Germany'
],
[
'tag' => 'ul',
'class' => 'dropdown-menu',
'item' => function ($index, $label, $name, $checked, $value) {
return '<li>' . Html::checkbox($name, $checked, [
'value' => $value,
'label' => Html::encode($label),
]) . '</li>';
}
]); ?>

You can create a simple textbox and when the user clicks you display a list of checkboxes
<div id="listOfDays">
<span><?php echo Yii::t('frontend','CHOOSE_DAY'); ?> </span>
</div>
<div id="itemslistOfDays" style="display:none;position:absolute;z-index:10;background-color:white; width:300px">
<?php
echo $form->checkBoxList($model, 'freq_details', array(
'1'=>Yii::t('frontend','MONDAY'),
'2'=>Yii::t('frontend','TUESDAY'),
'3'=>Yii::t('frontend','WEDNESDAY'),
'4'=>Yii::t('frontend','THURSDAY'),
'5'=>Yii::t('frontend','FRIDAY'),
'6'=>Yii::t('frontend','SATURDAY'),
'7'=>Yii::t('frontend','SUNDAY'),
)); ?>
</div>
You will have to write some javascript or jquery in order to hide,show and bind click event

Related

Codeigniter data from view to controller

Each User has multi profiles. once they logged in, they are asked to select a profile,
here is the code for Form to select profile.
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="login-panel panel panel-default">
<div class="panel-heading">
<h3 align ="center" class="panel-title">Select Profiles</h3>
</div>
<div class="panel-body">
<?php
foreach($resJacs->{'details'} as $key) {
echo form_open('selectaccess', array(
'class' => 'form-group',
'role' => 'form'
));
echo form_submit(array(
'value' => $key->profile_name,
'name' => $key->profile_type,
'class' => 'btn btn-lg btn-default btn-block'
));
echo form_close();
}
?>
</div>
</div>
</div>
</div>
when user selects the profile profile id is passed to session for later use. here is the code for "selectaccess",
public function SelectAccess() {
$sess_data = array(
'id' => $this->session->userdata['is_logged_in']['id'],
'prfid' => $this->input->post('')
);
print_r($sess_data);
}
how can i prfid as mentioned in selectaccess method.
I just want to give an example, maybe this can help u :
I am use pure html .
<form action="SelectAccess/<?php echo $id; ?>">
<input type="text" name="name">
<button type="submit">Submit</button>
</form>
Controller
function SelectAccess($val='') {
$input = $this->input->post('name');
$_SESSION['whatever'] = $val;
}
Normally you need to know the name of the field in order to get the value at the controller. But you are dynamically creating the field names so that gets tricky.
Fortunately you are only posting one input so $_POST should have only one item. The way your view is written the value of $_POST[0] will be provided by $key->profile_name. Hopefully, that value is what you are looking for.
public function SelectAccess() {
{
$sess_data = array(
'id' => $this->session->userdata['is_logged_in']['id'],
'prfid' => isset($_POST[0])) ? $_POST[0] : NULL;
);
}

Yii2 Render widget dynamically

I would like to render JQuery UI sortable list (I am using Yii2 JUI widget), to be populated with data from database table. I am passing data from database to the view and have it in a variable. The item HTML structure looks like this:
<div class="row row-item dist simple-border">
<div class="col-md-4">
<img alt="Bootstrap Image Preview" src="<IMAGE_PATH>" />
</div>
<div class="col-md-8">
<h4>
<ITEM DESCRIPTION>
</h4>
</div>
</div>
My question is: What is the best way to populate items inside the Widget, as it is impossible to execute php code within.
<?= yii\jui\Sortable::widget([
'items' => [
------<expected foreach php loop for each model>------
['content' =>
<This is where one HTML item goes>
],
------<end foreach>------
],
'options' => ['class' => 'connectedSortable',],
'clientOptions' => ['cursor' => 'move', 'connectWith' => '.connectedSortable']
]) ?>
My first idea was to create a function that returns item content as a string, but it doesn't seem like a good, efficient practice. Any ideas would be greatly appreciated. Regards.
EDIT: So far, I have created a function, which renders mentioned above items by concatenation. Still, I don't think this is the proper way to do it. I am still new to Yii and would be thankful for any tips. Regards.
I have found such solution for my problem, feel free to review and improve it.
Controller
public function actionIndex()
{
$searchModel = new Exchange();
$dataProvider = $searchModel->getOwnerItems(Yii::$app->request->queryParams);
$ownerItems = $this->getOwnerItems($dataProvider);
return $this->render('index', [
'ownerItems' => $ownerItems,
]);
}
protected function getOwnerItems($dataProvider){
$items=array();
foreach($dataProvider->models as $model){
$item=array('content' => '<div class="row row-item dist simple-border"> <div class="col-md-4"> <img alt="Bootstrap Image Preview" src="uploads/'.$model->image.'" /> </div> <div class="col-md-8"> <h4>'. $model->name .'</h4> </div> </div>');
array_push($items,$item);
}
return $items;
}
View
<?= yii\jui\Sortable::widget([
'items' => $ownerItems,
'options' => ['class' => 'connectedSortable',],
'clientOptions' => ['cursor' => 'move', 'connectWith' => '.connectedSortable']
]) ?>
I am aware of possible imperfection of this solution, but I have no other ideas at the moment. Regards

Symfony2 form type attribute in view

I using form type in symfony 2.8.x.
I need form attribute text in view.
form type:
$builder->add( 'category_id', ChoiceType::class,
array( 'label'=>'Category',
'constraints'=>array( new Regex(array("pattern"=>"'([^0-9]*)$'si",
"message"=>"Required field!"))
),
'choices'=>$categoriesRepo->getAllActive(),
'choices_as_values'=>true,
'choice_label' => 'getName',
'choice_value' => 'getId',
'attr'=>array( 'class'=>'form-control',
'help'=>'Help message.',
)));
In view:
<?php echo $view->render("XXXBundle:XXX:form_element.html.php", array('form'=>$templateForm['category_id']))?>
form_element.html.php
<div class="form-group">
<?php echo $view['form']->label($form, null, array('required'=>false, 'label_attr'=>array('class'=>'col-md-3 control-label'))) ?>
<div class="col-md-4">
<?php echo $view['form']->widget($form, array('attr'=>array('help'=>false))) ?>
<span class="help-block error"><?php echo $view['form']->errors($form) ?></span>
<span class="help-block"> HELP_MESSAGE </span>
</div>
</div>
I need "help" attribute to HELP_MESSAGE in view.
There is a solution to this problem?
Thank you!
You are the first Symfony user I've ever seen who renders PHP files instead of twig. Congratulations.
The real business part of your code you haven't posted, so it's hard to give a definitive answer, but somewhere at the bottom of your controller's action function there will be a call to $this->render('XXXBundle:XXX:template.html.php');
The 'Symfony way' is to pass the help message in that:
$help_message = 'My Help Message';
$this->render('XXXBundle:XXX:template.html.php', array('help_message' => $help_message));
In your template file:
<?php echo($view['help_message']);?>

How can I do a simple search form/function in yii2?

I want to do a search using a form which will search name field, brand field and category field.
Should I be using post or get for this situation?
How/where should I be creating my query search model, model or controller?
Any code snippets would be great.
I have the following in my view:
<?php $form = ActiveForm::begin(['id' => 'home-search','method' => 'get', 'action' => Url::to(['productitem/search'])]); ?>
<?= $form->field($productitem, 'name')->textInput(array('placeholder' => 'What are you looking for?'))->label(false) ?>
<?= $form->field($productitem, 'brand_id')->dropDownList(
ArrayHelper::map(ProductBrand::find()->all(),'id','name'),
['prompt'=>'Select Brand']
)->label(false) ?>
<?= $form->field($productitem, 'category_id')->dropDownList(
ArrayHelper::map(ProductCategory::find()->all(),'id','name'),
['prompt'=>'Select Department']
)->label(false) ?>
<div class="form-group search-button">
<button type="submit" class="btn btn-primary" name="login-button">Search <i class="fa fa-lg fa-arrow-circle-o-right"></i></button>
</div>
<?php ActiveForm::end(); ?>
The answer to your questions:
I would use get just for the convenience of pressing the "back" button. You are not changing the database at all so a GET should provide enough security / convenience
search model should be in the [Model]Search. Yii2 has specialized search models on top of the normal models why not keep them there? That is exactly what they are there for.
No code snippets SO helps you with your code, does not usually writes it for you. But then again somebody will probably come and spoon feed you code anyway so you will not learn anything so enjoy.

Yii : How to make a button with an html tag inside the label

I'm trying to use some bootstrap features like the Icon glyphs in the Yii CHtml class, here is my code:
<?php
echo CHtml::submitButton('<i class="icon-user"></i> Login',
array(
'class' => 'btn btn-large pull-right'
));
?>
But it kinda don't "recognize" the tag and just renders the tag out like the image bellow .
does anyone knows how to workaround it (without typing the html tags itself).
Thank you guys.
CHtml::submitButton produces an <input type="submit"> that cannot accept additional HTML as its content. However, you can do things to taste with CHtml::tag:
echo CHtml::tag('button',
array('class' => 'btn btn-large pull-right'),
'<i class="icon-user"></i> Login');
This will produce a <button> tag that can take arbitrary HTML as its content.
Update: As frostyterrier points out in the comments, there's a built-in method CHtml::htmlButton that allows you to do this even more easily:
echo CHtml::htmlButton('<i class="icon-user"></i> Login',
array('class' => 'btn btn-large pull-right'));
Try to set 'encode' to false in htmlOptions parameter.
<?php
echo CHtml::submitButton('<i class="icon-user"></i> Login',
array(
'encode' => false,
'class' => 'btn btn-large pull-right'
));
?>
CHTML::submitbutton generates <intput type="submit" /> tag and you're trying to cram HTML into its value attribute.
Why don't you simply add the icon-user CSS class to the button itself?
echo CHtml::submitButton('Login', array(
'class' => 'btn btn-large pull-right icon-user'
));
If it's a Bootstrap-defined class, it should style the input button nicely.
try this
$this->widget("bootstrap.widget.TbButton", array(
'buttonType'=>'submit',
'label=>'Login',
'type'=>'success', //or default or warning... as you like
'icon'=>'user',
'size'=>'large', //small, mini, or just comment line for default
));
documentation: http://www.cniska.net/yii-bootstrap/#tbButton
Firstly you should change from subbmitButton to htmlButton,for example write the following code:
<?php echo CHtml::htmlButton('<span>Log in!</span>',array('type'=>'submit','class'=>'tbutton small pad')); ?>

Categories