I create in Zend a nawe field (radio):
$sizeId = $this->createElement('radio', 'size_id');
But i need get in view code looks like this:
<li>
<input ...>
<label><span>name</span></label>
</li>
How to do it?
I did this code:
$sizeId = $this->createElement('radio', 'size_id', array(
'decorators' => array(
'ViewHelper',
array(array('AddTheLi' => 'HtmlTag'), array('tag' => 'li')),
array(array('AddTheUl' => 'HtmlTag'), array('tag' => 'ul', 'class' => 'size-picker clearfix')),
'Errors',
array('Description', array('tag' => 'p', 'class' => 'description')),
),
'disableLoadDefaultDecorators' => true,
'separator' => '</li><li>',
'class' => 'attribute-radio',
));
But no the code in view looks like this:
<li>
<label><input ...>name</label>
</li>
Why the input is in label?
Related
I want to insert a div before my submit button.
->add('address',new AddressForm(array(
))
->add('Valider', 'submit', array(
'attr' => array('class' => ' btn btn-lg btn-success btn-bloc')
))
->add('Cancel', 'reset', array(
'attr' => array('class' => ' btn btn-lg btn-cancel btn-bloc')
))
This the div proposed by Google:
<div class="g-recaptcha" data-sitekey="***************************"></div>
I'm using Twig I did manage to add it after the Form but not inside it.
Any suggestions will be appreciated.
I used the EWZ bundle to add googles recapture to my forms
https://github.com/excelwebzone/EWZRecaptchaBundle
With that bundle you can just add "->add('recaptcha', 'ewz_recaptcha')" to your form builder, for example:
$builder->add('name', 'text', array(
'attr' => array(
'placeholder' => 'Name',
'pattern' => '.{4,}' //minlength
)
))
->add('email', 'email', array(
'attr' => array(
'placeholder' => 'Email'
)
))
->add('company', 'text', array(
'attr' => array(
'placeholder' => 'Company/Organisation',
'pattern' => '.{3,}' //minlength
)
))
->add('message', 'textarea', array(
'attr' => array(
'cols' => 90,
'rows' => 10,
'placeholder' => 'Leave a message...'
)
))
->add('recaptcha', 'ewz_recaptcha')
->add('save', 'submit', array(
'label' => 'Submit',
'attr' => array (
'class' => 'submit btn-block btn-lg btn-primary'
)
));
Hi all I'm trying to render this html structure with Zend :
<div id="medias" class="item">
<h2> Medias </h2>
<a id="addmedia" href="#">
<img alt="" src="images/bigbtn-add.png">
</a>
</div>
Update 1 :
This is how I create the media_img element :
$media_img = new Zend_Form_Element_Image('media_img');
$media_img->setImage("/img/bigbtn-add.png");
$this->addElement($media_img);
Using this :
$this->setElementDecorators(array(
'ViewHelper',
'HtmlTag', array('HtmlTag', array('tag' => 'a', 'id' => 'addmedia','href' => '#'))
),
array('media_img')
);
I properly render this part of my code :
<a id="addmedia" href="#">
<img alt="" src="images/bigbtn-add.png">
</a>
How can I prepend the h2 and then wrap these elements inside my div ?
Update 2 :
Thanks to #Mubo's help, I render this html :
<h2 class="hint">Medias</h2>
<a id="addmedia" href="#">
<input id="media_img" type="image" src="/img/bigbtn-add.png" name="media_img">
</a>
Now I only need to wrap all of these lines in a div.
The decorator looks like this now :
$this->setElementDecorators(array(
'ViewHelper',
'HtmlTag', array('HtmlTag', array('tag' => 'a', 'id' => 'addmedia','href' => '#')),
array('Description', array('tag' => 'h2', 'placement' => 'prepend')),
),
array('media_img')
);
What's missing ?
Thanks for your help.
Update 3 : I still can't figure it out...
You can use setDescription and prepend it with decorator like this.
This is very tricky and not straightforward.
$myElement = new Zend_Form_Element_Text('name');
$myElement->setLabel('Label');
$myElement->setRequired(true);
$myElement->setDescription('Medias');
$myElement->setDecorators( array( 'DijitElement', 'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td')),
array('Description', array('tag' => 'h2', 'placement' => 'prepend')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr', 'class' => 'form_row'))
));
$this->addElement($myElement);
Wow, I finally found a solution. Here is my decorator :
$media_img->setDecorators(array(
'ViewHelper',
'Errors',
array('HtmlTag', array('tag' => 'a', 'id' => 'addmedia','href' => '#')),
array('Description', array('tag' => 'h2', 'placement' => 'prepend')),
array(
array('fieldDiv' => 'HtmlTag'),
array(
'tag' => 'div', 'class' => 'item', 'id' => 'medias'
)
)
));
I would like to render the following markup:
<div class="row">
<p>
<label>Your Name</label>
<input type="text" class="text_field" name="name">
<ul class="errors">
<li>Waarde is vereist en kan niet leeg worden gelaten</li>
</ul>
</p>
</div>
This is my Zend form element + decorator:
$this->addElement('text', 'name', array(
'label' => 'Naam:',
'class' => 'text_field',
'required' => true,
'decorators' => array(
'ViewHelper',
'Label',
'Errors',
array(array('row' => 'HtmlTag'), array('tag' => 'p')),
array(array('content' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row'))
)));
But this always renders the ul list below the p tag and never inside. It also adds an additional p tag below the list.
<div class="row">
<p>
<label class="required" for="name">Naam:</label>
<input type="text" class="text_field" value="" id="name" name="name">
</p>
<ul class="errors">
<li>Waarde is vereist en kan niet leeg worden gelaten</li>
</ul>
<p></p>
</div>
What am I doing wrong?
Found it! My stupid mistake. I did only check the final rendered output in my browser. I am using a template which also loads javascript and this changes the DOM which creates the unwanted result.
So the first decorator setup was working correct.
Try to do the following:
$this->addElement('text', 'name', array(
'label' => 'Naam:',
'class' => 'text_field',
'required' => true,
'decorators' => array(
'ViewHelper',
'Label',
'Errors',
array(array('content' => 'HtmlTag'), array('tag' => 'p')),
array(array('content' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row'))
)));
<div class="field50Pct">
<div class="fieldItemLabel">
<label for='First Name'>First Name:</label>
</div>
<div class="fieldItemValue">
<input type="text" id="firstname" name="firstname" value="" />
</div>
</div>
<div class="clear"></div>
I want the code to appear like this in source code . how do i write the same thing in zend using decorators ?
The element is like
$firstname = new Zend_Form_Element_Text('FirstName');
$firstname->setLabel('FirstName')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addErrorMessage('Error in First Name')
->addValidator('NotEmpty');
This seems to work for me:
(with <div class="clear"></div> after the input)
$firstname->setDecorators(array(
'ViewHelper',
'Description',
'Errors',
array('HtmlTag', array('tag' => 'div', 'class' => 'fieldItemValue')),
array(array('labelDivOpen' => 'HtmlTag'),
array('tag' => 'div',
'placement' => 'prepend',
'closeOnly' => true)),
'Label',
array(array('labelDivClose' => 'HtmlTag'),
array('tag' => 'div',
'class' => 'fieldItemLabel',
'placement' => 'prepend',
'openOnly' => true)),
array(array('fieldDiv' => 'HtmlTag'),
array('tag' => 'div', 'class' => 'field50Pct')),
array(array('divClear' => 'HtmlTag') ,
array('tag' => 'div' ,
'class' => 'clear',
'placement' => 'append'))
));
I would like to get rid of the definition list format of my Zend_Form. This is the layout that I'm going for:
<form>
<p>
<label for="email" class="required">Your email address:</label>
<input type="text" name="email" id="email" value="">
</p>
<p>
<input type="submit" name="submit" id="submit" value="Subscribe">
</p>
<input type="hidden" name="active" value="true" id="active">
<input type="hidden" name="signupDate" value="" id="signupDate">
</form>
What do I need to do to my form in order to get this layout?
class Default_Form_Subscribe extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$this->addElement('text', 'email', array(
'label' => 'Email address:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array('EmailAddress')
));
$this->addElement('submit', 'submit', array(
'label' => 'Subscribe',
'ignore' => true
));
$this->addElement('hidden', 'active', array(
'value'=>'true'
));
$this->addElement('hidden', 'signupDate', array(
'value' => Zend_Date::now()->toString('YYYY-MM-dd')
));
}
}
Ah, beat me to it... I went with the approach of creating a custom definition that can be applied to specific elements. Also had to reset the decorators on the form itself to remove the default 'dl' wrapper, seems to do exactly what you need:
class Default_Form_Subscribe extends Zend_Form
{
public function init()
{
$this->setMethod('post');
// reset form decorators to remove the 'dl' wrapper
$this->setDecorators(array('FormElements','Form'));
// custom decorator definition for form elements
$customElementDecorators = array(
'ViewHelper',
'Errors',
array(
'Description',
array('tag' => 'p','class' => 'description')
),
array(
'Label',
array('separator' => ' ')
),
array(
array('data' => 'HtmlTag'),
array('tag' => 'p')
)
);
$this->addElement('text', 'email', array(
'label' => 'Email address:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array('EmailAddress'),
'decorators' => $customElementDecorators
));
$this->addElement('submit', 'submit', array(
'label' => 'Subscribe',
'ignore' => true,
'decorators' => $customElementDecorators
));
$this->addElement('hidden', 'active', array(
'value'=>'true',
'decorators' => array('ViewHelper')
));
$this->addElement('hidden', 'signupDate', array(
'value' => Zend_Date::now()->toString('YYYY-MM-dd'),
'decorators' => array('ViewHelper')
));
}
}
let me add probably a bit shorter way which worked for me just fine:
//after adding all the form elements
//all form elements in a loop
foreach ($this->getElements() as $el) {
$el->setDecorators(
array('ViewHelper', 'Errors', array('HtmlTag', array('tag' => 'p')
);
}
//form itself
$this->setDecorators( array('FormElements', 'Form') );
it seams to me in your case you also should filter out elements by type in search for those which does not need outer html at all
You have to customize your Zend_Form elements's decorators. Check this tutorial.
In your case it will be something similar to this:
$form->setElementDecorators(array(
'ViewHelper',
'Errors',
array('Label', array('tag' => 'label', 'placement' => 'prepend'),
array(array('data' => 'HtmlTag'), array('tag' => 'p')),
));
That sets the decorators for all the form elements. You can as well configure individual elements (like your hidden-s & buttons).
It's possible also to form display groups, and decorate them individually.