How to avoid Yii from Capitalize our labels? - yii

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.

Related

How to make Yii CListView to display WYSIWYG formatted text?

On the admin side, I've used a WYSIWYG editor for all textareas.
When we format that with bold for example, the following string is stored in the database:
<b>hello bold</b>
However, when I try to see that text in bold, I, instead see something like this:
"<b>hello bold</b>"
The goal (so I suppose) would be to remove those double quotes, in order to allow us to see the proper formatted text.
Here's the widget call:
<?php $this->widget('bootstrap.widgets.BsListView',array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
));
Here's the view he is calling:
<b><?php echo CHtml::encode($data->getAttributeLabel('description')); ?>:</b>
<?php echo $data->description; ?><!-- Removed the encode from this line-->
<br />
If I remove the CHtml::encode, this works, but, if I have like 500 textareas, should I go to each view and remove this CHtml::encode :s
Any clue?
You can solve this with PHP "html_entity_decode" function
take a look here : http://www.yiiframework.com/forum/index.php/topic/22237-clistview-raw-html/
<?php
$orig = "I'll \"walk\" the <b>dog</b> now";
$b = html_entity_decode($a);
echo $b; // I'll "walk" the <b>dog</b> now
?>
Ref: http://www.php.net/html_entity_decode
Update
You should remove chtml::encode when you print html tags contain data. thats the issue here.
The usage of encode() is to Encode special characters into HTML entities http://yiiframework.com/doc/api/1.1/CHtml#encode-detail

Modifying Yii Portlet Layout

Is it possible to modify the layout of a Yii Portlet?
Within my application I want to use a Portlet to allow for users to admin certain controllers and the Portlet with htmlOptions seems like a perfect fit but I'd like to change the layout/view it uses.
Any advice?
HtmlOptions let you add some options to the container tag.
This widget is not using view.
If you want to modify the presentation of the title, then you should override the method renderDecoration(). Its default code is:
protected function renderDecoration()
{
if($this->title!==null)
{
echo "<div class=\"{$this->decorationCssClass}\">\n";
echo "<div class=\"{$this->titleCssClass}\">{$this->title}</div>\n";
echo "</div>\n";
}
}
To customize the style you can change contentCssClass, decorationCssClass and titleCssClass
Then to change the content of the Portlet you have to override the method renderContent() that is empty by default. You can also set the content as follow:
<?php $this->beginWidget('zii.widgets.CPortlet'); ?>
...insert content here...
<?php $this->endWidget(); ?>

yii framework: how to i put a label to a radio button? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Yii CHtml::radioButtonList - CSS to align horizontally
Am new to yii framework and the doubt might be silly!
I want to write a radio button code with its label next to it!
My code is as follows
<div class="column">
<?php echo $form->radioButton($model,'radio',false); ?>
<?php echo $form->error($model,'radio'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'I need a room'); ?>
</div>
But it doesnt look nice! Is there any better way of doing this?
I got the code online! Sharing it of that it can be useful for someone in future
<?php echo $form->radioButtonList($model,'radio',array('m'=>'male','f'=>'female'),array('separator'=>'', 'labelOptions'=>array('style'=>'display:inline'))); ?>
The following code will create a label for the radiobutton created with the 'radio' attribute. Place this code after the 'echo $form->radiob.....'
<?php echo CHtml::activeLabel($model, 'radio', array('label' => 'My Label', 'style'=>'display:inline')); ?>
Use the display:inline or add a css class to get the label exactly where you want.

Yii: How to populate a Select input with another model data?

I'm playing around with a small app in order to learn to use Yii.
I've created a small webapp with 2 models / tables: Projects and tasks. (1-to-many relationship, properly configured in the model classes).
I'm now trying to customize the Task/create view, replacing the text input field with a select box proposing the list of available projects.
I opened the form view and tried this:
<div class="row">
<?php echo $form->labelEx($model,'project_id'); ?>
<?php echo $form->textField($model,'project_id'); ?>
<?php
// my hack starts here
$projects = Project::model()->findAll();
$list = CHtml::listData($projects, 'id', 'name');
echo $form->listBox($model,'project_id','', $list); ?>
// my hack ends here
<?php echo $form->error($model,'project_id'); ?>
</div>
But it keeps throwing warnings or error (such as Invalid argument supplied for foreach(), and definitely does not work. I'm failing to understand what i'm doing wrong. Can you help ?
Your arguments are not in order (it should be):
$frameworks = Framework::model()->findAll();
$list = CHtml::listData($frameworks, 'id', 'name');
echo $form->listBox($model,'framework_id', $list,array());
Check the documentation
OK, i found it, thanks to Larry Ullman excellent tutorial.
Here it is:
<?php echo $form->dropDownList($model,'project_id', CHtml::listData(Project::model()->findAll(), 'id', 'name')); ?>

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.