Cakephp multiple forms in single view - forms

On my edit view I have created 2 forms :
<?php echo $this->Form->create('EditPasswordUserForm')); ?>
<?php echo $this->Form->create('EditInfoUserForm')); ?>
Hence, I created 2 Models (2 files) :
class EditPasswordUserForm extends User
{
and
class EditInfoUserForm extends User
{
Controller User :
public function edit($slug)
{
$this->loadModel('EditPasswordUserForm');
$this->loadModel('EditInfoUserForm');
$editpassword = $this->EditPasswordUserForm->findBySlug($slug);
$editinfo = $this->EditInfoUserForm->findBySlug($slug);
if(empty($this->data))
{
$this->request->data['EditPasswordUserForm'] = $editpassword['EditPasswordUserForm'];
$this->request->data['EditInfoUserForm'] = $editinfo['EditInfoUserForm'];
}
}//end edit
I got this error message :
Erreur: Class 'User' not found
Fichier: C:\xampp\htdocs\projectmvc\app\Model\EditPasswordUserForm.php
Could you please help me.
Thank you
Ligne: 4

You just import User model in your extend models, like as
App::import('Model', 'User');
class EditPasswordUserForm extends User
{
//
}
and
App::import('Model', 'User');
class EditInfoUserForm extends User
{
//
}
For best documentation read Behavior for Model Inheritance

You don't need to create two classes.
You can just do:
echo $this->Form->create('User', array('action' => 'editPw'));
echo $this->Form->create('User', array('action' => 'editInfo'));
In your UsersController:
class UsersController extends AppController {
public function editPw() {...}
public function editInfo() {...}
}

Related

Rendering Custom View in SuiteCRM not working

I have a controller that calls my custom view, but the custom view is not getting rendered.
I cant understand the issue, everything looks correct to me.
Neither is it displaying any errors or warnings.
My module name is SCRV_SSRS_CRM_Reports_View
I have below code in:
custom/modules/SCRV_SSRS_CRM_Reports_View/controller.php
require_once('include/MVC/Controller/SugarController.php');
class SCRV_SSRS_CRM_Reports_ViewController extends SugarController
{
function action_test(){
$GLOBALS['log']->fatal('Am in Controller');
$this->view = "test";
}
}
And in
custom/modules/SCRV_SSRS_CRM_Reports_View/views/view.test.php
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('include/MVC/View/views/view.list.php');
class SCRV_SSRS_CRM_Reports_ViewViewtest extends ViewList
{
public function display()
{
echo "HIIII";
$GLOBALS['log']->fatal('Am in View');
}
}
Cant see the view getting rendered when I access it by
http://localhost:8080/dev-crm/index.php?module=SCRV_SSRS_CRM_Reports_View&action=test
I cant see HIIII displayed on screen or the log entry.
Controller should be
class CustomMeetingsController extends SugarController {
$this->view = 'invite';
}
and View should be like
class CustomMeetingsViewinvite extends ViewList {
public function display() {
echo 'hiiiii';
}
}

Trying to disable layout in zend

I try to disable layout in zend action in this way:
$this->_helper->layout->disableLayout();
But it doesn't work:
Fatal error: Call to undefined method Application_Controller_Helper_Layout::disableLayout() in application/controllers/AssetController.php on line 18
I reckon that its, because I made my own helper.
Bootsrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initMyActionHelpers()
{
$this->bootstrap('frontController');
$layout = Zend_Controller_Action_HelperBroker::getStaticHelper('Layout');
Zend_Controller_Action_HelperBroker::addHelper($layout);
}
}
application.ini
resources.frontController.actionhelperpaths.Application_Controller_Helper = APPLICATION_PATH "/controllers/helpers"
and my helper
class Application_Controller_Helper_Layout extends Zend_Controller_Action_Helper_Abstract
{
public function preDispatch()
{
}
}
Thanks!
If you want to rewrite Zend default layout helper, please extend Zend_Layout_Controller_Action_Helper_Layout.
class Application_Controller_Helper_Layout extends Zend_Layout_Controller_Action_Helper_Layout
Then you will still be able to use default disableLayout() function if you are not overwriting it in your own helper.
Try testing it using:
class Application_Controller_Helper_Layout extends Zend_Layout_Controller_Action_Helper_Layout
{
public function preDispatch(){}
public function disableLayout(){
echo "I won't disable layout anymore, because I overwrote the action with displaying this string.";
}
}

Change name and id in form

I would change my Return form
I create form with a loop inside my twig, and I would like to change the name and id fields
eg
<input type="text" id="foo_1" name="foo_1[title]" maxlength="255">
I want to add an index like I did for the id of the form {{loop.index}}
I might have to pass something to the constructor of my form, but I have not found anything that has helped me
For Now i do It:
My controller
foreach($fotos as $key => $foto){
$array_fotos[] = array(
"form" => $this->createForm(new \My\FotoBundle\Form\FotoType($key), $fotos[$key])->createView(),
);
}
In My FormType i do this
class FotoType extends AbstractType
{
protected $key;
public function __construct($chiave) {
$this->key = $chiave;
}
.....
public function getName()
{
return 'form_gestione_foto_'.$this->key;
}
}

How to correctly address associations data in forms with cakephp 2.3

I am a pre-newbie with cakephp.
I have a "users" table and a "category" table
user belongsTo category (fields: users.id, users.name, users.category)
category hasMany users (fields: category.id, category.name, users.category)
I am addressing associating data like this.
in (users) edit.ctp I put
// view/Users/edit.ctp
echo $this->Form->input('name');
echo $this->Form->input('categories', array( 'value' => $this->Form->value('User.category'),
'name'=>'data[User][category]') );
</pre>
in users controller I have
<pre>
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->User->read(null, $id);
}
$sexes = $this->User->Sex->find('list');
$categories = $this->User->Category->find('list');
$this->set(compact('categories'));
}
Everything is working, but i suspect there's a much easier way to do that form.
Is this realy needed?
, array( 'value' => $this->Form->value('User.category'), 'name'=>'data[User][category]')
Without theese params the select box does not highlight the selected option, and nothing is saved.
Could be something like
echo $this->Form->input('Category.name');
for instance? But code like that does not show a select box.
And it doesn't save the users.category field.
I was not able to find any tutorial or code with samples about this.
Links would be appreciated.
Try to use Cake's nameing conventions for database tables and fields. If you follow the conventions Cake will do a lot of the heavy lifting for you:
users table:
users.id, users.name, users.category_id
categories table
categories.id, categories.name
User Model
class User extends AppModel {
public $belongsTo = array(
'Category'
);
}
Category Model
class Category extends AppModel {
public $hasMany = array(
'User'
);
}
Users Controller:
class UsersController extends AppController {
public function edit($id) {
if (!empty($this->request->data) {
// form submitted - try to save
if ($this->User->save($this->request->data)) {
$this->Session->setFlash('User updated');
}
else {
$this->Session->setFlash('Please correct the errors');
}
}
else {
// prepopulate the form
$this->request->data = $this->User->findById($id);
}
// populate the categories dropdown
$this->set('categories', $this->User->Category->find('list');
}
}
/app/views/Users/edit.ctp
<?php
echo $this->Form->create();
echo $this->Form->inputs();
echo $this->Form->end('Update');

common constant variables in Zend Framework

Where is the best place to create a class that contain all the application constant variables ?
is it :
- Bootstrap
- In the Application Common library
for example :
1- When i retrieve an image name from a database if this record doesnt have an image , i want to put a default value somewhere so i can use it in my models
** a constants that i use in all my application so if i change it , i dont want to go back to all in my code and change it everywhere
application.ini is the best place for example define some constant there
constants.PUBLIC_PATH = APPLICATION_PATH "/../public/"
constants.THEME = blue
Then in your bootstrap do
protected function setConstants($constants)
{
foreach($constants as $name => $value)
{
if(!defined($name))
define($name, $value);
}
}
ZF take 'constants' from config and call setConstants method in your bootstrap passing all lines prefixed by constants hence its an array .
I try not use constants and prefer class constants over global constants. However, when constants are unavoidable, for whatever reason, I go with .ini config file and Bootstrap/library/model class constants.
An example of .ini config constants
(assumes default zf project structure)
application/configs/application.ini
constants.MESSAGE_1 = "Message 1"
constants.MESSAGE_2 = "Message 2"
Bootstap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initConstants()
{
$options = $this->getOption('constants');
if (is_array($options)) {
foreach($options as $key => $value) {
if(!defined($key)) {
define($key, $value);
}
}
}
}
// ...
}
Example usage in controller:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->message1 = MESSAGE_1;
$this->view->message2 = MESSAGE_2;
}
}
I would extend on the above to allow configuration of how your constants are defined. For example you may want all constants UPPERCASED or not, and allow or disallow already defined constants, so:
application/configs/application.ini
constants.forceUppercase = 1
constants.allowAlreadyDefined = 1
constants.set.message_1 = "Message 1"
constants.set.message_2 = "Message 2"
Bootstrap:
protected function _initConstants()
{
$options = $this->getOption('constants');
if (isset($options['set']) && is_array($options['set'])) {
if (isset($options['forceUppercase'])
&& (bool) $options['forceUppercase']) {
$options['set'] = array_change_key_case($options['set'], CASE_UPPER);
}
$allowAlreadyDefined = false;
if (isset($options['allowAlreadyDefined'])
&& (bool) $options['allowAlreadyDefined']) {
$allowAlreadyDefined = true;
}
foreach($options['set'] as $key => $value) {
if (!defined($key)) {
define($key, $value);
} elseif (!$allowAlreadyDefined) {
throw new InvalidArgumentException(sprintf(
"'%s' already defined!", $key));
}
}
}
}
Bootstrap class constants
Could be your own library, or model class, etc., it depends.
In the bootstrap:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
const MESSAGE_CONSTANT = "Hello World";
// ...
}
Example usage in controller:
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$this->view->message = Bootstrap::MESSAGE_CONSTANT;
}
}