Create a label/value (readonly) fieldset using zend framework 2 - php

I just want to create a Label/value row like this:
<label>
<span>My Label:</span><span>My value from my object</span>
</label>
So what should I put in the type attribute ?
$this->add(array (
'name' => 'myFieldName',
'attributes' => array(
'type' => '???????',
),
'options' => array (
'label' => 'My Label:',
),
));

$this->add(array (
'name' => 'myFieldName',
'type' => 'hidden',
'options' => array (
'label' => 'My Label:',
),
));
And in view
<?php $element = $form->get('myFieldName') ?>
<label>
<span><?php echo $element->getLabel() ?></span>
<span><?php echo $this->escapeHtml($element->getValue()) ?></span>
</label>

Related

How to read only some form fields in php using yii framework

I am new at php and yii framework.can any one help me with my form.I have a update form which has 3 fields with drop down menu.how to make the from field value read only.it will be very much helpfull if any one provide me with code.here is my update form code:
Update form:
<div class="row">
<?php
$criteria=new CDbCriteria();
echo $form->dropDownListGroup(
$model,
'user_id',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(User::model()->findAll($criteria), 'id', 'user_id'),
'dataProvider'=>$model->searchByUserId(Yii::app()->user->getId()),
'htmlOptions' => array('prompt'=>'Select'),
)
)
); ?>
</div>
<div class="row" id="jobTitle">
<?php
$criteria = new CDbCriteria;
$criteria->condition = "status= 'active'";
echo $form->dropDownListGroup(
$model,
'job_title_id',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(JobTitle::model()->findAll($criteria), 'id', 'name'),
'htmlOptions' => array('prompt'=>'Select job title'),
)
)
); ?>
</div>
<div class="row" id="file_name">
<?php echo $form->textFieldGroup(
$model,'file_name',
array(
'wrapperHtmlOptions' => array(
'class'=> 'col-sm-5',
),
)
);
?>
</div>
<div class="row" id="statustype">
<?php
$is_current = array('yes'=>'Yes', 'no'=>'No');
echo $form->dropDownListGroup(
$model,
'is_current',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => $is_current,
'htmlOptions' => array('prompt'=>'Select a status'),
)
)
); ?>
</div>
To make a value readonly - add 'readonly'=>true to the options array of the field you want to make readonly.
For example:
<?php
$is_current = array('yes'=>'Yes', 'no'=>'No');
echo $form->dropDownListGroup(
$model,
'is_current',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => $is_current,
'htmlOptions' => array('prompt'=>'Select a status'),
)
'readonly' => true
)
);
?>
It's impossible to set select element readonly. With Yii you can set it disabled and add value into hidden field using unselectValue.
echo $form->dropDownListGroup(
// ...
array(
// ...
'widgetOptions' => array(
'htmlOptions' => array(
'disabled' => 'disabled',
'unselectValue' => $model->user_id,
),
)
)
);
Or use TbSelect2 it has readonly property.

Remove model name from submitted form url (GET)

Here's my form
<?php $form=$this->beginWidget('booster.widgets.TbActiveForm',array(
'id'=>'listing-main-form',
'enableAjaxValidation'=>false,
'action'=>Yii::app()->createUrl('site/search'),
'method'=>'get',
)); ?>
<div class="form-group" style="padding-bottom:0px;border:none">
<label class="control-label" for="selecttype">Type</label>
<?php echo $form->dropDownListGroup(
$model,
'prp',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(Type::model()->findAll(), 'id', 'type'),
'htmlOptions' => array('id'=>'selecttype'),
),
'label' => ''
)
); ?>
</div>
<div class="form-group">
<div id="resproperties">
<div class="resdv">
<?php echo $form->checkboxListGroup(
$model,
'rs',
array(
'widgetOptions' => array(
'data' =>CHtml::listData(ResourceCategory::model()->findAll(), 'id', 'res_category'),
),
'label' => ''
)
); ?>
</div>
</div>
............
............
When the form is submitted, I can read all the field's data fine. But the url appears with Model[field] for each fields and looks very ugly (see below). Is there any where I can remove the model name from there?
index.php?r=site/search&ItemModel[prp]=1&ItemModel[rs]=&ItemModel[rs][]=2&ItemModel[rs][]=3&ItemModel[rs][]=4&ItemModel[cm] ............
You can explicitly set input name.
...
'htmlOptions' => array(
'id'=>'selecttype',
'name' => 'fieldname'
)
...
Also you can override CHtml and CActiveForm classes.
In your array for each element, add
'name'=>'your_custom_name'
So...
<?php echo $form->dropDownListGroup(
$model,
'prp',
array(
'wrapperHtmlOptions' => array(
'class' => 'col-sm-5',
),
'widgetOptions' => array(
'data' => CHtml::listData(Type::model()->findAll(), 'id', 'type'),
'htmlOptions' => array('id'=>'selecttype'),
),
'label' => '',
'name' => 'customName'
)
); ?>

Generate radio with cakephp 1.3

