Declaring a variable on element, showing it on view (unrecognized variable) - variables

I have an element called 'init' and a view (CAKEPHP)
I'm declaring variables on 'init' like this:
<?
$auxUrl = Router::url(array("controller"=>"Uploader","action"=>"admin_index"));
$uploaderLink = 'r';
?>
then in View I do:
<?
echo $this->element('init');
echo $uploaderLink;
?>
and it says that it doesn't recognize the echoed variable...
I'm obviously doing something very wrong, but what it is??

If the only thing you are doing in your element is declaring a few variables, you should be doing this in the controller and using $this->set to pass the variables to the view.

Related

Pass params from view to layout in Yii2

I have a variable in view file, i want pass this to layout file.
I use params like below:
$this->params['name'] = 'masoud';
I try to print this in layout file:
<?= $this->params['name'] ?>
I get an error: Undefined index: name how cat i fix it?
If your view is rendered by a controller, you can do as below.
Declare a public member in your controller
public $params;
Assign value in your view
$this->context->params['name'] = 'masoud';
Now you can use the variable in your layout
<?= $this->context->params['name'] ?>

CakePHP 3, variables in Internationalization / translation

I want to use variables in some translation texts bu I can't figure out how to make it work. Your help would be appreciated.
What I would Ideally like:
In my page view:
<?= __("welcome_message", ['John']) ?> // or some variant
In my /en/default.po file
msgid "welcome_message"
msgstr "Welcome {1}, step in and have some fun!"
In short, how can I use a variable in a translated text? Thanks.
Use this in your view, the args are transfered to the translating function as an array, {0} takes the element at index 0 of your array passed in args
<?= __("Welcome {0}", ['John']) ?>
you can also use this syntax : where you pass variables as independent arguments to the function
<?= __("Welcome {0}", 'John') ?>
Using Variables in Translation Messages

How to implement a dynamic js script in Yii List View?

Hello and thanks for reading my question. I have a typical list view:
<?php $this->widget('bootstrap.widgets.TbListView',array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
'emptyText'=>'No Jobs',
)); ?>
In my _view file I have a div and a button that slideToggles the div. If I just put the Javascript at the top of the page, it does not work because the results are dynamic and the name of the div changes with the id returned, eg:
id="detailsDiv-<?php echo $data->id_employer_contract;?>"
The problem is in my Javascript, which is as follows:
<?php Yii::app()->clientScript->registerScript('details', "$('#details-$data-id_employer_contract').click(function(){
$('#detailsDiv-$data->id_employer_contract').slideToggle();
return false;});");?>
How can I make this Javascript code dynamic? Meaning, how can I loop through the id? I tried adding the code to the listview property ajaxUpdate but it's still not working. Can someone tell me how I can loop a Javascript in a list view?
Add the id to your toggle buttons as data attribute:
<button class="toggleDetails" data-id="<?php echo $data->id_employer_contract ?>">
Then you can access these data attributes like this js:
<?php Yii::app()->clientScript->registerScript('toggleDetails', "
$('.toggleDetails').click(function(e){
var id = $(this).data('id');
$('#detailsDiv-' + id).slideToggle();
e.preventDefault();
});
", CClientScript::POS_READY) ?>
NOTE: You should not put this javascript into _view.php but into the main file where you render the List View. You only need this one single snippet to deal with all your buttons.

How to avoid Yii from Capitalize our labels?

If we call this:
<?php echo $form->label($model,'Hello my friend'); ?>
We get output it on the viewport the following:
"Hello My Friend"
On the CSS I have nothing that says capitalize, anywhere.
How can we change this behavior ?
I've tried to edit the CSS and make:
label {
text-transform: none !important;
}
No luck.
How can we have
Hello my friend
printed exactly as we write it ?
Instead of doing like this on the view file:
<?php echo $form->label($model, 'Hello my friend'); ?>
We have change it to:
<?php echo $form->label($model, 'hello_friend'); ?>
where hello_friend is defined on the respective model method attributeLabels(), like this:
'hello_friend' => 'Hello my friend')
So, as bool.dev properly says, we have to place the attribute name AND NOT a random string.

Saving a checkbox value in Yii

I can't figure out how to properly save checkbox values in Yii. I have a MySQL column, active, defined as a tinyint. I have the following form creation code, which correctly shows the checkbox as checked if the value is 1 and unchecked if 0:
<?php echo $form->labelEx($model,'active'); ?>
<?php echo $form->checkBox($model,'active'); ?>
<?php echo $form->error($model,'active'); ?>
And the code to save the form correctly changes other, text-based values:
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Thing']))
{
$model->attributes=$_POST['Thing'];
if($model->save())
$this->redirect(array('thing/index'));
}
$this->render('update',array(
'model'=>$model,
));
}
The value of active is not saved. Where am I going wrong?
You can use htmlOptions array to specify value attribute. Below is the code example:
<?php echo $form->labelEx($model,'active'); ?>
<?php echo $form->checkBox($model,'active', array('value'=>1, 'uncheckValue'=>0)); ?>
<?php echo $form->error($model,'active'); ?>
Since version 1.0.2, a special option named 'uncheckValue' is
available that can be used to specify the value returned when the
checkbox is not checked. By default, this value is '0'.
(This text is taken from YII Documenration)
For every input that you are accepting from user, you need to define it in model::rule(). is active defined there in rule()?
In general, if you are having problems saving to the database, i would replace
$model->save();
with
if($model->save() == false) var_dump($model->errors);
that way, you can see exactly why it did not save. it is usually a validation error.
Please follow:
1. in protected/models/Thing.php add active as a numeric
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('active', 'numerical', 'integerOnly'=>true),
//OR optional
array('active', 'safe'),
);
}
Controller action: Its ok
View:
<?php echo $form->labelEx($model,'active'); ?>
<?php echo $form->checkBox($model,'active', array('value'=>1, 'uncheckValue'=>0)); ?>
<?php echo $form->error($model,'active'); ?>
Hope this will work for you...
Article which can be helpful when figuring out how to handle booleans & checkboxes in Yii
http://www.larryullman.com/2010/07/25/handling-checkboxes-in-yii-with-non-boolean-values/
I used a bit type field in my DB and it didn't work.
1.- I changed the field type to tinyint
2.- In the rules function added:
array('active','numerical'),
3.-In the form (as D3K said) do:
<?echo $form->checkBox($model,'active',array('value'=>1, 'uncheckValue'=>0));?>
You can check by printing all the attributes which are being captured. If active is not captured, it must not be safe. you need to declare the variable as safe or define a rule around that variable. This will make the variable safe.
I have similar the same problemce before,I change data type is int,so it save
We can also add a rule as safe in model to pass the values from form to controller without missing.
array('active', 'safe'),
well this post is so old but I've found a solution very useful specially for giving checkbox a value specified rather than number. The new syntax is something like this
notice I'm using ActiveForm
field($model3, 'External_Catering')->checkbox(['id' => 'remember-me-ver', 'custom' => true,'value'=>"External_Catering", 'uncheckValue'=>"vide"]) ?>
1) where my model is =>model3
2) with the name External_Catering
3) that take the value External_Catering and empty when it's uncheckValue
4) in Controller you get the value just by specifying the model and it's attribute like
  $External_Catering=$model3->External_Catering.