Evaluating Checkboxes in Yii2 - yii2

I created several checkboxes like this:
<?= $form->field($model,'rememberMe')->checkBox() ?>
Unfortunately, I don't know how to evaluate, whether Checkbox has been checked, or not. Following code:
if ($model->rememberMe ==1) echo "Checkbox is activated";
else echo "Checkbox is not activated";
always prints out 'Checkbox is not activated'. Any ideas, how to fix that?

Related

Split pjax wiget in form part and result part

I'm using Yii2, activeForm and the Yii2 pjax widget to handle an search form with result list.
But for layout reason, I have to split this package to two parts: the search form, which should be located in the header of the page and the result listing, which should be placed in the middle of the page.
This means it's not possible to do something like that (pseudocode):
Pjax::begin
$form=>activeForm::begin..
ActiveForm::end();
ListView::widget...
Pjax::end()
What I need is placing the activeForm in the layout header, placing the ListView wiget in the center of the page and telling pjax: read the input from the form, load data from Server and replace the clist view.
Is that possible using the build-in functionality of the widgets?
I didn't get it working, maybe I misunderstood the basic concept?
Using gridview (or widget which use searchModel) you could use a separated model for modelSearch in header and show the resul in the gridview palced where you prefer
in this way you need only a pjax block
<?= $this->render('_search', ['model' => $searchModel]); ?>
<?php Pjax::begin(); ?>
<?= GridView::widget([
...
]); ?>
<?php Pjax::end(); ?>
for other soluzione not based on gridview and when you need multiple seprataed pjax block
You could try using named pjax block using a value for id
<?php \yii\widgets\Pjax::begin(['id'=>'pjax-block1']); ?>
....
....
<?php \yii\widgets\Pjax::end(); ?>
<?php \yii\widgets\Pjax::begin(['id'=>'pjax-block2']); ?>
....
....
<?php \yii\widgets\Pjax::end(); ?>
and if you need you could manage specific refresh using jquery
$.pjax.reload({container: '#pjax-block1', async: false});
$.pjax.reload({container: '#pjax-block2', async: false});

yii2 star widget does not appear

I can not see starwidget in my yii2, I installed the widget, but it doesn't display stars.
See loading gif:
Following is my code:
echo '<label class="control-label">Rating</label>';
echo StarRating::widget([
'name' => 'rating_2',
'value' => 2.5,
'disabled' => true
]);
Is there an explanation to this. Please help!?
Check your js assets. Check if jQuery is not loading twice.

How to reference grid to ActiveForm

You have on one page ActiveForm as master record, and grid as child records, how can i make relation between them. At create time there isn't id of master record! Any advice is wellcome! TIA. Asim
There is no need to use modal in order to add the child object. You can do it in one form. If you use a modal, you'll need to create the parent object first, and then allow this modal to be shown. In this way, your worries will be invalid.
If you want to add a parent and a child in one form, you can pass both objects to the view and then put their fields in the form. Then on form submit, you validate both objects, and then if everything is fine, save the parent, and then the child, assigning the parent id to the child. You can do this in a transaction so that if the child fails, the parent won't be recorded as well or vise versa.
Here is some code:
class YourController extends Controller
{
public function actionSomething()
{
$parent = new Parent();
$child = new Child();
$request = Yii::$app->request;
if ($parent->load($request->post('Parent')) && $child->load($request->post('Child'))) {
// Do validation and if everything is fine, then save the fields
Yii::$app->db->transaction(function() {
$parent->save(false);
$child->parent_id = $parent->id;
$child->save(false);
});
}
return $this->render('view', compact('parent', 'child'));
}
}
The view:
$form = ActiveForm::begin() ?>
<?= $form->field($parent, 'fieldA') ?>
<?= $form->field($parent, 'fieldB') ?>
<?= $form->field($child, 'fieldA') ?>
<?= $form->field($child, 'fieldB') ?>
...other input fields...
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
Did you get the idea? If you have questions or problems, please ask. Here is another example. This is with an update, but the idea is the same:
Getting Data for Multiple Models

html button that stays pushed after refresh

I am making a site from which I control some electrical devices in the house. I have checkboxes for controlling the devices and a submit form, but after submit the checkboxes appear unchecked. I want a way for them to stay checked after submit and page refreshes. It doesn't have to be necessarily a checkbox... anything that can implement the on-off toggle with memory of previous state is fine. Any suggestions?
(the on-off state of every device is stored in a txt file that updates at every change, if that helps)
Here is an example of what you could do :
First read the value of your text file :
<?php
$filename = "/usr/local/something.txt"; // Your file
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
?>
... and then in your form, check the box only if you file has the correct content :
<input type="checkbox" name="name" value="Value" <?php echo ($contents == '1' ? 'checked' : '' )?>> Value<br>
Of course you have to adapt $contents == '1' to whatever is in your text file when you want it to be checked.

Grabbing the Value of a Form and Using POST

Would like to post the value of a dynamic selection menu when the OnChange event is called. My code is currently this:
<form action="test4.php" method="POST" name="itemform">
<select name="input_name" id="input_name" onChange="this.form.submit();">
<?php
while($row = mysql_fetch_array($result))
echo "<option value='".$row['item_id']."'>" . $row['itemname'] . "</option>";
?>
</form>
The option values are populated by a query defined above and that works like a charm. The problem I am facing is for some reason the form is not grabbing and POSTing the value selected in the menu box when the OnChange event is raised. Any Ideas?
<form action="test4.php" method="POST" name="itemform">
My page test4.php uses this line to retrieve the value: echo ($_GET["input_name"]);
You're mixing up $_GET and $_POST, you need to use the one that corresponds to your form's method.
Close your <select> tag. ;D
Try closing your <select> element - that could be it.