Combine link & image - lithium

how can i combine the link and image helper in lithium? I want something like:
<a href="http://...">
<img src="mypic.png" />
And a title
</a>
I tried different options but nothing seems to work, do i have to write my own helper?
<?php
$image = $this->html->image('mypic.png');
echo $this->html->link('And a title', '', array('html' => $image));
?>

You should escape html code
$image = $this->html->image('mypic.png');
$this->html->link($image, $url, array('escape' => false))
http://li3.me/docs/lithium/template/helper/Html::link()

Related

how to make a list selected in my dropdownlist yii?

I get a list of email address from my query and i want the list to be selected by default in my form.How to achieve that? My code is
<div class="row col2">
<?php echo $form->labelEx($model,'email_to'); ?>
<?php
foreach ($mailList as $eachValue){
$selectedOptions[$eachValue] = array('selected' => 'selected');
}
echo $form->dropDownList($model,'email_to',$mailList,array('class'=>'span4 chosen','maxlength'=>20,'multiple' => 'multiple','options'=>$selectedOptions,'readonly'=>true));
?>
<?php echo $form->error($model,'email_to'); ?>
</div>
My $mailList contains only email address.
Your code is fine except the foreach loop:
foreach ($mailList as $eachValue){
$selectedOptions[$eachValue] = array('selected' => 'selected');
}
Assuming $mailList is an array like:
$mailList = array("a#a.com", "b#b.com", "c#c.com");
You have to preselect the value not the content of the option tag, so, modify your foreach something like:
foreach ($mailList as $optionKey=>$optionVal) {
if ($optionVal) {
$selectedOptions[$optionKey] = array('selected' => 'selected');
}
}
This will add the selected attribute to the array keys.

changing my forms get url params

