How to make custom Radio-Button labels in ZF2 forms? - php

I have a form with Radio-button:
$this->add([
'name' => 'time',
'options' => [
'value_options' => [
'0' => '9:00 - 12:00',
'1' => '12:00 - 16:00',
'2' => '16:00 - 19:00',
],
'label_attributes' => [
'class' => 'WW_OBJ_fm-label',
]
],
'type' => 'Radio'
]);
In the view I make the output like this:
<div>
<?php echo $this->formElement($form->get('time')); ?>
</div>
and get the output (formatted for readability):
<div>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="0"/>
9:00 - 12:00
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="1"/>
12:00 - 16:00
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="2"/>
16:00 - 19:00
</label>
</div>
But I need, that label text ist wrapped by a <span>:
<div>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="0"/>
<span class="WW_label-text">9:00 - 12:00</span>
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="1"/>
<span class="WW_label-text">12:00 - 16:00</span>
</label>
<label class="WW_OBJ_fm-label">
<input type="radio" name="time" value="2"/>
<span class="WW_label-text">16:00 - 19:00</span>
</label>
</div>
What is the best way to achieve it?

I see three possible solutions for your problem.
1) Extend the Zend\Form\View\Helper\FormRadio class, overriding the renderOptions method, replicating almost entirely the one that you can find in Zend\Form\View\Helper\FormMultiCheckbox but maybe adding an option to pass optional attributes to the span element
2) Very subtle, but could save you writing some code: using the translator. Since the radio value options are translated, you could keep your values defined in the configuration but adding the span element in the transation
3) Do not use $this->formElement to display the element, but actually write all the html

A solution is to use labelOption 'disable_html_escape' :
$this->add([
'name' => 'time',
'options' => [
'value_options' => [
'0' => '<span class="WW_label-text">9:00 - 12:00</span>',
'1' => '<span class="WW_label-text">12:00 - 16:00</span>',
'2' => '<span class="WW_label-text">16:00 - 19:00</span>',
],
'label_attributes' => [
'class' => 'WW_OBJ_fm-label',
]
],
'type' => 'Radio'
]);
$element = $this->get('time');
$element->setLabelOptions(['disable_html_escape' => true]);

Related

How to print a string into a array

I tried to print a string into a array.
The form will be submitted trough a array. The amount of forms is unknow because user is allowed to add extra inputs if needed (Order system).
To run my validation of the form it needs to be like this:
$toxValidate = new toxValidate();
$toxValidation = $toxValidate->check('$_POST', array(
'part1' => array('required' => TRUE),
'part2' => array('required' => TRUE),
'part3' => array('required' => TRUE),
'part4' => array('required' => TRUE),
'part5' => array('required' => TRUE)
));
But because we dont know how many inputs it will be, there needs for every input one line of: 'part1' => array('required' => TRUE),.
This is what i tried:
for($i=0; $i<count($part); $i++){
$parts .= "part".$i." => array('required' => true)";
$t0x = $t0x + 1;
if($t0x < count($part)){
$parts .= ', ';
}
}
This would be the var_dump output of $parts when submitting 3 forms
''part0' => array('required' => true), 'part1' => array('required' => true), 'part2' => array('required' => true)'
That looks exactly what needs to be printed in:
$toxValidation = $toxValidate->check($_POST, array(
#HERE#
));
i tried with print and echo which i allready tought wouldt work.
How can i print $parts into the array.
Update
HTML Form:
<div id="form">
<p class="order">
<label style="margin-left:20px;">Partnumber:</label><br />
<input type="text" style="margin-left:20px;" class="text medium" id="part" name="part[]" placeholder="SP partnumber" />
</p>
<p class="order" >
<label style="margin-left:20px">Qty</label><br>
<input type="text" id="qty" name="qty[]" style="margin-left:20px;" class="text small" placeholder="Qty" />
<p class="order">
<label style="margin-left:20px">Price</label><br>
<input type="text" style="margin-left:20px;" class="text small" id="price" placeholder="Price" tabindex="-1" readonly /><br>
</p>
</div><p>
jQuery to add a other line of inputs:
$('#addpart').click(function(){
var loop = $('#loop').val();
var html;
html = '<p>';
html += '<input type="text" name="part[]" style="margin-left:20px;" class="text medium" id="part" placeholder="SP partnumber" />';
html += '<input type="text" name="qty[]" style="margin-left:20px;" class="text small" placeholder="Qty" />';
html += '<input type="text" style="margin-left:20px;" class="text small" id="price" placeholder="Price" tabindex="-1" readonly />';
html += '</p>';
for (i = 0; i < loop; i++) {
$("#form").append(html);
}
});
Update 2
But there also needs to be other 'inputs' to be check. Like Purchase order, backorder, date. See below:
$toxValidation = $toxValidate->check('$_POST', array(
'po' => array('required' => TRUE),
'date' => array('required' => TRUE),
'bo' => array('required' => TRUE),
'remarks' => array('required' => TRUE),
#PARTS HERE#
));

