how to set default selection in radio button - Active form yii - php

Here i need to add default selection in radio button(checked).
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<?= $form->field($model, 'email') ?>
<?php if(!empty($usernames)){
echo '<label>Select Any Username To Which You Want To Login</label><div class="all_username">';
foreach ($usernames as $key => $value) { ?>
<?= $form->field($model, 'username[]')->radio(['value' => $value['username'], 'id' => ''])->label($value['username']) ?>
<?php } ?>
</div>
<?= $form->field($model, 'password')->passwordInput() ?>
<?php } ?>

You need to use following method where 1 is your selected ID.
$form->radioButtonList($model,'1', $usernames, array('separator'=>"" ));

You can probably use something like this:
foreach ($usernames as $key => $value) {
if(/*somecondition*/){
$model->username = $value['username'];
}?>
<?= $form->field($model, 'username[]')->radio(['value' => $value['username'], 'id' => ''])->label($value['username']) ?>
<?php } ?>
Only by assigning the $model->username a default value you can choose which radio will be selected.

Related

How i can change the value of the checkbox base in a dropDownList dynamically

I have a form and a need to change the checkbox to checked or unchecked based int selection of the dropDownList. I am a beginner in PHP and Yii2.
Here is my form
<div class="npi-approval-review-form">
<?php if(!empty(Yii::$app->session->get('activity') && Yii::$app->session->get('activity')->need_signature == 'S'))$model->sign = 1; ?><?php ?>
<?php $form = ActiveForm::begin(); ?>
<table>
<tbody>
<tr>
<td>
<?php if ($model->type == TypeOrigin::Aprovação): ?>
<?= $form->field($model, 'status')->dropDownList(StatusReview::asArray(), ['prompt'=>StatusApproval::asArray()[$model->status]]); ?>
<?php endif; ?>
<?php if ($model->type == TypeOrigin::Revisão): ?>
<?= $form->field($model, 'status')->dropDownList(StatusReview::asArray(), ['prompt'=>StatusReview::asArray()[$model->status]]); ?>
<?php endif; ?>
</td>
<td> </td>
<td>
<?= $form->field($model, 'type')->textInput(['value'=>TypeOrigin::asArray()[$model->type]
, 'readonly' => true, 'disabled' => 'true']);?>
</td>
</tr>
</tbody>
</table>
<?= $form->field($model, 'conclusion')->textarea(['rows' => 5, 'maxlength' => true]) ?>
<?= $form->field($model, 'sign')->checkBox(['value'=> $model->status == '2' ? '0' : '1']) ?>
<div class="form-group">
<?= Html::submitButton('Salvar', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
first of all, its better to set id for your dropdown and your checkbox,
to set id for select add it on options parametrs:
....['prompt'=>StatusReview::asArray()[$model->status,'id'=>'select_id']
then with this jquery js you can see your result.
<script>
$("#select_id").on('change',function(){ // if dropdown with id select_id changed.
var checkbox = $("#checkbox_id");
if($(this).val() === 'x'){ // set checkbox checked if option equal x
checkbox.prop('checked', true);
}else{ // else remove checked.
checkbox.prop('checked', false);
}
})
</script>

select some field in database base on Check field on form php

I have table name Checkin with fields : id,chief_compain, taking_history,pass_history,physical_exam and diagnosis.
i want use Checkbox Attribute on form as name of Field in a table " Checkin" , so after submit form will select only name of column have selected from database.
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'id')->checkbox(['value'=>Yii::$app->request->get('id')]) ?>
<?= $form->field($model, 'chief_compain')->checkbox(['value'=>'chief_compain']) ?>
<?= $form->field($model, 'taking_history')->checkbox(['value'=>'taking_history']) ?>
<?= $form->field($model, 'pass_history')->checkbox(['value'=>'pass_history']) ?>
<?= $form->field($model, 'physical_exam')->checkbox(['value'=>'physical_exam']) ?>
<?= $form->field($model, 'diagnosis')->checkbox(['value'=>'diagnosis']) ?>
<div class="form-group text-center">
<?= Html::submitButton($model->isNewRecord ? 'Submit' : 'Save Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
// === Controler
public function actionMedicalReport()
{
$model = new CheckIn();
if ($model->load(Yii::$app->request->post()) ) {
$id=Yii::$app->request->post('id');
$string = '';
foreach (\Yii::$app->request->post() as $key => $value){
if($value){
$string .= $key . ' ';
var_dump($string);
}
}
$report=CheckIn::find()->select($string)->where(['id'=>$id])->all();
return $this->render('print-report', ['report' => $report,]);
} else {
return $this->render('medical-report', [
'model' => $model,
]);
}
}
I have solved the problem. thanks all respond
now it work good
1) Change name of field to array and use only 1 name
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'id')->checkbox(['value'=>Yii::$app->request->get('id')]) ?>
<?= $form->field($model, 'chief_compain[]')->checkbox(['value'=>'chief_compain']) ?>
<?= $form->field($model, 'chief_compain[]')->checkbox(['value'=>'taking_history']) ?>
<?= $form->field($model, 'chief_compain[]')->checkbox(['value'=>'pass_history']) ?>
<?= $form->field($model, 'chief_compain[]')->checkbox(['value'=>'physical_exam']) ?>
<?= $form->field($model, 'chief_compain[]')->checkbox(['value'=>'diagnosis']) ?>
<div class="form-group text-center">
<?= Html::submitButton($model->isNewRecord ? 'Submit' : 'Save Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
2) in Controller
$model = new CheckIn();
if ($model->load(Yii::$app->request->post()) ) {
$id=Yii::$app->request->post('id');
$string = '';
$post=$model->chief_compain;
foreach ($post as $key ){
if($key)
{
$string .=$key.' ';
}
}
$myval=str_replace(" ",",",trim($string));
$report=CheckIn::find()->select($string)->where(['id'=>$id])->all();
return $this->render('print-report', ['report' => $report,]);
Ok, In Yii2 form you can not submit like this. You have to use ActiveForm widget or insert a hidden field with token, ovirwize you will get an Exception.
Second: if you pass your data throughout POST or GET you can easy check it using loop. Because you use checkbox, i assume that you pass throughout post and value is true or false.
$string = '';
foreach (\Yii::$app->request->post() as $key => $value){
if($key !== '_csrf-backend' && $value){
$string .= $key . ' ';
}
}
after that you create your new Query and or you can implement that query before the loop and pass needed field in the loop. I strongly recommend to not use raw sql.

Yii2 Checkbox list, not checked model values

Lets talk in code directly.
<div class="checkbox-list">
<?php
var_dump ($model->categoriesIds); // [1,2] so database has two categries.
$cats=Category::find()->all(); foreach($cats as $i=>$category){?>
<?= $form
->field($model, 'categoriesIds[]')
->checkbox([
'label'=>$category->name,
'value' => $category->id
])
->label(false)
?>
<?php } if(count ($cats)==0){ echo '<li>No Categories found.</li>';} ?>
</div>
i've categories's values but it don't check checkboxes based on this array, its saving values as well properly.
Use checkboxlist to loop your categories
<?
use yii\helpers\ArrayHelper;
$cats=Category::find()->all();
$cats=ArrayHelper::map($cats, 'id', 'name');
echo $form->field($model, 'categoriesIds[]')->checkboxList($cats);
?>
all();
$regs=ArrayHelper::map($reg, 'region_id', 'region_name');
?>
<?php echo $form->field($model, 'region_id[]')->checkboxList($regs); ?>

CakePHP 3.x multiple file upload not working

When using the line:
<?= $this->Form->input('ad_photos[]', ['type' => 'file', 'multiple' => true, 'label' => 'Add Some Photos']); ?>
The form won't even submit. It is like the page gets reloaded. This code use to work. Also, if I use ad_photos. instead of ad_photos[] I get this error message:
Cannot get an empty property
If I just use ad_photos with no dot or brackets only one file shows up in this request->data. I have tried to debug and var_dump the data as shown below, but it doesn't even make it to that part of the code using brackets and throws an error with just the dot.
controller method:
public function add()
{
$listing = $this->HavesAndWants->newEntity();
$error = '';
if ($this->request->is('post')) {
debug($this->request->data()); exit;
echo "<pre>"; var_dump($this->request->data()); echo "</pre>"; exit;
$listing = $this->HavesAndWants->patchEntity($listing, $this->request->data);
$listing->user_id = $this->Auth->user('id');
$listing->ad_photos = '';
if ( $error ) {
$this->Flash->error(__('The listing could not be saved. Please, try again.'));
} else {
if ($this->HavesAndWants->save($listing)) {
$this->Flash->success(__('The listing has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The listing could not be saved. Please, try again.'));
}
}
}
$this->set(compact('error', 'listing'));
$this->set('_serialize', ['listing']);
}
view form:
<?= $this->Form->create($listing, ['type' => 'file']) ?>
<h1><?= __('Add Listing') ?></h1>
<fieldset>
<legend><?= __('Contact Info') ?></legend>
<?= $this->Form->input('contact_name'); ?>
<?= $this->Form->input('contact_email'); ?>
<?= $this->Form->input('contact_phone'); ?>
<?= $this->Form->input('contact_street_address'); ?>
<?= $this->Form->input('contact_city'); ?>
<?= $this->Form->input('contact_state'); ?>
<?= $this->Form->input('contact_zip'); ?>
</fieldset>
<fieldset>
<legend><?= __('Listing Info') ?></legend>
<?= $this->Form->input('ad_title'); ?>
<?= $this->Form->input('ad_street_address'); ?>
<?= $this->Form->input('ad_city'); ?>
<?= $this->Form->input('ad_state'); ?>
<?= $this->Form->input('ad_zip'); ?>
<?= $this->Form->input('ad_additional_info', ['label' => 'Ad Description']); ?>
<?= $this->Form->input('ad_photos[]', ['type' => 'file', 'multiple' => true, 'label' => 'Add Some Photos']); ?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
I feel kind of stupid, but the security component was blackholing me. All I had to do was unlock the ad_photos field and I was able to upload multiple files. Although, I am unsure why I was being blackholed in the first place. If anyone has any ideas please post them below. :)

