Register form doesn't work - forms

I got RegisterController.php and inside of it I got:
class RegisterController extends AppController {
public $name = 'Register';
public $components = array('Session');
public function index() {
if ($this->request->is('account')) {
// if ($this->Post->save($this->request->data)) {
echo "got it";
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
// }
}
}
}
My /View/Register/index.tcp file:
<?php
echo $this->Form->create('Account');
echo $this->Form->input('username');
echo $this->Form->input('password');
$options = array(
'label' => 'Register',
'class' => 'submit'
);
echo $this->Form->end($options);
?>
And my /Model/Account.php file:
<?php
class Account extends AppModel {
public $name = 'Account';
public $validate = array(
'username' => array(
'rule' => 'notEmpty'
),
'password' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Can not be empty.'
),
'minLength' => array(
'rule' => array('minLength', '8'),
'message' => 'Min. 8 chars.'
)
)
);
}
?>
The problem is that, I'm clicking on the submit button, and nothing happends. It should at least check for validation.
Where is the error?

Should work if you add the action in options:
echo $this->Form->create('Account', array('action' => 'your/url'));

Related

Translate zend framework 1 form labels and error message

I have a web site which is in English and Arabic. I converted the text into Arabic but the form labels and error message is not converting. I am using gettext adapter and how do i convert this labels. I am using Zend_Form and creating object of this form and passign this to view.
Bootstrap file
protected function _initTranslate() {
$translate = new Zend_Translate('gettext', APPLICATION_PATH . "/lang/", null, array('scan' => Zend_Translate::LOCALE_DIRECTORY));
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Translate', $translate);
//Zend_Form::setDefaultTranslator($translate);
$translate->setLocale('ar');
}
public function _initRoutes() {
$this->bootstrap('FrontController');
$this->_frontController = $this->getResource('FrontController');
$router = $this->_frontController->getRouter();
$langRoute = new Zend_Controller_Router_Route(
':lang', array(
'lang' => 'ar',
'module' => 'default',
'controller' => 'index',
'action' => 'index'
), array(
'lang' => 'en|ar'
)
);
$defaultRoute = new Zend_Controller_Router_Route(
':controller/:action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
)
);
$defaultRoute = $langRoute->chain($defaultRoute);
$router->addRoute('langRoute', $langRoute);
$router->addRoute('defaultRoute', $defaultRoute);
}
protected function _initLanguage() {
$front = Zend_Controller_Front::getInstance();
$front->registerPlugin(new My_Controller_Plugin_Language());
}
Form
class Application_Form_Contactus extends Zend_Form {
public function init() {
// Set the method for the display form to POST
$this->setMethod('post');
$this->addElement('text', 'name', array('label' => 'Name', 'class' => 'inputbox',
'filters' => array('StringTrim'),
'required' => true));
// Add an email element
$this->addElement('text', 'email', array('label' => 'Email', 'class' => 'inputbox',
'required' => true, 'filters' => array('StringTrim'), 'validators' => array('EmailAddress')));
$this->addElement('submit', 'submit', array(
'required' => false,
'label' => 'Send',
'value' => 'save',
'class' => 'submit-but',
'attribs' => array('type' => 'submit'),
));
}
}
Controller
$form = new Application_Form_Contactus();
$form->setAction($this->view->getSiteUrl() . 'Contactus');
$translate = Zend_Registry::get('Zend_Translate');
$form->element->setTranslator($translate);
$this->view->form = $form;
view
echo $this->form;
Try to attach to the Zend_Form object as a global translator. This will also translate validation error messages :
Zend_Form::setDefaultTranslator($translate);
Or you can do this :
Zend_Validate_Abstract::setDefaultTranslator($translator);
Please see zend form i18n
Try to set default translator.
Zend_Form::setDefaultTranslator($translate);
One more alternate way could be:
$form = new Application_Form_Contactus();
$translate = Zend_Registry::get('Zend_Translate');
foreach ($form->getElements() as $key => $element) {
$element->setTranslator($translate);
}

How to create multiple form submit buttons with alternate routes in ZF2

