Problems creating a captcha in Zend Framework 1.11.11 - zend-framework

How can I create a captcha field when I have created the form using conventional html rather than the addElement() methods of ZF? That might sound stupid, but i'm a novice and after 6 hours of straight googling, still no answer.

I was able to add a captcha directly to the view using this code:
<?php
$form = new Zend_Form();
$captcha = new Zend_Form_Element_Captcha('Captcha', array(
'captcha' => array(
'captcha' => 'Image',
'wordLen' => 6,
'timeout' => 300,
'width' => 300,
'height' => 100,
'imgUrl' => '/captcha',
'imgDir' => APPLICATION_PATH . '/../public/captcha',
'font' => APPLICATION_PATH . '/../public/fonts/LiberationSansRegular.ttf')));
$form->addElement($captcha);
echo $form;//use this line in view script
$this->view->form = $form //use this line in controller
?>
you could also put the same code into your controller, assign it to the view and then call it in the view like:
<form>
//form stuff html
<?php echo $this->form->captcha ?>
</form>

Related

how to add form id in cakephp when creating a form

i am new in cakephp so i dont know how can i do this ..
i want to add custom form id in my form but it is not adding the id ..it is using the default one adding the 'UserIndexForm' id..
how can i add this id
i want to do like this
<form method="post" action="#" id="form-login">
here cakephp code
<?php
echo $this->Form->create('User', array(
'inputDefaults' => array(
'label' => false,
'div' => false,
'id' =>'form-login'//not working
)
));
?>
please help me if anyone know this
thankyou in advance
The inputDefaults option is only changing the input fields so you need to set the id on the root level of the array:
<?php
echo $this->Form->create('User', array(
'id' => 'form-login',
'inputDefaults' => array(
'label' => false,
'div' => false
)
));
?>

Zend From: How to display only one element of a form?

I have a zend form:
// Email
$email = $this->createElement('text', 'email');
$email->setLabel('Адрес электронной почты:')
->addValidator('EmailAddress')
->addValidator('StringLength', FALSE, array(6, 30))
->setRequired(TRUE);
// Пароль
$password = $this->createElement('password', 'password');
$password->setLabel('Пароль:')
->addValidator('StringLength', FALSE, array(6, 30))
->setRequired(TRUE);
// Шрифт капчи
$fonts = scandir(APPLICATION_PATH . '/other/fonts/');
$font = APPLICATION_PATH . '/other/fonts/' . $fonts[rand(2, sizeof($fonts) - 1)];
// Инициализация капчи
$captcha = new Zend_Form_Element_Captcha(
md5($_SERVER["REMOTE_ADDR"] . date('d.m.Y')),
array('label' => 'Введите код с картинки:',
'required' => TRUE,
'captcha' => array(
'captcha' => 'Image',
'FontSize' => rand(25, 30),
'wordLen' => rand(4, 6),
'timeout' => 300,
'font' => $font,
'imgDir' => 'img/captcha/',
'imgUrl' => '/img/captcha/',
)));
$submit = $this->createElement('submit', 'submit_button', array(
'label' => 'Enter',
));
And I have a my own html code for this form, it contained in view script, but captcha element is dynamic. Is there any way to display only captcha element html code? Like this:
<!-- my html code -->
<?php $this->myForm->captcha; ?>
<!-- my html code -->
Thanks.
yes you can use like below
You need to pass the form to your view script to make it work.
public function someAction()
{
$this->view->form = new Form_Name(); // form class name
}
in your view script you can receive the elements via
<table>
<tr>
<td><?= $this->form->getElement('captcha'); ?></td>
</tr>
</table>
let me know if i can help you more.

CakePHP Text as Form Submit