Php form ajax request not working , yii framework

I am doing a yii web application
i have a drop down list that should be dependent on another , i use ajax however it doesnt work.
ive seen the yii tutorial for dependent drop downs and searched everywhere.
http://www.yiiframework.com/wiki/24
this is my main drop down list:
<div class="row">
<?php echo $form->labelEx($model, 'sourceID'); ?>
<?php
echo $form->dropDownList($model, 'sourceID', CHtml::listData(Sources::model()->findAll(), 'sourceID', 'name'), array('empty' => 'select source'), array(
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('reservations/atest'),
'update' => '#meal'
)
)
);
?>
<?php echo $form->error($model, 'sourceID'); ?>
</div>
this is the dependent drop down list :
<div class="row">
<?php echo $form->labelEx($model, 'meal'); ?>
<?php echo $form->dropDownList($model, 'meal', array()); ?>
<?php echo $form->error($model, 'meal'); ?>
</div>
this is my controller action:
public function actionAtest() {
$data = Sources::model()->findAll();
$data = CHtml::listData($data, 'sourceID', 'name');
foreach ($data as $value => $name) {
echo CHtml::tag('option', array('value' => $value), CHtml::encode($name),true);
} }
also, i added the action to the access rules.
any help is appreciated ,
thank you in advance.
You placed the ajax option after the htmlOptions. Here is the modified code
<div class="row">
<?php echo $form->labelEx($model, 'sourceID'); ?>
<?php
echo $form->dropDownList($model, 'sourceID', CHtml::listData(Sources::model()->findAll(), 'sourceID', 'name'), array('empty' => 'select source','ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('reservations/atest'),
'update' => '#meal'
)
)
);
?>
<?php echo $form->error($model, 'sourceID'); ?>
</div>
And instead of using forms dropdownlist use CHtml::dropDownList for the dependent drop-down.
echo CHtml::dropDownList('meal','', array());
You can also use CActiveForm::dropDownList but in that case you have to use CHtml::resolveNameId in the update option of ajax

Categories