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.
Related
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
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
)
);
I want to use angularJS in a zend framework project and in this project forms are generated using zend form. How can I add angular directive such as "ng-model" in the form elements but whenever i was trying to add this custom attribute in the zend-form elements (input, select etc) in view I am not getting this attribute ----
Here is my lead form
class LeadForm extends Form {
public function __construct() {
parent::__construct('lead_form');
$this->setAttributes(array(
'action' => '',
'method' => 'post',
'name' => 'lead_form',
'id' => 'lead_form',
'class' => 'smart-form',
'role' => 'form',
'novalidate' => 'novalidate'
));
$this->add(array(
'name' => 'first_name',
'type' => 'text',
'options' => array(
'label' => 'First Name',
),
'attributes' => array(
'class' => 'form-control validate[required,custom[onlyLetterSp]]',
'placeholder' => 'First name',
**'ng-model' => "first_name", // this attribute is not generating in view**
),
));
}
}
Here is my controller which is calling this form and send to view for displaying
$createLeadForm = new \Admin\Form\LeadForm();
return new ViewModel(array(
'form' => $createLeadForm,
));
Here is my code in view for showing element
<?php echo $this->formInput($this->form->get('first_name')); ?>
But after prinnting this form element i am getting see no "ng-model" in the input element
<input type="text" value="" placeholder="First name" class="form-control validate[required,custom[onlyLetterSp]]" name="first_name" >
I have set one attribute "ng-model " but this attribute is not appearing in the view
I want this element like (using zend form ) -
<input type="text" ng-model="first_name" value="" placeholder="First name" class="form-control validate[required,custom[onlyLetterSp]]" name="first_name" >
How can I do that using zend form and what need to change in this form and why i can not able to add custom attribute? please help me.
Thanks in Advance
You always can use data-ng-*:
$this->add(array(
// ...
'attributes' => array(
// ...
'data-ng-model' => 'first_name',
),
));
Zend does not support other attribute if that attribute start other than data-* so for adding attribute for ng-* I had to modify the View Helper class
under this namespace ( Zend\Form\View\Helper ) inside the main library you will find a class named "AbstractHelper" and in this class you can get a method named "prepareAttributes" and here you have to change the following lines -
if (!isset($this->validGlobalAttributes[$attribute])
&& !isset($this->validTagAttributes[$attribute])
&& 'data-' != substr($attribute, 0, 5)
)
to
if (!isset($this->validGlobalAttributes[$attribute])
&& !isset($this->validTagAttributes[$attribute])
&& 'data-' != substr($attribute, 0, 5)
&& 'ng-' != substr($attribute, 0, 3)
)
see here I add
&& 'ng-' != substr($attribute, 0, 3)
so that ng-* attributes can be added using zend form
Thanks Again
This worked for me.
class FooForm extends Form
{
public function __construct(LookupService $lookupService)
{
parent::__construct('fooForm');
$this->setAttribute('novalidate', true);
}
...
}
Try this: (data attribute)
ZF2:
public function __construct() {
parent::__construct('lead_form');
$this->setAttributes(array(
'action' => '',
'method' => 'post',
'name' => 'lead_form',
'id' => 'lead_form',
'class' => 'smart-form',
'role' => 'form',
'novalidate' => 'novalidate'
));
$this->add(array(
'name' => 'first_name',
'type' => 'text',
'options' => array(
'label' => 'First Name',
),
'attributes' => array(
'class' => 'form-control validate[required,custom[onlyLetterSp]]',
'placeholder' => 'First name',
'data-ng' => json_encode(
array(
'ng-model' => 'first_name',
'ng-123' => '123',
), true
)
),
));
}
Call this JS function after your HTML-Manipulation:
/**
* ZF2 supports no custom attributs
* this function converts data-ng attributes to ng attributes
*/
function addNgAttributes() {
$('[data-ng]').each(function () {
var ngdata = $(this).data('ng');
var _this = this;
$.each(ngdata, function (key, value) {
$(_this).attr(key, value)
});
})
}
You should extend Zend\Form\View\Helper\FormFile and replace it via factory:
namespace YourNamespace;
use Zend\Form\View\Helper\FormFile;
class FormFile extends FormFile {
/**
* Attributes valid for the input tag type="file"
*
* #var array
*/
protected $validTagAttributes = [
'name' => true,
'accept' => true,
'autofocus' => true,
'disabled' => true,
'form' => true,
'multiple' => true,
'required' => true,
'type' => true,
'yourCustomAttribute' => true, //<--- here add Your attribute
];
}
and in module.php:
public function getViewHelperConfig() {
return array(
'factories' => array(
'Zend\Form\View\Helper\FormFile' => function($sm) {
$helper = new \YourNamespace\FormFile();
return $helper;
}
)
);
}
then You can use yourCustomAttribute in form class as other attributes.
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' => ''
));
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.