div => false is not working in CakepPHP input() FormHelper - php

I am using the last version of cakephp, with the follow code I have to create a list of checkboxes.
echo $this->Form->input('regions', array(
'type' => 'select',
'hiddenField' => false,
'options' => $regions,
'multiple' => 'checkbox',
'div' => false
));
the code works 90%, I mean... the list has been created BUT I still see <div>
This is the result:
<div class="checkbox"><input type="checkbox" name="data[regions][]" value="1" id="Regions1" /><label for="Regions1">AAAA</label></div>
<div class="checkbox"><input type="checkbox" name="data[regions][]" value="2" id="Regions2" /><label for="Regions2">BBBB</label></div>
<div class="checkbox"><input type="checkbox" name="data[regions][]" value="3" id="Regions3" /><label for="Regions3">CCCC</label></div>
The result I need is:
<li>
<input type="checkbox" name="data[regions][]" value="1" id="Regions1" /><label for="Regions1">AAAA</label>
</li>
...
How can I do it ?

By setting 'div' => false, your prevents creating a <div> around the whole input section (i.e. the set of checkboxes). But obviously you want to disable the div's around the option. Unfortunately, I have not been able to find a way to disable this with Cake.
However, you can simulate the <li> items with some CSS trickery. Encapsulate the inputs in a div with a special class (the opposite of what you are doing now), then use CSS to force using <li> styling:
In your CSS:
.box2li div
{
display: list-item;
}
In your Cake view:
echo $this->Form->input('regions', array(
'type' => 'select',
'hiddenField' => false,
'options' => $regions,
'multiple' => 'checkbox',
'div' => array ('class' => 'box2li')
));
Each checkbox is now preceded by a... ...bullet

There should be a more simple way but you can always do it in the traditional way :)
while (list($key, $value) = each($regions)){
echo '<li>'.
$this->Form->input($value,
array(
'type' => 'checkbox',
'name' => 'data[regions][]',
'div' => false,
'value' => $key,
'label' => false,
'after' => $this->Form->label($value, $value)
))
.'</li>';
}
Not so beautiful, but works :)

Use Form->Checkbox instead of Form->input
$this->Form->checkbox('', array(
'label' => false,
'div' => false,
'class' => ''
));

$this->Form->checkbox('', array(
'label' => false,
'div' => false,
'class' => ''
));
$this->Form->checkbox('', array(
'label' => false,
'div' => false,
'class' => ''
));

Related

how to make array element readonly in php

hello I was trying to make text box on the page read-only and I tried what exactly what I think is enough but the text box still writeable and I still confused and don't know what I missed there.
I tried to write
'readonly' => true,
'readonly' => 'true',
'readonly' => 'readonly',
and all didn't worked for me :(
function my_custom_checkout_field( $checkout ){
echo '<div id="EATM_custom_checkout_field">';
echo '<input class="EATM_checkout_exchange_rate" value="'.$this->exchange_data['EATM_exchange_rate'].'" hidden/>';
echo sprintf('<div class="EATM_container-payments"><div class="EATM_container-image-checkout"><img src="%1$s" /></div>',$this->payment_images[ str_replace(' ','_',$this->exchange_data['EATM_payment_send_from']) ] );
echo "<div class='payment'>";
woocommerce_form_field( 'my_field_name_1', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('ارسال'),
'required' =>true,
'readonly' =>true,
'value' =>$this->exchange_data['EATM_payment_send_from'],
'placeholder' => __('Send'),
), $this->exchange_data['EATM_payment_send_from']);
use the custom_attributes tag:
function my_custom_checkout_field( $checkout ){
// ...
woocommerce_form_field('my_field_name_1', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('ارسال'),
'required' => true,
'custom_attributes' => ['readonly' => true], // or ['disabled' => true]
'default' => $this->exchange_data['EATM_payment_send_from'],
'placeholder' => __('Send'),
), $this->exchange_data['EATM_payment_send_from']);
Also, I think the value field should be default or use the last parameter (like you are doing) and remove the value line entirely.
Documentation here: https://rudrastyh.com/woocommerce/woocommerce_form_field.html
To make a textbox readonly add
disabled
like this
<input type="text" value="1" class="form-control" disabled="">

zf2 Checkbox rendering by formInput fails

I want to make a chekbox for terms of use. I've found an interesting solution, given by a webcoder How to validate a checkbox in ZF2
The case is that I have a difficult HTML structure, that's why I can't use neither formRow, nor formCollection to render. I'm trying to use the following approach instead:
$agreement = $form->get('agreement');
echo $this->formInput($agreement);
echo $this->formElementErrors($agreement);
And I receive the empty value:
<input type="checkbox" name="agreement" class="checkbox" value="">
I also tried to add hidden field, but my attempt also failed:
$agreement = $form->get('agreement');
echo $this->formHidden($agreement);
echo $this->formInput($agreement);
echo $this->formElementErrors($agreement);
The html is:
<input type="hidden" name="agreement" class="checkbox" value="">
<input type="checkbox" name="agreement" class="checkbox" value="">
As a result I receive errors about "Value is required and can't be empty"
May be someone can give me a tip how to properly render checkbox in my case?
code of my inputFilter:
<?php
namespace Auth\Form;
use Zend\InputFilter\InputFilter;
use Zend\Validator\Digits;
class UserFormFilter extends InputFilter {
public function __construct() {
$this->add(array(
'name' => 'agreement',
'validators' => array(
array(
'name' => 'Digits',
'break_chain_on_failure' => true,
'options' => array(
'messages' => array (
Digits::NOT_DIGITS => 'You must agree to the terms of use.',
)
),
),
),
));
}
}
?>
Check this out please.
$this->add(array(
'type' => 'Zend\Form\Element\Checkbox',
'name' => 'agreeterms',
'options' => array(
'label' => 'I agree to all terms and conditions',
'use_hidden_element' => true,
'checked_value' => 1,
'unchecked_value' => 'no'
),
'attributes' => array(//the difference is here
'value' => 'yes'
)
));
if you add the attributes array it should work.

Before submit at least one checkbox is required

I have shown multiple checkboxes. When I submit I need at least one checkbox is required.
I tried this:
$fieldset->addField('checkboxes', 'checkboxes', array(
'label' => Mage::helper('listings')->__('Select Categories'),
'name' => 'cat_ids[]',
'values' => $categorieslist,
'onclick' => "",
'onchange' => "",
'value' => $cat_ids,
'disabled' => true,
'class' => 'category_match_to_listing',
'required' => true,
));
even class name also not displayed in html
<input type="checkbox" value="14" name="cat_ids[]" id="checkboxes_id">
If I use one value 'values' => 'single', required field working fine, then the html is
<input type="checkbox" value="0" class="category_match_to_listing required-entry" name="cat_ids[]" id="checkboxes_id">
When I get dynamic it doesn't work. Please suggest any idea.
Use validate-one-required instead of required entry

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 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