I need to generate Radio button with cake php format like this
<div class="radioset gender">
<div>
<div>Male</div>
<label>
<input type="radio" name="sex" value="1"/>
</label>
</div>
<div>
<div>Female</div>
<label>
<input type="radio" name="sex" value="0"/>
</label>
</div>
I can not use
e($form->radio("User.sex",array(0=>"Male",1=>"Female"),array("legend"=>false)));
Because it can not generate HTML format that i want.
I use multi input like that
e($form->input( 'User.sex', array('type'=>'radio', 'options' => array(0=>""),'div'=>false, "error"=>false, 'label' => false ) ));
e($form->input( 'User.sex', array('type'=>'radio', 'options' => array(1=>""),'div'=>false, "error"=>false, 'label' => false ) ));
But when i submit form. Server can not get value.
Please help me how to do it.
Thanks.
Use this format:
echo $form->input('field', array(
'type' => 'radio',
'legend'=>'Group of Radio',
// 'after' => '--after--',
// 'between' => '--between---',
'separator' => '--separator--',
'default' => '--which is by default selected--',
'options' => array('Button One', 'Button Two')
));
you can do this with Separator. I.e
echo $this->Form->input('name', array( 'type' => 'radio',
'before' => '<div>',
'separator'=> '</div><div>',
'after' => '</div>',
'options' => $option,
'label' => true,
"legend" => false
)
);

How i get the real value in select form zf2

I need to get the real value, instead of the number position of a Select form field
I explain it with one example:
first in the form
$procesa=new Querys();
$datosAsignaturas=$procesa->getDatosAsignaturas();
$groups = array();
foreach ($datosAsignaturas as $id => $list) {
$groups[$id] =$list["nombreA"];
}
$selection= $factory-> createElement(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'subjects',
'attributes' => array(
'id' => 'subjects',
'options' => $groups
),
));
$this->add($selection);
Second, the view
<div class="form_element">
<?php $element = $form->get('subjects');
?>
<label>
<?php echo $element->getOption('value'); ?>
</label>
<?php echo $this->formSelect($element); ?>
</div>
third
["subjects"]=> string(1) "1"
i need something like this
["subjects"]=> string(1) "Maths"
Proper assignement of values to a Zend\Form\Element\Select is through the means of value_options inside options or using the element function setValueOptions(). Here a simple example:
$form->add(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'language',
'options' => array(
'label' => 'Which is your mother tongue?',
'empty_option' => 'Please choose your language',
'value_options' => array(
'0' => 'French',
'1' => 'English',
'2' => 'Japanese',
'3' => 'Chinese',
),
)
));
Now if you need to access the value options like this, you simply call getValueOptions() on the element and you'll receive exactly the same array as above. Then you could do something like this (which I'm assuming is what you're trying to do):
$elemLanguage = $form->get('language');
echo "<select name='{$elemLanguage->getName()}'>\n";
foreach($elemLanguage->getValueOptions() as $id => $language) {
echo "<option value='{$id}'>{$id} - {$language}</option>\n";
}
echo '</select>';
Maybe you mean :
<div class="form_element">
<?php $element = $form->get('subjects');
?>
<label>
<?php echo $element->getOption('label'); //-------- label instead value?>
</label>
<?php echo $this->formSelect($element); ?>
</div>
And about the form
$procesa=new Querys();
$datosAsignaturas=$procesa->getDatosAsignaturas();
$groups = array();
foreach ($datosAsignaturas as $id => $list) {
$groups[$id] =$list["nombreA"];
}
$selection= $factory-> createElement(array(
'type' => 'Zend\Form\Element\Select',
'name' => 'subjects',
'attributes' => array(
'id' => 'subjects',
'options' => $groups
),
'options'=>array(
'label'=>"Your label",
'description' => 'your description',
'value_options' => array(
'0' => 'French',
'1' => 'English',
'2' => 'Japanese',
'3' => 'Chinese',
),
)
));
$this->add($selection);

How to add attributes to a label generated with Zend/Form in Zend framework 2

I'm adding forms to my page using Zend/Form.
I'm adding elements by defining them as follows:
$this->add(array(
'name' => 'value',
'attributes' => array(
'type' => 'text',
'id' => 'value',
'autocomplete' => 'off',
'placeholder' => 'Cost',
),
'options' => array(
'label' => 'Cost',
),
));
As you can see there is a 'label' => 'cost' node, this generated a label to go with the input element.
How do I add classes, attributes to this label ?
Please try this, i haven't tested or used this, but going by the source it should function properly:
$this->add(array(
'name' => 'value',
'attributes' => array(),
'options' => array(
'label_attributes' => array(
'class' => 'mycss classes'
),
// more options
),
));
If this does not function, please leave me a comment. If it won't function, it is not possible using this approach, since the FormLabel restricts the validAttributes quite a bit:
protected $validTagAttributes = array(
'for' => true,
'form' => true,
);
This works well in Zend Framework 2.3 :
$this->add(array(
'name' => 'userName',
'attributes' => array(
'type' => 'text',
'class' => 'form-control',
'placeholder' =>'Username',
),
'options' => array(
'label' => 'Username',
'label_attributes' => array('class' => 'control-label')
),
));
$element->setOptions(array('label_class' => array('class' => 'control-label')));
Produces code like this:
<label class="control-label">
<input type="radio" name="option1" id="option1" value="1">
Option 1
</label>
<label class="control-label">
<input type="radio" name="option2" id="option2" value="2">
Option 2
</label>
I have tried this. It works in Zend Framework One.
Note if you use
$element->setOptions(array('label_attributes' => array('class' =>
'control-label')));
you get the undesirable effect for some reason of
<label attributes="control-label">
<input type="radio" name="option1" id="option1" value="1">
Option 1
</label>
For programmatic approach on ZF2+ try this:
$element->setOptions(array(
'label_attributes' => array(
'style' => 'color:gray;'
)
));
Inspired by Damon's answer.

Categories