Form action showing two controller actions in cakephp form - forms

I am new to cakephp and currently building a login authentication page. My users_controller.php is:
<?php
class UsersController extends AppController {
var $name = 'Users';
var $components = array('Auth');
function index() {
}
function login() {
}
function logout() {
$this->redirect($this->Auth->logout);
}
}
And login.ctp is
<?php
echo $this->Session->flash('auth');
echo $this->Form->create('Users');
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->end('Login');
?>
But when I checked the page source, the action of the form is <form action="/student/app/webroot/index.php/users/users/login" id="UsersLoginForm" method="post" accept-charset="utf-8">. As you can see there are two users controller in the action . Why this additional user is coming ?
NB: Cakephp version 1.3. All our projects were done in this version and not yest upgraded !

Related

codeigniter, form validation false verse first time visit

In Codeigniter, the following code is typically used for a page that has a form. But the first time a user lands on the page and a form validation fails gets routed through the same path.
As this example shows, the flash data will trigger. even if the user just land on the page and have not submit any form yet.
I am trying to echo a new class name to some input field to highlight them if validation fails. but currently it highlights the field on first load as well.
I am aware I can echo a validation_error or form_error. is there a way to echo a generic message that is not tied to a field-name and only after submission fails
// rules and other stuff above
if ($this->form_validation->run() == FALSE){
$this->session->set_flashdata('errorClass',"is-invalid");
$this->load->view('defaultOrFalse');
}else{
$this->load->view('success');
}
//view file
<input class=" <?php $this->session->flashdata('errorClass') ; ?>">
Basically I am trying to get bootstrap 4's input validation to show up
https://getbootstrap.com/docs/4.0/components/forms/#server-side
I don't know your exact setup but you can do logic like the following:
<?php
class Some_controller extends CI_Controller {
// controller/search/{$term}
public function some_method($term = null) {
// where some_field is some field in your form
// that gets posted on submit
if ($this->input->post('some_field')) {
// or if (isset($_POST)) {
if ($this->form_validation->run() == FALSE) {
$this->session->set_flashdata('errorClass', "is-invalid");
$this->load->view('defaultOrFalse');
} else {
$this->load->view('success');
}
} else {
// default view
}
}
}
?>
For your second question:
<h5>Username</h5>
<?php echo form_error('username'); ?>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" <?php if (!empty(form_error('username'))) { echo "class='error'"; } ?> />
Can also make a helper and use instead of form_error to check if field has error for your class (haven't verified this works but it should).
/**
* Checks if form validation field by name
* has error
*
* #param string $field Name of field
* #return boolean
*/
function field_has_error($field) {
$CI = &get_instance();
$CI->load->library('form_validation');
$arr = $CI->form_validation->error_array();
if (isset($arr[$field])) {
return true;
}
return false;
}
Usage:
<?php if (field_has_error('username')) { echo "class='error'"; } ?> />

how to pass hidden parameters in the url

//i want hidden parameters send in my controler/method
suppose my url link Category/category_product_get/id; ?> but i want id; ?> is hide and pass my controller/method
<?php
if($Category):
foreach($Category as $main):
?><a href="<?php echo base_url() ?>Category/category_product_get/<?php echo $main->id; ?><?php endforeach;endif;?>
//this is my controller Category.php
public function category_product_get($id = null) {
echo $id;
}
You can`t hide parameters with url, you can send id using any encryption method like base64_encode().

How to handle a failed login with ZfcUser's LoginWidget?

In my Zend Framework 2 application I am using the zfcUserLoginWidget, a View Helper provided by ZfcUser. This View Helper is meant to embed the login form on another page.
<?php echo $this->zfcUserLoginWidget(); ?>
I use this View Helper in my index.phtml which is controlled by my IndexController. Pressing the "Login" button calls the loginAction function of the UserController. If the entered login data are invalid, the UserController redirects by default to the login page:
class UserController extends AbstractActionController
{
const ROUTE_LOGIN = 'zfcuser/login'; // Leads to 'user/login'
public function loginAction()
{
// ...
if (!$form->isValid()) {
$this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage($this->failedLoginMessage);
return $this->redirect()->toUrl($this->url()->fromRoute(static::ROUTE_LOGIN).($redirect ? '?redirect='. rawurlencode($redirect) : ''));
}
// ...
}
}
The login.phtml displays by default an error message if the entered login data has been invalid:
<?php echo $this->formElementErrors($form->get('identity')) ?>
In my application I have changed the redirection route back to the index.phtml (to the LoginWidget) controlled by the IndexController instead of the login.phtml controlled by the UserController:
const ROUTE_LOGIN = 'home'; // Leads to 'index/index'
Changing this redirection route causes the loss of error messages: The previously mentioned formElementErrors function is still called, but there are no messages available anymore.
Question:
How can I inform the LoginWidget about the fact that there has been entered invalid login data and that the page has not only been simply reloaded?
I'd like to colorize the input fields red if that is the case...
having the same problem i solved it using use Zend\Session\Container, generating my own message.
Modified:
ZendSkeletonApplication\vendor\zf-commons\zfc-user\src\ZfcUser\Controller\UserController.php
Add
use Zend\Session\Container;
const ROUTE_LOGIN = 'home';
in loginAction of UserController.php added/changed
//open session
$session = new Container('loginmsg');
if (!$form->isValid()) {
//$this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage($this->failedLoginMessage);
//$this->flashMessenger()->addMessage('not working');
//return $this->redirect()->toUrl($this->url()->fromRoute(static::ROUTE_LOGIN).($redirect ? '?redirect='.$redirect : ''));
$session->loginmsg = 'Username and password do not match.'; //store loginmsg
return $this->redirect()->toRoute('home');
} else {
$session->loginmsg = "";
}
Also modify the authenticateAction
if (!$auth->isValid()) {
$this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage($this->failedLoginMessage);
$adapter->resetAdapters();
//open session
$session = new Container('loginmsg');
$session->loginmsg = 'Username and password do not match.'; //store loginmsg
return $this->redirect()->toUrl($this->url()->fromRoute(static::ROUTE_LOGIN)
. ($redirect ? '?redirect='.$redirect : ''));
}
In the home Route (indexAction):
$session = new Container('loginmsg');
$loginmsg = strip_tags($session->loginmsg);
$session->getManager()->getStorage()->clear('loginmsg');
$this->layout('layout/index');
return new ViewModel(array(
'loginmsg' => $loginmsg,
));
In the View of the Index
<?php if(!(empty($loginmsg))):?>
<div class="alert alert-danger" role="alert"><?php echo $loginmsg;?></div>
<?php endif; ?>
Worked for me (at first), msg me for further information.
Kind regards,
David

cakephp multiple forms with same action

I've got on my page several News, to every News we can add comment via form.
So actually I've got 3 News on my index.ctp, and under every News is a Form to comment this particular News. Problem is, when i add comment, data is taken from the last Form on the page.
I don;t really know how to diverse them.
i've red multirecord forms and Multiple Forms per page ( last one is connected to different actions), and i don't figure it out how to manage it.
Second problem is, i can't send $id variable through the form to controller ( $id has true value, i displayed it on index.ctp just to see )
This is my Form
<?php $id = $info['Info']['id']; echo $this->Form->create('Com', array('action'=>'add',$id)); ?>
<?php echo $this->Form->input(__('Com.mail',true),array('class'=>'form-control','field'=>'mail')); ?>
<?php echo $this->Form->input(__('Com.body',true),array('class'=>'form-control')); ?>
<?php echo $this->Form->submit(__('Dodaj komentarz',true),array('class'=>'btn btn-info')); ?>
<?php $this->Form->end(); ?>
and there is my controller ComsController.php
class ComsController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Session');
public function index()
{
$this->set('com', $this->Com->find('all'));
}
public function add($idd = NULL)
{
if($this->request->is('post'))
{
$this->Com->create();
$this->request->data['Com']['ip'] = $this->request->clientIp();
$this->request->data['Com']['info_id'] = $idd;
if($this->Com->save($this->request->data))
{
$this->Session->setFlash(__('Comment added with success',true),array('class'=>'alert alert-info'));
return $this->redirect(array('controller'=>'Infos','action'=>'index'));
}
$this->Session->setFlash(__('Unable to addd comment',true),array('class'=>'alert alert-info'));
return false;
}
return true;
}
}
you are not closing your forms
<?php echo $this->Form->end(); ?>
instead of
<?php $this->Form->end(); ?>
for the id problem you should write
echo $this->Form->create(
'Com',
array('action'=>'add/'.$id
)
);
or
echo $this->Form->create(
'Com',
array(
'url' => array('action'=>'add', $id)
)
);

Joomla 2.5 - component development - using form

I am trying to add some form to my component, but I am not shure what naming conventions must be applied to work it correctly.
Currently I have a working form - it displays fields stored in XML file and loads data from database to it. However, when i try to submit this form (edit or add new records), it doesn't work. After pressing submit (save() method) it just redirects me and displays that record was edited successfuly but it wasn't. When I try to edit existing record, after pressing submit nothing happens and when I try to add new record, it just adds empty/blank record.
So I was doing a little debug and discovered, that problem is in the JController::checkEditId() method. It always returns false which means that JControllerForm::save() returns false as well and that's why it doesn't save it correctly. HTML code of form is correct and I can access the data by using global array $_POST.
I suspect that this problem is because of naming conventions in methods loadFormData, getForm of JModelAdmin class. I am not sure how to name that form.
So here is my code related to this problem:
Subcontroller for displaying the form - controllers/slideshowform.php
class SlideshowModelSlideshowForm extends JModelAdmin{
public function getForm($data = array(), $loadData = true){
return $this->loadForm('com_slideshow.slideshowform', 'editform', array('load_data' => $loadData, 'control' => 'jform'));
}
protected function loadFormData(){
$data = JFactory::getApplication()->getUserState('com_slideshow.edit.slideshowform.data', array());
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
public function getTable($table = "biometricslideshow"){
return parent::getTable($table);
}
}
views/slideshowform/view.html.php
class SlideshowViewSlideshowForm extends JView{
public function display($tmpl = null){
if (count($errors = $this->get('Errors')))
{
JError::raiseError(500, implode('<br />', $errors));
return false;
}
$this->form = $this->get('form');
$this->item = $this->get('item');
JToolBarHelper::save('slideshowform.save');
parent::display();
}
}
views/slideshowform/tmpl/default.php
<?php
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
?>
<form method="post" action="<?php echo JRoute::_("index.php?option=com_slideshow&id=".(int) $this->item->id)?>" name="adminForm" id="slideshow-form">
<fieldset class="adminform">
<legend>Edit slide</legend>
<table>
<input type="hidden" name="task" value="">
<?php echo JHtml::_('form.token'); ?>
<?php
foreach($this->form->getFieldset() as $field){
?>
<tr><td><?php echo $field->label ?></td><td><?php echo $field->input ?></td></tr>
<?php
}
?>
</table>
</fieldset>
</form>
Can someone take o look, please?
you have to add controller SlideshowControllerSlideshowForm and code save method. In there you have to validate the form data and call SlideshowModelSlideshowForm->save event, then redirect with success/failure message.