Changing Radio button Input template using CakePHP 3

I would like change structure but it does not work, it's always the same structure. How to solve my problem?
<?php
$default = 'wepay';
$options = array('wepay' => 'WePay', 'paypal' => 'Paypal', 'donorbank' => 'Donor Bank');
echo $this->Form->input('payment_method', [
'templates' => [
'radioWrapper' => '<label class="radio-inline">{{label}}</label>'],
'type' => 'radio',
'options' => $options,
'default' => $default
]);
?>
But this code does not generate output properly. I want to generate output like below:
<label class="radio-inline">
<input type="radio" name="payment_method" value="wepay" id="payment-method-wepay" checked="checked">WePay
</label>
<label class="radio-inline">
<input type="radio" name="payment_method" value="paypal" id="payment-method-paypal">Paypal
</label>
<label class="radio-inline">
<input type="radio" name="payment_method" value="donorbank" id="payment-method-donorbank">Donor Bank
</label>
My Code generate output like below:
<label class="radio-inline">
<label for="payment-method-wepay">
<input type="radio" name="payment_method" value="wepay" id="payment-method-wepay" checked="checked">WePay
</label>
</label>
<label class="radio-inline">
<label for="payment-method-paypal">
<input type="radio" name="payment_method" value="paypal" id="payment-method-paypal">Paypal
</label>
</label>
<label class="radio-inline">
<label for="payment-method-donorbank"><input type="radio" name="payment_method" value="donorbank" id="payment-method-donorbank">Donor Bank
</label>
</label>
Here one extra label field added. How can I remove this extra-label?
What you are showing there is pretty much the default output, except for the class attribute and the missing for attribute on the label. So what you want to modify most probably is the nestingLabel template, surely not the radioWrapper one:
'nestingLabel' =>
'{{hidden}}<label class="radio-label"{{attrs}}>{{input}}{{text}}</label>'
If you want to remove the for attribute, then you'll have to remove the {{attrs}} placeholder, that way no further attributes can be added to the label element.
Finally, solved my problem using #ndm hints. Final solution like this below
<?php
$default = 'wepay';
$options = array('wepay' => 'WePay', 'paypal' => 'Paypal', 'donorbank' => 'Donor Bank');
echo $this->Form->input('payment_method', [
'nestingLabel' =>
'{{hidden}}<label class="radio-inline"{{attrs}}>{{input}}{{text}}</label>',
'type' => 'radio',
'options' => $options,
'default' => $default
]);
?>
Hope this solution help others in future.

Radiobutton group, change event?

I have a group of radiobuttons generated by cakephp.
<input type="radio" name="data[hexInput]" id="HexInput1" value="h1">
<input type="radio" name="data[hexInput]" id="HexInput2" value="h2">
<input type="radio" name="data[hexInput]" id="HexInput3" value="h3">
Is there some way to check if one of the radiobuttons in the group was checked? By a change event or something?
I know how to do this with a specific button but not with the group. For a specific button i use this Js helper:
$this->Js->get('HexInput2')->event('change', $this->Js->request(array(
'controller' => 'designer',
'action' => 'test',
), array(
'update' => '#resultDiv',
'async' => true,
'method' => 'post',
'dataExpression' => true,
'data' => $this->Js->serializeForm(array(
'isForm' => false,
'inline' => true
))
))
);
Just create an onclick for each radio button. You could do something like this:
function update(val) {
alert("Radio button changed to " + val);
}
Then just tie it to each of the inputs like this:
<input onclick="update(this.value);" type="radio" name="data[hexInput]" id="HexInput1" value="h1">
<input onclick="update(this.value);" type="radio" name="data[hexInput]" id="HexInput2" value="h2">
<input onclick="update(this.value);" type="radio" name="data[hexInput]" id="HexInput3" value="h3">
I think you are looking for something like this
http://www.java2s.com/Code/JavaScript/Form-Control/FindingtheSelectedButtoninaRadioGroup.htm

