hi all i have page that has two buttons, one should allow the person go to the add page and continue adding to the database otherwise if they click the other button it goes to the index page.
currently they both just add the entered information into the database and refresh the page, so when a person clicks the type_2 button they aren't being taken to the index page.
here is the if statement in controller
if ($this->Field->save($this->request->data))
{
if($this->params['form']['type_1'] == 'type_1')
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
}
else if($this->params['form']['type_2'] == 'type_2')
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Templates','action' => 'index'));
}
}
here is the view
<?php
echo $this->Form->create('Field', array('action'=>'add'));
echo $this->Form->create('Field', array('action'=>'add'));
echo $this->Form->input('name', array('label'=>'Name: '));
echo $this->Form->input('description', array('label'=>'Description: '));
echo $this->Form->input('templates_id', array('label'=>'Template ID: ', 'type' => 'text'));//this would be the conventional fk fieldname
echo $this->Form->button('Continue adding fields', array('name' => 'type', 'value' => 'type_1'));
echo $this->Form->button('Finish adding fields', array('name' => 'type', 'value' => 'type_2'));
echo $this->Form->end();
?>
Your if conditions are wrong, you're checking for indices ['form']['type_1'] and ['form']['type_2'], this should be ['form']['type'] in both occasions, and then you check for their value, so it becomes:
if ($this->Field->save($this->request->data))
{
if($this->params['form']['type'] == 'type_1')
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
}
else if($this->params['form']['type'] == 'type_2')
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Templates','action' => 'index'));
}
}
had to use request instead of params
if ($this->Field->save($this->request->data))
{
if($this->request->data['submit'] == "type_1")
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'fields','action' => 'add'));
}
if($this->request->data['submit'] == "type_2")
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'templates','action' => 'index'));
}
}
Related
How to edit a record with Cakephp and modal bootstrap?
Because when I edit a contact, I get the error 500?
missing view The view for UserContactsController:: admin_modal_edit()
was not found.
In to controller admin_modal_edit() I have set $this->layout = NULL;
These are the files of the app.
File: user/view.ctp with list of contact and the modal for add or edit.
BUTTON FOR EDIT OR DELETE
<?php echo $userContact['UserContactType']['title']; ?>: <?php echo $userContact['contact']; ?>
<?php echo __('Edit'); ?>
BOOTSTRAP MODAL
<?php
echo $this->Form->create('UserContact', array('url' => array('admin' => true, 'prefix' => 'admin', 'plugin' => 'user', 'controller' => 'user_contacts', 'action' => 'modal_edit')));
?>
<?php echo $this->Form->input('UserContact.id', array('class' => 'form-control')); ?>
<?php echo $this->Form->input('UserContact.user_id', array('default' => $user_id, 'type' => 'hidden')); ?>
<?php echo $this->Form->input('UserContact.user_contact_type_id', array('class' => 'form-control', 'empty' => true)); ?>
<?php echo $this->Form->input('UserContact.contact', array('class' => 'form-control')); ?>
<?php echo $this->Form->submit(__('Save'), array('div' => false, 'class' => 'btn btn-success')); ?>
<?php echo $this->Form->end(); ?>
File: controller/UsersController.php that generates view.ctp
public function admin_view($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$user_id = $id;
$options = array(
'contain' => array('UserContact' => 'UserContactType', 'UserGroup', 'UserState', 'UserGender', 'UserAddress' => 'UserAddressType', 'UserPaymentType', 'Item', 'Comment'),
'conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
// blocco find list
$userContactTypes = $this->UserContactType->find('list');
$userAddressTypes = $this->UserAddressType->find('list');
$this->set(compact(array('userContactTypes', 'userAddressTypes', 'user_id')));
}
File: controller/UserContactsController.php for the modal
public function admin_modal_edit() {
$id = $this->request->query('id');
$this->layout = NULL;
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->UserContact->save($this->request->data)) {
$this->Session->setFlash(__('The record has been saved'), 'flash/success');
$this->redirect(array('controller' => 'users', 'action' => 'view', $this->request->data['UserContact']['user_id']));
} else {
$this->Session->setFlash(__('The record could not be saved. Please, try again.'), 'flash/error');
}
} else {
if (!empty($id)) {
$options = array('conditions' => array("UserContact.{$this->UserContact->primaryKey}" => $id));
$this->request->data = $this->UserContact->find('first', $options);
}
}
}
Your issue is that CakePHP is trying to render the 'admin_modal_edit.ctp' View template.
If you don't want CakePHP to render anything set autoRender to false:-
public function admin_modal_edit() {
$this->autoRender = false;
}
This will prevent CakePHP from looking for a View template to render.
$this->layout = null does not stop Cake from attempting to render templates. Which I suspect is what you're trying to achieve.
This error is showing because cakephp is complaining about the function admin_modal_edit for not having a view since it cannot find the admin_modal_edit.ctp in the view folder of your controller.
To fix this, add this
$this->autoRender = false
to your function admin_modal_edit to disable the rendering of view for that function.
So your function should look like this.
public function admin_modal_edit() {
/** -------------- **/
$this->autoRender = false;
/** -------------- **/
$id = $this->request->query('id');
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->UserContact->save($this->request->data)) {
$this->Session->setFlash(__('The record has been saved'), 'flash/success');
$this->redirect(array('controller' => 'users', 'action' => 'view', $this->request->data['UserContact']['user_id']));
} else {
$this->Session->setFlash(__('The record could not be saved. Please, try again.'), 'flash/error');
}
} else {
if (!empty($id)) {
$options = array('conditions' => array("UserContact.{$this->UserContact->primaryKey}" => $id));
$this->request->data = $this->UserContact->find('first', $options);
}
}
}
I am developing a plugin for CakePHP 2.
My problem is below:
I have a view with a filter search, where users can search by category:
<div>
<?php echo $this->Form->create('Permission'); ?>
<fieldset>
<?php echo $this->Form->input("permissionCategory", array('label' =>
__d('permission_manager','Categoría'), 'options' => $categorias, 'empty' =>
__d('permission_manager','-- Categorías --'), 'default' => '','class' => 'select-cat', 'onchange' => 'this.form.submit()')); ?>
</fieldset>
<?php //echo $this->Form->end(__d('permission_manager','Aceptar')); ?>
</div>
When I do click over delete button, it shows the confirm message, but when I push yes, the item is not deleted.
I realized it has relation with the action. Maybe with the request type.
The action code:
public function index() {
$this->paginate = array('order' => 'Permission.category asc, Permission.code asc');
$this->Permission->recursive = 2;
if($this->request->is('post')) {
if(empty($this->request->data['Permission']['permissionCategory'])) {
$conditions = null;
} else {
$cat = $this->Permission->findById($this->request->data['Permission']
['permissionCategory']);
$conditions = array('Permission.category ' => $cat['Permission']['category']);
}
$this->Paginator->settings = array(
'conditions' => $conditions );
}
$this->set('categorias', $this->Permission->find('list', array('fields' => 'category',
'group' => 'category')));
$this->set('permissions', $this->Paginator->paginate());
}
Edit:
My delete button:
(I have one like this for row in DB)
<?php echo $this->Form->postLink($this->Html->image('PermissionManager.cross.png',
array('title' =>
__d('permission_manager','Borrar'))),'delete/'.h($permission['Permission']['id']),
array( 'escape' => false), __d('permission_manager','Are you sure... ?'));
?>
My delete action:
public function delete($id = null) {
$this->Permission->id = $id;
if (!$this->Permission->exists()) {
//throw new NotFoundException(__d('permission_manager','Permiso inexistente'));
$this->Session->setFlash(__d('permission_manager','Permiso inexistente'), 'default', array('class'=>'error'));
return $this->redirect(array('action' => 'index'));
}
$this->request->onlyAllow('post', 'delete');
if ($this->Permission->delete()) {
$this->Session->setFlash(__d('permission_manager','El permiso ha sido borrado correctamente.'), 'default', array('class'=>'success'));
} else {
$this->Session->setFlash(__d('permission_manager','El permiso no ha podido borrarse. Por favor, inténtelo de nuevo.'), 'default', array('class'=>'error'));
}
return $this->redirect(array('action' => 'index'));
}
hi all the data from my dropdown box isn't saving correctly to the database, its saving to the foreign key template_id as null
here is the function
function add(){
$this->Session->setFlash("Please create your required fields.");
$templates = $this->Template->find('all', array('fields' => array('Template.id' )));
$this->set('templates', $templates);
if($this->request->is('post'))
{
$this->Field->create();
if ($this->Field->save($this->request->data))
{
if($this->request->data['submit'] == "type_1")
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'fields','action' => 'add'));
}
if($this->request->data['submit'] == "type_2")
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'templates','action' => 'index'));
}
}
else
{
$this->Session->setFlash('The field could not be saved. Please, try again.');
}
}
}
here is the view
<?php
echo $this->Form->create('Field', array('action'=>'add'));
echo $this->Form->create('Field', array('action'=>'add'));
echo $this->Form->input('name', array('label'=>'Name: '));
echo $this->Form->input('description', array('label'=>'Description: '));
echo $this->Form->input('template_id',array('label'=>'Template ID: ', 'options' => $templates));
echo $this->Form->button('Continue adding fields', array('name' => 'submit', 'value' => 'type_1'));
echo $this->Form->button('Finish adding fields', array('name' => 'submit', 'value' => 'type_2'));
echo $this->Form->end();
?>
sorry guys it was a long day, it is saving the correcting information.
my function puts the correct value into the form for my view but doesn't save the correct value in the database. The database is saving the accounts_users.id NOT the accounts_users.account_id to my the code looks like it should be entering the persons accounts_users.account_id. I see the correct information in the dropdown box in the form
add function
function add(){
$accounts=$this->User->AccountsUser->find('list', array(
'fields'=>array('id','account_id'),'conditions' => array(
'user_id' => $this->Auth->user('id'))));
if($this->request->is('post')){
$this->Template->create();
if ($this->Template->save($this->request->data)) {
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
} else {
$this->Session->setFlash('The template could not be saved. Please, try again.');
}
}
here is the add view
<?php
echo $this->Form->create('Template', array('action'=>'add'));
echo $this->Form->input('name',array('label'=>'Template Name: '));
echo $this->Form->input('account_id',array('label'=>'Business: ', 'type' => 'select', 'options' => $accounts));
echo $this->Form->input('description',array('label'=>'Short Description Of Template: '));
echo $this->Form->end('Click Here To Submit Template');
?>
Try the following code:
function add(){
$accounts=$this->User->AccountsUser->find('list', array(
'fields'=>array('account_id','account_id'),'conditions' => array(
'user_id' => $this->Auth->user('id'))));
if($this->request->is('post')){
$this->Template->create();
if ($this->Template->save($this->request->data)) {
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
} else {
$this->Session->setFlash('The template could not be saved. Please, try again.');
}
}
How can I do edits to a table on multiple rows?
I followed the tutorial here http://matsimitsu.com/blog/2008/01/06/saveall-with-cakephp.html but it doesn't seem to work.
Here is what I'm doing, but it's not working.
Thanks,
Tee
function editAll() {
$this->data = $this->Settings->find('all', array('conditions' => $conditions));
}
Then in the view, this is what I have
foreach ($this->data as $setting):
echo $form->input('Setting.' . $setting['Setting']["id"] . '.value', array('value' => $setting['Setting']["value"]));
endforeach;
Then in the add function I have
function add() {
if (!empty($this->data)) {
$this->Setting->create();
if ($this->Setting->saveAll($this->data)) {
$this->Session->setFlash(__('The Setting has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The Setting could not be saved. Please, try again.', true));
}
}
}
You need to include the id field, so the data in your controller will look like this:
'Setting' => array(
0 => array(
'id' => 42,
'value' => 'foo'
),
1 => array(…)
)
So in the view, do this:
foreach ($this->data as $i => $setting) {
echo $this->Form->hidden("Setting.$i.id", array('value' => $setting['Setting']['id']));
echo $this->Form->input("Setting.$i.value", array('value' => $setting['Setting']['value']));
}