I've searched the web and have come up with nothing. (Multiple search engines too - I have looked!)
I'm trying to have a text link as the 'form submit' button. Any ideas if this is possible in CakePHP?
Current view code below!
<?php
echo $this->Form->create('trainees', array(
'action' => 'reassign'
));
echo $this->Form->input('emailaddress', array(
'value' => 'scott#something',
'type' => 'hidden',
));
echo $this->Form->submit('Re-Assign Mentor', array(
'class' => 'submit mid',
'before' => '<p>',
'after' => '</p>'
));
echo $this->Form->end();
?>
You need to use the HtmlHelper to output a link. In it's simplest form you use the text you want displayed with the URL that it should link to. In this case it will be JavaScript:
$this->Html->link('Submit Form', 'javascript:document.forms["myform"].submit();');
There are two additional parameters (a $options array and $confirmMessage boolean), but they along with the URL are optional.
You can also call your own JavaScript function if you need to do client side verification and call the submit function from there (also verify on the server as clients can lie).
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::link

Redirection from module to application

how can we go back form one module to parent application in ZEND. I use this code:-
<?php echo $this->url(array('controller' => '', 'action' => '')); ?>
If you add a 'module' key to the URL array, you can easily traverse modules. If you're trying to get to the "Parent" (the default module, perhaps?), you'd change your code to:
<?php echo $this->url(array('module' => 'default', 'controller' => 'index', 'action' => 'index)); ?>

Building a modular Website with Zend Framework: Am I on the right way?

I´m a little bit confused by reading all the posts and tutorials about starting with Zend, because there a so many different ways to solve a problem.
I just need some feedback about my code to know if I am on the right track.
To simply get a (hard coded) Navigation for my site (depending on who is logged in) I build a Controller Plugin with a postDispatch method:
public function postDispatch(Zend_Controller_Request_Abstract $request)
{
$menu = new Menu();
//Render menu in menu.phtml
$view = new Zend_View();
//NEW view -> add View Helper
$prefix = 'My_View_Helper';
$dir = dirname(__FILE__).'/../../View/Helper/';
$view->addHelperPath($dir,$prefix);
$view->setScriptPath('../application/default/views/scripts/menu');
$view->menu = $menu->getMenu();
$this->getResponse()->insert('menu', $view->render('menu.phtml'));
}
Is it right that I need to set the helper path again?
I did this in a Plugin Controller named ViewSetup. There I do some setup for the view like doctype, headlinks, and helper paths (This step is from the book: Zend Framework in Action).
The Menu class which is initiated looks like this:
class Menu
{
protected $_menu = array();
/**
* Menu for notloggedin and logged in
*/
public function getMenu()
{
$auth = Zend_Auth::getInstance();
$view = new Zend_View();
//check if user is logged in
if(!$auth->hasIdentity()) {
$this->_menu = array(
'page1' => array(
'label' => 'page1',
'title' => 'page1',
'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page1'))
),
'page2' => array(
'label' => 'page2',
'title' => 'page2',
'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page2'))
),
'page3' => array(
'label' => 'page3',
'title' => 'page3',
'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page3'))
),
'page4' => array(
'label' => 'page4',
'title' => 'page4',
'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page4'))
),
'page5' => array(
'label' => 'page5',
'title' => 'page5',
'url' => $view->url(array('module' => 'pages','controller' => 'my', 'action' => 'page5'))
)
);
} else {
//user is vom type 'client'
//..
}
return $this->_menu;
}
}
Here´s my view script:
<ul id="mainmenu">
<?php echo $this->partialLoop('menuItem.phtml',$this->menu) ?>
</ul>
This is working so far. My question is: is it usual to do it this way; is there anything to improve?
I´m new to Zend and I've seen deprecated tutorials on the web which often are not obvious. Even the book is already deprecated where the autoloader is mentioned.
You shouldn't be creating a new view. Since you have already created the View object in your Boostrap (and used it to render the rest of the site) you should fetch the already created view object.
If you are using Zend_Application_Resource to setup your view in the bootstrap you can fetch it like this:
$view = Zend_Controller_Front::getInstance()
->getParam('bootstrap')
->getResource('view');
This way there is no need to set the view helper path again and create another view object.
If you are not using the Zend_Application to boostrap your app you could try something like this:
$view = Zend_Layout::getMvcInstance()->getView();
Unless you are working on a relatively small side, I wouldn't do this in the controller,
since you will have to add this to many controllers.
Why not check in the bootstrap or even checking in your layout would make more sense to me although it wouldn't be proper.