Zend Framework: Captcha problem - zend-framework

Iam using following code to generate CAPTCHA :
$captcha = $this->createElement('captcha', 'captcha',
array('required' => true,
'captcha' => array('captcha' => 'Image',
'font' => 'resource/fonts/arial.ttf',
'fontSize' => '24',
'wordLen' => '5',
'height' => '50',
'width' => '150',
'imgDir' => 'resource/captcha',
'imgUrl' => 'resource/captcha',
'gcFreq'=>'10',
'dotNoiseLevel' => '10',
'lineNoiseLevel' => '2')));
$captcha->setLabel('Captcha');
Following code is generated:
<label for="captcha-input" class="login_label required">Captcha</label>
<img width="150" height="50" alt="" src="captcha/eb3a592c8b1c7a71b0c7ce5179422be2.png" />
<input type="hidden" name="captcha[id]" value="eb3a592c8b1c7a71b0c7ce5179422be2" id="captcha-id">
<input type="text" name="captcha[input]" id="captcha-input" value="">
<input type="text" name="captcha" id="captcha" value="eb3a592c8b1c7a71b0c7ce5179422be2">
Can someone guide me how can I remove extra input text fields like
<input type="text" name="captcha" id="captcha" value="eb3a592c8b1c7a71b0c7ce5179422be2">
Thanks in advance

It's important to do the
$this->getElement('captcha')->removeDecorator("viewhelper");
after you have enabled the ElementsDecorators (which sets the ViewHelper in the first place - don't delete this, it's required anyway)
For me it looks like this:
$this->setElementDecorators(array(
'ViewHelper',
'Errors',
array(array('data' => 'HtmlTag'), array('tag' => 'td')),
array('Label', array('tag' => 'td')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
));
$this->getElement('captcha')->removeDecorator("viewhelper");

That input is not "extra" - it's essential.
When the CAPTCHA is validated, the value of that field is used to look up the correct solution to the CAPTCHA, which is then compared against the user's input.
Without that field, your CAPTCHA will break.
Why would you want to remove it in the first place?

You can try this:
$this->getElement('captcha')->removeDecorator("viewhelper");

I had the same problem. Remove the "ViewHelper" decorator and the captcha will render properly.

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?

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

How can I use zend form decorator to render errors inside my paragraph tag wrapping label and input

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

using zend form decorators

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

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.