In ZF2, how do you create multiple submit buttons that each lead to different routes? In the Forms and actions chaper of the ZF2 tutorial, a form is created with a single submit button with the label “Go” that processes the input data and returns to the index page (route). Where do we put the pertinent scripts if we wanted four buttons:
Save action: saves user input, route: return to current page
Save and Close action: saves user input, route: return to index (Album)
Clear action: no action, route: return to current page
Close action: no action, route: return to index (Album)
I assume the buttons are created like this:
namespace Album\Form;
class AlbumForm extends Form
{
public function __construct($name = null)
{
// ... //
$this->add(array(
'name' => 'savebutton',
'attributes' => array(
'type' => 'submit',
'value' => 'Save',
'id' => 'savebutton',
),
));
$this->add(array(
'name' => 'save_closebutton',
'attributes' => array(
'type' => 'submit',
'value' => 'Save & Close',
'id' => 'save_closebutton',
),
));
$this->add(array(
'name' => 'clearbutton',
'attributes' => array(
'type' => 'submit',
'value' => 'Clear',
'id' => 'clearbutton',
),
));
$this->add(array(
'name' => 'closebutton',
'attributes' => array(
'type' => 'submit',
'value' => 'Close',
'id' => 'closebutton',
),
));
}
}
This is what the edit action looks like with only one submit button:
// module/Album/src/Album/Controller/AlbumController.php:
//...
// Add content to this method:
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('album', array(
'action' => 'add'
));
}
$album = $this->getAlbumTable()->getAlbum($id);
$form = new AlbumForm();
$form->bind($album);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($album->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getAlbumTable()->saveAlbum($form->getData());
// Redirect to list of albums
return $this->redirect()->toRoute('album');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
//...
Since pairs of buttons have the same form action and pairs of buttons have the same route, I image we want to add two if statements somewhere here, unless a switch statement is better.
Quick 'n dirty way to do what you need:
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('album', array(
'action' => 'add'
));
}
$album = $this->getAlbumTable()->getAlbum($id);
$form = new AlbumForm();
$form->bind($album);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($album->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$input = $form->getData();
if (!empty($input['save_closebutton'])) {
return $this->redirect()->toRoute('album', array(
'controller' => 'AlbumController',
'action' => 'index',
));
}
}
}
return array(
'id' => $id,
'form' => $form,
);
}

How to add a navbarForm to Yiistrap navbar

I'd like to add a login form to my navbar using Yiistrap.
However the documentation is lacking.
Does anyone know how to implement this?
You can extend TbHtml class with something like this:
class HHtml extends TbHtml {
/**
* #param $action
* #param string $method
* #param array $htmlOptions
* #return string
*/
public static function loginForm($action, $method = 'post', $htmlOptions = array()) {
if (isset($htmlOptions['visible']) && !$htmlOptions['visible'])
self::addCssClass('hide', $htmlOptions);
self::addCssClass('navbar-form', $htmlOptions);
$output = self::beginFormTb(self::FORM_LAYOUT_INLINE, $action, $method, $htmlOptions);
$output .= self::textField('UserLogin[username]', '', array('placeholder' => 'Username', 'size' => TbHtml::INPUT_SIZE_SMALL));
$output .= self::passwordField('UserLogin[password]', '', array('placeholder' => 'Password', 'size' => TbHtml::INPUT_SIZE_SMALL));
//$output .= self::checkBox('UserLogin[rememberMe]', false, array('label' => 'Remember me'));
$output .= self::submitButton('Sign in');
$output .= parent::endForm();
return $output;
}
}
So you can use it items section of TbNavbar that way:
array(
'class' => 'bootstrap.widgets.TbNav',
'htmlOptions' => array(
'class' => 'pull-right'
),
'items' =>array(
HHtml::loginForm('/site/login', 'post', array('visible' => Yii::app()->user->isGuest)),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest),
),
),
<?php $this->widget('bootstrap.widgets.BsNavbar', array(
'collapse' => TRUE,
'color' => BsHtml::NAVBAR_COLOR,
'brandLabel' => BsHtml::icon(BsHtml::GLYPHICON_HOME),
'brandUrl' => Yii::app()->homeUrl,
'items' => array(
array(
'class' => 'bootstrap.widgets.BsNav',
'type' => 'navbar',
'activateParents' => true,
'items' => array(
//other items
),
),
BsHtml::navbarSearchForm(Yii::app()->createUrl('search'), 'get', array(
'class' => 'navbar-form navbar-right',
)),
))); ?>

Zend Framework: My form wont render

