Insert Google Recaptcha inside Symfony2 Form - forms

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'
)
));

Related

Zend1 - form radio decorators

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?

How to create icon inside form postlink with cakephp and twitter bootstrap

This gives me what I want:
<?php echo $this->Html->link(
$this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-edit')) . " Edit",
array('action' => 'edit', $comment['Comment']['comment_id']),
array('class' => 'btn btn-mini', 'escape' => false)
); ?>
But when I create a Form postLink I don't know how to get the remove icon in front of it..
<?php echo $this->Form->postLink(
$this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-remove')) . " Delete",
array('action' => 'delete', $comment['Comment']['comment_id']), null, __('Are you sure you want to delete # %s?', $comment['Comment']['comment_id']),
array('class' => 'btn btn-mini', 'escape' => false)
); ?>
It gives me <i class="glyphicon glyphicon-remove"></i> Delete
You forgot add option escape to false
echo $this->Form->postLink(
$this->Html->tag('i', '', array('class' => 'glyphicon glyphicon-remove')). " Delete",
array('action' => 'delete', $comment['Comment']['comment_id']),
array('escape'=>false),
__('Are you sure you want to delete # %s?', $comment['Comment']['comment_id']),
array('class' => 'btn btn-mini')
);
using a button
<?php echo $this->Form->postLink(
'<button class="btn btn-danger">
<i class="icon-trash icon-white"></i>
</button>',
array(
'action' => 'delete', $post['Post']['id']
),
array(
'class' => 'tip',
'escape' => false,
'confirm' => 'Are you sure ?'
));
?>
Try this:
<?php
echo $this->Form->postLink(
'Delete',
array('controller'=>'Comments',
'class'=>'glyphicon glyphicon-remove','action' => 'delete',$comment['id']),
array('confirm' => 'Are you sure?')
);
?>
The below code will create Link Button for deleting Items with a Confirmation box.
$this->Form->postLink( 'Delete Item',
['action' => 'delete', 'paramId' => $item->id ],
['confirm' => __('Are you sure you want to delete this Item?'), 'class'=> 'btn btn-outline-danger']
)

Zend Form Decorator on 1.12 version

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'
)
)
));

Zend Framework 2 formInput or formElement ID tag not rendering

In Zend framework 2, when I use the view's formRow method like so
$this->formRow($form->get('product_name'));
it will generate HTML like this
<label for="product_name">
<span>Name</span>
<input type="text" id="product_name" name="product_name">
</label>
but if I use formInput
<div class="control-group">
<?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
<div class="controls">
<?php echo $this->formInput($form->get('product_name')); ?>
</div>
</div>
$this->formInput($form->get('product_name'));
i don't get the id tag
<input type="" name="product_name">
I've tried with formElement with same results.
How can I get it to render just the input with all attributes and values?
This is how my Zend Framework 2 View looks like (simplified)
<?php echo $this->form()->openTag($form); ?>
<div class="control-group">
<?php echo $this->formLabel($form->get('product_name')->setLabelAttributes(array('class'=>'control-label'))); ?>
<div class="controls"><?php echo $this->formInput($form->get('product_name')); ?></div>
</div>
<div class="control-group">
<div class="controls"><?php echo $this->formSubmit($form->get('submit')); ?></div>
</div>
<?php echo $this->form()->closeTag(); ?>
and the Zend Framework 2 Form
<?php
namespace Product\Form;
use Zend\Form\Form;
class ProductForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('product');
$this->setAttribute('method', 'post');
$this->setAttribute('class','form-horizontal');
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
'id' => 'submitbutton',
'class'=>'btn btn-success btn-large'
),
));
}
}
?>
Change:
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Name',
),
));
to:
$this->add(array(
'name' => 'product_name',
'attributes' => array(
'type' => 'text',
'id' => 'product_name',
),
'options' => array(
'label' => 'Name',
),
));
Actually this code:
$this->add(array(
'type' => 'Zend\Form\Element\Text',
'name' => 'product_name',
'attributes' => array(
'id' => 'product_name',
'class' => 'span3',
),
'options' => array(
'label' => 'Your label',
),
));
could be used correctly by a css renderer like Bootstrap due to fact that the $this->formRow(...) form helper will produce:
<label for="product_name">Your label</label><input type="text" name="product_name" class="span3" id="product_name" value="">
which is more readable than the original formRow output (without id neither class attributes)

How to get rid of the Zend_Form dl, dt, dd tags?

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.