I'm using Zend Framework 1 and this library to integrate Twitter-bootstrap. It's great library, but I want to add new decorators for it (for Zend Form Element File), so that it looked like here
So instead of
<input id="image" type="file" name="image">
I want to render something like that (with all styles and javascript)
<span class="btn btn-success fileinput-button">
<i class="icon-plus icon-white"></i>
<span>Add files...</span>
<input type="file" multiple="" name="files[]">
</span>
I'm new to Zend Framework and do not know how to do that.
The question is, what have I put in library/Twitter/Form.php, in public/css and public/javascript to make it work?
Or maybe I have to do something else?
I tried to add decorators in Form.php, but no decorators applied (I do not remember what I did exactly)
So the main question is, how can I add this decorators for my Element in library/Twitter?
Edited:
I was working on the problem and managed to show a green button with a "plus" picture using jQuery-file-upload-ui.css (do not remember a name of the file) and changing $decorators in Form.php where code
if($element instanceof Zend_Form_Element_File)
is situated to
$decorators = array(
"ViewHelper",
array("Errors", array("placement" => "append")),
array("Description", array("tag" => "span", "class" => "help-block")),
array(array("ivanov2" => "HtmlTag"), array("tag" => "i", "class" => "icon-plus icon-white",'openOnly' => true,)),
array(array('close' => 'HtmlTag'), array('tag' => 'i', 'closeOnly' => true)),
array(array("ivanov" => "HtmlTag"), array("tag" => "span", "class" => "btn btn-success fileinput-button")),
array(array("innerwrapper" => "HtmlTag"), array("tag" => "div", "class" => "controls")),
array("Label", array("class" => "control-label")),
array(array("outerwrapper" => "HtmlTag"), array("tag" => "div", "class" => "control-group"))
);
But I still do not know how I can place an element with some content (for example with text):
<span>Add Files..</span>
And also I want to use drag and drop, but do not know how.
Related
How can i add a new personalized attribute with zend From, exemple <input type='text' **ref='name**' name='id_commande' id='commande'/>
Use setAttrib on element for single, or setAttribs for multiple attributes you want to add
This should make a job:
$id_commande = new Zend_Form_Element_Text('id_commande');
$id_commande->setAttribs([
'ref' => 'name',
'class' => 'your_class',
'style' => 'width: 150px'
]);
I'm trying to figure out why ZF3 encodes my element's class string, but can't find anything about that behaviour on the internet.
$this->add([
'type' => 'Button',
'name' => 'submitLogin',
'options' => [
'label' => '<i class="zmdi zmdi-check"></i>',
'label_options' => [
'disable_html_escape' => true,
]
],
'attributes' => [
'type' => 'submit',
'class' => 'btn btn--icon login__block__btn',
],
]);
becomes
<button type="submit" name="submitLogin" class="btn btn--icon login__block__btn" value=""><i class="zmdi zmdi-check"></i></button>
I think this is an abstract concept. Generally we take some steps when we work with data. We filter input values and escape outputs. This is a security philosophy.
Zend Framework did the same thing while something is about security. This means this behavior is by default. ZF escapes attributes' values when it is being displayed onto the browser. ZF only allows non-escaping through explicit options like you did for the label's content above.
You will get some concept via this issue on github where Matthew said:
Secure by default is the mantra
Is there an individual class option for radio buttons in Cake 2.x?
In my opinion the logical approach would be like:
<div class="input radio required">
<?php
$options = array(
array('1' => 'Kuchen', 'class' => 'cake'),
array('2' => 'Kekse', 'class' => 'biscuits'),
array('3' => 'Eis', 'class' => 'iceCream'),
);
$attributes = array(
'legend' => false,
'default' => '1'
);
echo $this->Form->radio('INCOMETYPE', $options, $attributes);
?>
</div>
But that doesn´t work. I hope you can help. Thanks :)
You could extend FormHelper to add implement this functionality if you intend to use it elsewhere. If it's a one off, you might be better to produce the markup by hand. Use the code produced by $this->Form->radio to give you a start.
i need help with my dynamically extensible zend form.
I have form with subform, which contains two elements:
<form>
<fieldset class="itemGroup">
<label>
Question
<input type="text" name="items[questions][]" value="">
</label>
<label>
Answer
<input type="text" name="items[answers][]" value="">
</label>
</fieldset>
</form>
I obtained it with following procedure:
$itemsSubform = new Zend_Form_SubForm();
$form->addSubForm($itemsSubform, 'items');
$itemsSubform->setElementsBelongTo('items');
$itemQuestion = new Zend_Form_Element_Text('questions', array(
'label' => 'Question',
'isArray' => true,
'filters' => array(
'stringTrim',
),
'validators' => array(
array('stringLength', array('max' => 255)),
),
));
$itemAnswer = new Zend_Form_Element_Text('answers', array(
'label' => 'Answer',
'isArray' => true,
'filters' => array(
'stringTrim',
),
'validators' => array(
array('stringLength', array('max' => 255)),
),
));
$itemsSubform->addDisplayGroup(array($itemQuestion, $itemAnswer), 'itemGroup');
If is needed, i just copy all fieldset for extend form by javascript.
All is working correct, until i submit form. During validation is no element validated, and during rendering form, populated by such data, i get error message from class Zend_View_Abstract that escaping value is array instead of string (this method is called during rendering element for escape its value).
For compltion, if i call $form->getValues(); after validation, (by javascript another fieldset is added) i get this:
Array
(
[items] => Array
(
[questions] => Array
(
[0] => lorem
[1] => dolor
)
[answers] => Array
(
[0] => ipsum
[1] => sit
)
)
)
Could someone advise me how to behave to form? The ideal solution would be when form themselves validate each value separately and find how many times should he render fieldset (displayGroup).
First your elements fail to validate because you set isArray => TRUE and the validator you chose evaluates strings.
Next you issue with populating the form I believe is likely due to the fact that you need to supply the data to populate in the same multidimensional array as the form produces (the arrays should map one to one).
The link below is to an example of how to build a form dynamically, you should be able to use it as a template to produce one to fill your needs.
Example of generating form elements with loop
I would like to be able to add a hidden form field using array notation to my form. I can do this with HTML like this:
<input type="hidden" name="contacts[]" value="123" />
<input type="hidden" name="contacts[]" value="456" />
When the form gets submitted, the $_POST array will contain the hidden element values grouped as an array:
array(
'contacts' => array(
0 => '123'
1 => '456'
)
)
I can add a hidden element to my form, and specify array notation like this:
$form->addElement('hidden', 'contacts', array('isArray' => true));
Now if I populate that element with an array, I expect that it should store the values as an array, and render the elements as the HTML shown above:
$form->populate($_POST);
However, this does not work. There may be a bug in the version of Zend Framework that I am using. Am I doing this right? What should I do differently? How can I achieve the outcome above? I am willing to create a custom form element if I have to. Just let me know what I need to do.
You have to use subforms to get the result you seek. The documentation was quite a ride but you can find it here
Using what I found there I constructed the following formL
<?php
class Form_Test extends Zend_Form {
public function init() {
$this->setMethod('post');
$this->setIsArray(true);
$this->setSubFormDecorators(array(
'FormElements',
'Fieldset'
));
$subForm = new Zend_Form(array('disableLoadDefaultDecorators' => true));
$subForm->setDecorators(array(
'FormElements',
));
$subForm->addElement('hidden', 'contacts', array(
'isArray' => true,
'value' => '237',
'decorators' => Array(
'ViewHelper',
),
));
$subForm2 = new Zend_Form(array('disableLoadDefaultDecorators' => true));
$subForm2->setDecorators(array(
'FormElements',
));
$subForm2->addElement('hidden', 'contacts', array(
'isArray' => true,
'value' => '456', 'decorators' => Array(
'ViewHelper',
),
));
$this->addSubForm($subForm, 'subform');
$this->addSubForm($subForm2, 'subform2');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setValue('Submit');
$this->addElement('submit', 'submit');
}
}
Wich outputs this html:
<form enctype="application/x-www-form-urlencoded" method="post" action=""><dl class="zend_form">
<input type="hidden" name="contacts[]" value="237" id="contacts">
<input type="hidden" name="contacts[]" value="456" id="contacts">
<dt id="submit-label"> </dt><dd id="submit-element">
<input type="submit" name="submit" id="submit" value="submit"></dd></dl></form>
And when submited the post looks like:
array(2) {
["contacts"] => array(2) {
[0] => string(3) "237"
[1] => string(3) "456"
}
["submit"] => string(6) "submit"
}
So thats how you can create the kind of forms you seek. Hope this helps! if you have a question post a comment!
Its quite hackish if you ask me. You basically create subforms but disable there form decorators so just the element gets output. Since the identical contacts[] elements are in different form object zend does'nt overwrite them and it works. But yeah..
Edit: changed it a bit to remove labels and garbage arount the hidden inputs.
To use array notation, you need to specify that the element "belongs to" a parent array:
$form->addElement('hidden', 'contact123', array('belongsTo' => 'contacts', 'value' => '123'));
$form->addElement('hidden', 'contact456', array('belongsTo' => 'contacts', 'value' => '456'));
This indeed seems to be a bug in Zend Framework - the value attribute for an element is properly set to array, but it's ignored when the element renders - it just uses$this->view->escape($value) to output element's html.
I've solved this by implementing a custom helper for such elements:
class My_View_Helper_HiddenArray extends Zend_View_Helper_FormHidden
{
public function hiddenArray($name, $value = null, array $attribs = null)
{
if (is_array($value)) {
$elementXHTML = '';
// do not give element an id due to the possibility of multiple values
if (isset($attribs) && is_array($attribs) && array_key_exists('id', $attribs)) {
unset($attribs['id']);
}
foreach ($value as $item) {
$elementXHTML .= $this->_hidden($name, $item, $attribs);
}
return $elementXHTML;
} else {
return $this->formHidden($name, $value, $attribs);
}
}
}
Which, when used the next way:
$contacts = $form->createElement('hidden', 'contacts')
->setIsArray(true)
->setDecorators(array(
array('ViewHelper', array('helper' => 'HiddenArray')),
));
$form->addElement($contacts);
generates the needed output.
The reason to extend Zend_View_Helper_FormHidden here is just to be able to call the default behaviour if no array value is set ( return parent::formHidden($name, $value, $attribs) ).
Hope this helps someone :)
For the newer versions of ZF you should use https://framework.zend.com/manual/2.1/en/modules/zend.form.elements.html#multicheckbox