So I have created a form below with Zend Framework which I'm then going to customise. My first issue is that with the csrf hash security I get an application error. However, when I remove them lines all I get is a blanks screen which is only resolved when I remove the CPATCHA protection. Can anyone explain to me why?
My Form:
class Application_Form_Clips extends Zend_Form
{
public function init()
{
// Set the method for the display form to POST
$this->setMethod('post');
// Add an email element
$this->addElement('text', 'email', array(
'label' => 'Your email address:',
'required' => true,
'filters' => array('StringTrim'),
'validators' => array(
'EmailAddress',
)
));
// Add the comment element
$this->addElement('textarea', 'comment', array(
'label' => 'Please Comment:',
'required' => true,
'validators' => array(
array('validator' => 'StringLength', 'options' => array(0, 20))
)
));
// Add a captcha
$this->addElement('captcha', 'captcha', array(
'label' => 'Please enter the 5 letters displayed below:',
'required' => true,
'captcha' => array(
'captcha' => 'Figlet',
'wordLen' => 5,
'timeout' => 300
)
));
// Add the submit button
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => 'Sign Guestbook',
));
// And finally add some CSRF protection
$this->addElement('hash', 'csrf', array(
'ignore' => true,
));
}
}
My Controller:
class AdminController extends Zend_Controller_Action
{
public function init()
{
// get doctrine and the entity manager
$this->doctrine = Zend_Registry::get('doctrine');
$this->entityManager = $this->doctrine->getEntityManager();
// get the users repository
$this->indexVideos = $this->entityManager->getRepository('\ZC\Entity\Videos');
$this->indexClips = $this->entityManager->getRepository('\ZC\Entity\Clips');
}
public function indexAction()
{
// action body
}
public function clipsAction()
{
// get a form
$form = new Application_Form_Clips();
$this->view->form = $form;
}
public function videosAction()
{
// action body
}
}
My View:
<?php echo $this->form; ?>
Basic error, I hadn't uncommented session.pat in my php.ini

CakePHP 2 sends out 2 emails each time

I've come across a strange error in CakePHP when sending a contact form to a recipient via e-mail. When a contact form is filled out and is submitted, CakePHP then sends the e-mail to recipient absolutely fine. But instead of sending one email once it seems to send two emails at the same time.
My controller code is this:
var $helpers = array('Html', 'Form', 'Js');
var $components = array('Email', 'Session');
public function index() {
$this->layout = 'modal';
if(isset($this->data['Contact'])) {
$userName = $this->data['Contact']['yourname'];
$userPhone = $this->data['Contact']['yourtelephone'];
$userEmail = $this->data['Contact']['youremail'];
$userMessage = $this->data['Contact']['yourquestion'];
$email = new CakeEmail();
$email->viewVars(array(
'userName' => $userName,
'userPhone' => $userPhone,
'userEmail' => $userEmail,
'userMessage' => $userMessage
));
$email->subject(''.$userName.' has asked a question from the website');
$email->template('expert', 'standard')
->emailFormat('html')
->to('recipient-email#gmail.com')
->from('postman#website.co.uk', 'The Postman')
->send();
if ($email->send($userMessage)) {
$this->Session->setFlash('Thank you for contacting us');
}
else {
$this->Session->setFlash('Mail Not Sent');
}
}
}
And this is the code for the contact form:
<?php
echo $this->Form->create('Contact', array('url' => '/contact/ask-the-expert/', 'class' => 'contact'));
echo $this->Form->input('yourname', array(
'type' => 'text',
'label' => 'Your Name <span class="star">*</span>',
));
echo $this->Form->input('yourtelephone', array(
'type' => 'text',
'label' => 'Your Telephone',
));
echo $this->Form->input('youremail', array(
'type' => 'text',
'label' => 'Your Email <span class="star">*</span>',
));
echo $this->Form->input('yourquestionAnd ', array(
'type' => 'textarea',
'label' => 'Your Question <span class="star">*</span>',
));
echo $this->Form->submit();
echo $this->Form->end();
?>
Cheers!
Put your Mail Send code inside if ($this->request->is('post')) {} and try.
UPDATE
updated the code.
var $helpers = array('Html', 'Form', 'Js');
var $components = array('Email', 'Session');
public function index() {
$this->layout = 'modal';
if(isset($this->data['Contact'])) {
$userName = $this->data['Contact']['yourname'];
$userPhone = $this->data['Contact']['yourtelephone'];
$userEmail = $this->data['Contact']['youremail'];
$userMessage = $this->data['Contact']['yourquestion'];
$email = new CakeEmail();
$email->viewVars(array(
'userName' => $userName,
'userPhone' => $userPhone,
'userEmail' => $userEmail,
'userMessage' => $userMessage
));
$email->subject(''.$userName.' has asked a question from the website');
$email->template('expert', 'standard')
->emailFormat('html')
->to('recipient-email#gmail.com')
->from('postman#website.co.uk', 'The Postman')
if ($email->send($userMessage)) {
$this->Session->setFlash('Thank you for contacting us');
}
else {
$this->Session->setFlash('Mail Not Sent');
}
}
}