i made a simple form with checkboxlist() when i submit the form using get it goes to the next page, but the url is nasty looking.
http://mysite/dev/research/abc/all/?Abc[type]=N&Abc[type][]=1&Abc[type][]=2&Abc[type][]=3&yt0=Search
how would i change it to something like this?
http://mysite/dev/research/abc/all/?type=N&type[]=1&type[]=2&type[]=3
or if possible something nicer looking like
?type=1,2,3
note that i don't want &yt0=Search either
in my controller
public function actionIndex()
{
$model=new ABC;
$this->render('index',array(
'model'=>$model
));
}
in my view i have this
<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'shop',
'action'=>$this->createUrl('/modulename/abc/all'),
'method'=>'get',
'enableAjaxValidation'=>false,
));
echo $form->checkBoxList($model,'type', $arr,
array('separator'=>'',
'template'=>'<div class="col-md-6 col-xs-12">{input} {label}</div>',
'uncheckValue'=>N,
)
);
echo CHtml::submitButton('Search', array('class'=>'btn btn-success'));
echo CHtml::button('Clear Filter', array('class'=>'btn btn-link', 'type'=>'reset'));
$this->endWidget();
?>
UPDATE:
figured it out. had to add 'name'=> 'type'
but how do i remove &yt0=Search
echo $form->checkBoxList($model,'type', $arr,
array('name'=> 'type',
'separator'=>'',
'template'=>'<div class="col-md-6 col-xs-12">{input} {label}</div>',
'uncheckValue'=>N,
you could use CHtml:button to not post the &yt0=Search
<?php
echo CHtml::button('Search',
array(
'submit'=>array('/modulename/abc/all'),
'class'=>'btn btn-success'
)
);
?>

Yii CActiveForm expects object and not array

My Goal:
I am trying to filter a model, based on a GET parameter, and populate a form based on this.
What I require is that when users select the update icon on the Gridview
I will
grab the 'telephone number' they want to edit,
populate a form with the telephonenumber's data
allow the user to edit this data for the telephonenumber and submit
I then run my own custom sql update query based on the new data.
My Problem
My gridview can successfully capture the telephonenumber for a selected row.
It can successfully send this to the Controller Update method (see below)
I cannot however filter a model based on this telephonenumber and then populate a form with this model.
My Error
get_class() expects parameter 1 to be object, array given
/framework/web/helpers/CHtml.php(2220)
/framework/web/helpers/CHtml.php(2220): get_class(array())
/framework/web/helpers/CHtml.php(1236): CHtml::resolveName(array(), "TelephoneNumbers_TelephoneNumber")
/framework/web/widgets/CActiveForm.php(562): CHtml::activeLabelEx(array(), "TelephoneNumbers_TelephoneNumber", array())
/views/dateAudiid/editupdateform.php(18): CActiveForm->labelEx(array(), "TelephoneNumbers_TelephoneNumber")
/framework/web/CBaseController.php(126): require("/var/www/OMReport/protected/views/dateAudiid/editupdateform.php")
Here's my Gridview.
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'dateaudiidcondensed-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'class'=>'bootstrap.widgets.TbButtonColumn',
'template'=>'{update}',
'buttons'=>array
(
'update' => array
(
'url'=>'Yii::app()->createUrl("dateAudiid/Update/",array("TelephoneNumbers_TelephoneNumber"=>$data->TelephoneNumbers_TelephoneNumber))',
),
),
),
'TelephoneNumbers_TelephoneNumber',
'FormId_Formid',
'Date',
'AudibeneID_Audibene_ID'
),
));
Here's my Controller
public function actionUpdate($TelephoneNumbers_TelephoneNumber)
{
$criteria=new CDbCriteria;
$criteria->compare('TelephoneNumbers_TelephoneNumber',$_GET['TelephoneNumbers_TelephoneNumber'],true);
$criteria->limit = 1;
$criteria->offset = 1;
$model = DateAudiidCondensedByAudibeneId::model()->findAll($criteria);
$this->render('editupdate',array('model'=>$model));
}
Here's my View
<?php
/* #var $this DateAudiidController */
/* #var $model DateAudiidCondensedByAudibeneId */
?>
<h1>Update Assignments </h1>
<?php echo $this->renderPartial('editupdateform', array('model'=>$model)); ?>
Here's my Form
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'date-audiid-condensed-by-audibene-id-customupdate-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php
echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'TelephoneNumbers_TelephoneNumber'); ?>
<?php echo $form->textField($model,'TelephoneNumbers_TelephoneNumber'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'FormId_Formid'); ?>
<?php echo $form->textField($model,'FormId_Formid'); ?>
<?php echo $form->error($model,'FormId_Formid'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'Date'); ?>
<?php echo $form->textField($model,'Date'); ?>
<?php echo $form->error($model,'Date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'AudibeneID_Audibene_ID'); ?>
<?php echo $form->textField($model,'AudibeneID_Audibene_ID'); ?>
<?php echo $form->error($model,'AudibeneID_Audibene_ID'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
My Thoughts / What I've tried so far
I am returning the model as an array...but my form wants it as an object.
I need to change something in the way I filter my model in the Controller Update action, however I cannot see how to do this.
All my other methods use similar model filtering code.
here is what you can do to fix this :
your form is trying to use models labeling but you have given an array, ->findAll() will return an array of models, here you need a single object of model
the error your having is because $model in filter of your grid needs to be an object of model which here will try to validate using this model, so won't work when you are giving it an array of your models,
if you want to filter the results based on something, you need to do this where the dataprovider of your grid is being populated, so in this case, is in $model->search()
get the parameter and append it to that criteria
so your grid could look like this:
$this->widget('bootstrap.widgets.TbGridView', array(
'id'=>'dateaudiidcondensed-grid',
'dataProvider' => $model->search(), //create a new model with search scenario
'filter' => $model, // here use that model to validate fields
'columns'=>array(
.
.
.
),
));
and in your models search method:
public function search() {
$criteria = new CDbCriteria;
// grab the sent data and use it here
$tel = Yii::app()->request->getParam('TelephoneNumbers_TelephoneNumber' , null);
if(!empty($tel))
$criteria->compare('TelephoneNumbers_TelephoneNumber' , $tel , true);
.
.
.
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Here's what eventually worked for me
The usage of 'findByAttributes' to filter and return a model to the Form
public function actionUpdate($TelephoneNumbers_TelephoneNumber)
{
$model = DateAudiidCondensedByAudibeneId::model()->findByAttributes(array("TelephoneNumbers_TelephoneNumber" => $_GET['TelephoneNumbers_TelephoneNumber']));
$this->render('editupdate',array('model'=>$model));
}

ZF2 form element description

in ZF1 form and form elements had a setDescription method that was outputted as <p>description here</p> in view ....
ZF2 seems that doesn't have this method
so my question is how can i add description to form elements ?
this is my view :
<div>
<?
$form = $this->form;
$form->prepare();
echo $this->form()->openTag($form);
foreach ($form->getElements() as $el) {
?>
<div class="form_element">
<?=$this->formRow($el);?>
</div>
<?
}
echo $this->partial('system/form/buttons_form_part.phtml', array('form' => $form));
echo $this->form()->closeTag();
?>
</div>
Using ZF 2.1.5, one solution might be setOptions().
In the form definiton:
$file = new Element\File('file');
$file->setLabel('Photo (.jpg, .gif, .png)');
$file->setOptions(array('description' => 'test description'));
…
When rendering the form element:
$element = $form->get(…);
var_dump($element->getOptions());
Will give you access to:
array(1) { ["description"]=> string(16) "test description" }
If you mean a label for an Element you can use the setLabel method when you create the form (in Controller).
$name = new Element('name');
$name->setLabel('Your name');
Or if you use an array to create your form elements use this:
array(
'spec' => array(
'name' => 'name',
'options' => array(
'label' => 'Your name',
),
'attributes' => array(
'type' => 'text'
),
)
),
Here is a link:
enter link description here

How to add span to a label using $form->labelEx from Yii?

I wish to generate the following markup:
<label>some label here <span>(optional)</span></label>
Is there a way to do this, by using Yii labelEx or just label?
Thanks
If the attribute is required
You can set the CHtml static properties like this
CHtml::$beforeRequiredLabel = '';
CHtml::$afterRequiredLabel = '<span>(optional)</span>';
Yii will generate a tag
<label>CHtml::$beforeRequiredLabel.$label.CHtml::$afterRequiredLabel</label>
but the static properties will effect all labels generated by $form->labelEx() (#see CHtml::activeLabelEx())
if you don't need, you must set the static properties to default
If the attribute is not required
you can set the htmlOptions
$form->labelEx($model, $attribute, array('label' => 'your label'))
It's simple, make use of the Decorator pattern:
<?php
// components/CustomCHtml.php
class CustomCHtml extends CHtml
{
public static function activeLabelEx($model,$attribute,$htmlOptions=array())
{
if (isset($htmlOptions['optional']) && $htmlOptions['optional']) {
if (isset($htmlOptions['label']) && $htmlOptions['label']) {
$htmlOptions['label'] .= $htmlOptions['optional'];
} else {
$htmlOptions['label'] = $htmlOptions['optional'];
}
}
return parent::activeLabelEx($model,$attribute,$htmlOptions);
}
}
use case:
<?php echo CustomCHtml::activeLabelEx($model, 'field', array('optional'=>' <span>(optional)</span>')); ?>
Here is a little hack to make it possible.
<?php
$username = $form->field($model, 'username')
->input('text', ['class' => 'form-control input-lg'])
->label(true, 'username', ['class' => 'fa fa-user signin-form-icon']);
$username->parts['{label}'] = '<span class="fa fa-user signin-form-icon">dwe</span>';
echo $username;
?>
You could use CHtml::$afterRequiredLabel and/or CHtml::$beforeRequiredLabel
$initValue = CHtml::$afterRequiredLabel;
CHtml::$afterRequiredLabel = $initValue.'<span>(optional)</span>';
echo $form->labelEx($model, $fname, array('class' => ''));
echo $form->textField($model, $fname, array('class' => ''));
CHtml::$afterRequiredLabel = $initValue;
This way the label will be applied only on the specified form field. You actually set the initial value of CHtml::$afterRequiredLabel and/or CHtml::$beforeRequiredLabel on a temp variable, you overwrite the value that you need, and then reset it to original value.