Cakephp - Checkbox names

Currently, i have succeeded in creating the checkbox.
The array which i have setup is as below:
$emailName = $this->User->find('list', array(
'fields' => array('User.username', 'User.email')
));
The output is as follows:
array
'admin' => string 'asd#asd.asd' (length=11)
'test' => string 'test#test.test' (length=14)
'Floo' => string 'XXXX#gmail.com' (length=16)
I'm trying to make the checkbox shows the username instead of the user email in view.ctp.
I have tried using the following code in view.ctp
<?php echo $this->Form->input('Address_list.['.$emailName['username'].']', array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => $emailName['email']
)); ?>
However, it seems that this doesn't work. Any ideas?
You are not formatting your form field for the checkbox list correctly. Try changing it to this:
echo $this->Form->input('Address_list', array(
'multiple' => 'checkbox',
'options' => $emailName,
));
However, this will return the value of Username based on the selection of Email the user chooses. It creates the form like this:
<label for="Address_list">Address List</label>
<input type="hidden" id="Address_list" value="" name="data[Address_list]"/>
<div class="checkbox"><input type="checkbox" id="AddressListAdmin" value="admin"
name="data[Address_list][]"/><label for="AddressListAdmin">asd#example.com</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListTest" value="test"
name="data[Address_list][]"/><label for="AddressListTest">test#example.com</label></div>
<div class="checkbox"><input type="checkbox" id="AddressListFloo" value="Floo"
name="data[Address_list][]"/><label for="AddressListFloo">XXX#example.com</label></div>

Zend Form How to group element like this?

I created a zend form. I want to make a group of elements that output html like this:
<div class="form_wrapper">
<div class="form_label">
<label class="txtlabel" for="salary_type">Salary type</label>
<span>*</span>
</div>
<div class="form_element">
<select class="jp_select" id="salary_type" name="salary_type">
<option>..1..</option>
<option>2</option>
<option>3</option>
</select>
<input type="text" id="salary_min" class="txtinput" name="salary_min" value="Min" onfocus="if(this.value=='Min') this.value='';" onblur="if(this.value=='') this.value=('Min');">
<input type="text" id="salary_max" class="txtinput" name="salary_max" value="Max" onfocus="if(this.value=='Max') this.value='';" onblur="if(this.value=='') this.value=('Max');">
</div>
</div> <!-- end .form_wrapper -->
My code:
$this->addElement('select', 'salary_type', array(
'label' => 'Salary type',
'multiOptions' => $salary_types,
'class'=>'jp_select'
));
$this->addElement('text', 'salary_min', array(
'label' => 'Min',
'class'=>'txtinput'
));
$this->addElement('text', 'salary_max', array(
'label' => 'Max',
'class'=>'txtinput'
));
$this->addDisplayGroup(array('salary_type','salary_min','salary_max'), 'salary_group');
How can I set decorators to have html output like above
As far as I know, you are not able to render that exact html structure with simple Decorators alone.
One way to solve this problem is by using the ViewScript Decorator within your displayGroup decorator stack. Within the view script, you are able to retrieve the individual elements by using $this->element->getElements(); ($this->element is an instance of Zend_Form_DisplayGroup)
For example:
$this->addDisplayGroup(
array('salary_type','salary_min','salary_max'),
'salary_group',
array(
'decorators' => array(
array('ViewScript', array('viewScript' => 'path/to/viewscript.phtml')),
),
)
);
Haven't tried it myself, but it should work.

Categories