Jump to content


How to add custom field in edit/add view that doesn't exist in database?

fields custom fields add edit

  • Please log in to reply
9 replies to this topic

#1 waqar

waqar

    Member

  • Members
  • PipPip
  • 10 posts

Posted 11 September 2012 - 11:11 AM

I have a table of Transactions with columns like:
date
title
deposit
withdraw

I want to show the add/edit form the following fields:
date
title
amount
type (deposit or withdraw)

I want the user to select whether the type of this transaction he's adding is of deposit or withdraw, and then before insertion I want the system to figure out what user has selected, and then set the field value (either deposit or withdraw) to what he has chosen, from the field of amount.

But the problem is, I can't add a custom field to the add/edit form that doesn't exist in database, because if I do, the insertion of CRUD simply fails.

Here's some code snippet, can anyone help?
// Set title or subject of this CRUD
   $crud->set_subject('Transaction');
   // Set database table name for CRUD operations
   $crud->set_table('transactions');
   // Set Relation of tables for CRUD operations
   $crud->set_relation('category_id','category', 'title');
  
   // Columns for Grid
   $crud->columns('date','title','category_id', 'deposit', 'withdraw', 'balance');
   $crud->unset_fields('deposit','withdraw');
   // Order by
   $crud->order_by('date','asc');
  
   // Load custom Transactions_model
   $crud->set_model('Transactions_model');
  
   // Callbacks for columns on grid
   //$crud->callback_column('deposit',array($this,'transaction_type'));
   //$crud->callback_column('balance',array($this,'transaction_balance'));
  
   // Display field as
   $crud->display_as('title','Description');
   $crud->display_as('category_id','Category');
  
   // Fields for add/edit form
   //$crud->fields('date','title','deposit','withdraw','amount','category_id');
   // Hide some fields from add/edit form from user
   // $crud->change_field_type('deposit','invisible');
   // $crud->change_field_type('withdraw','invisible');
  
   // Required fields
   $crud->required_fields('date','title','category_id');
  
   // Callback on field on add/edit form
   //$crud->callback_before_insert(array($this,'amount_callback'));
   // Render Crud
   $output = $crud->render();
  
   // Load view
   $this->_transactions_output($output);


#2 waqar

waqar

    Member

  • Members
  • PipPip
  • 10 posts

Posted 11 September 2012 - 12:01 PM

SOLVED :)

I added a field of amount with $crud->fields, I hid the fields of deposit and withdraw on add/edit form, displayed an input field for amount manually through a callback function, and then through another callback before insert I set the field values for deposit/withdraw and unset the amount field so it didn't cause issues with CRUD. Here's the updated code snippet, hope this helps someone :)

// Fields for add/edit form
   $crud->fields('date','title','deposit','withdraw','amount','category_id');
   // Hide some fields from add/edit form from user
   $crud->change_field_type('deposit','invisible');
   $crud->change_field_type('withdraw','invisible');
  
   // Required fields
   $crud->required_fields('date','title','category_id','amount');
  
   // Callbacks
   $crud->callback_add_field('amount',array($this,'amount_field_add_callback'));
   $crud->callback_before_insert(array($this,'amount_callback'));
   // Render Crud
   $output = $crud->render();

And the callback functions are as follows:
function amount_field_add_callback()
{
  return '<input type="text" maxlength="50" value="" name="amount">';
}

function amount_callback($post_array) {
  // category id
  $category_id = $post_array['category_id'];
  // get category type whether income or expense
  $category_type = $this->Transactions_model->get_category_type($category_id);
  // if type "income"
  if ($category_type->title == 'income') {
   // set deposit field
   $post_array['deposit'] = $post_array['amount'];
  } else { // else if type "expense"
   // set withdraw field
   $post_array['withdraw'] = $post_array['amount'];
  }
  // Unset field of amount as that does not exist in database.
  unset($post_array['amount']);
  //array_splice(1, count($post_array), $post_array['amount']);
  // return field variables with updated information for insertion
  return $post_array;
}


Thanks
Waqar

We build great Web and Software Applications and Software Products for you @ 4 Ace Technologies

#3 saulimus

saulimus

    Advanced Member

  • Members
  • PipPipPip
  • 40 posts

Posted 11 September 2012 - 12:03 PM

If I understood your problem correctly, you can easily unset a field before inserting, like this:
$crud->callback_before_insert(array($this,'unset_some_field'));
function unset_some_field($post_array)
{
	unset($post_array['some_field']);
	return $post_array;
}
EDIT: Crap, I was a couple of minutes late! I'm adding this anyways..

#4 waqar

waqar

    Member

  • Members
  • PipPip
  • 10 posts

Posted 11 September 2012 - 12:05 PM

@saulimus: Thanks for the help, a good deed is always appreciated :)

#5 Terence Vusile Silonda

Terence Vusile Silonda

    Member

  • Members
  • PipPip
  • 11 posts

Posted 10 January 2013 - 09:20 AM

This is kinda late, but I hope it'll be helpful.

If I were you, I would have this table structure:

date
amount
title
transaction_type


Transaction Type would be a drop down, with options Deposit and Withdraw.

So when a user saves a transaction, the amount is just stored in one field (amount). You can then figure out whether it's a deposit or withdraw using the transaction_type field.

What do you think?

Think it'll make your life much simple :)

#6 kenshicu

kenshicu

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts
  • LocationHolguín, Cuba

Posted 25 March 2013 - 03:27 PM

in add/edit form I need to put a selectbox (field4), which does not exist in the database, so I put:

$crud->fields('field1','field2','field3','field4');

 

but to save the form, by clicking the button (Save and go back to list), the system does nothing, does not save. :(
 



#7 Christophe Conduché

Christophe Conduché

    Member

  • Members
  • PipPip
  • 19 posts

Posted 15 April 2013 - 04:43 PM

I tried this, but the callback is not called...

 

    public function piltdm()
    {

        $this->grocery_crud->set_table('piltdm');
	$this->grocery_crud->columns('nom','prenom','surnom', 'date_fin_parapro');
    	$this->grocery_crud->set_subject('Pilote Tandem');
 
// WHERE pour filtrer une liste, par exemple les passagers d'un jour en particulier
//    $crud->where('status','active');


    $this->grocery_crud->set_relation_n_n('Attibuts', 'piltdm_attributs', 'attributs', 'piltdm_id', 'attributs_id', 'valeur');

//   RELATION 1:N not working... Try to do something else
//    $this->grocery_crud->set_relation('piltdm_id','parachutes','nom');


// ADD A NON EXISTING FIELD TO DISPLAY A LIST
   $this->grocery_crud->fields('nom','prenom','surnom','listpara');

// Callbacks pour ajouter la liste des parachutes sur la creation/modification du pilote
   $this->grocery_crud->callback_add_field('listpara',array($this,'listpara_field_add_callback'));

// Pour controler les dates
    $this->grocery_crud->callback_column('date_fin_parapro',array($this,'checkDate'));

        $output = $this->grocery_crud->render();
 
        $this->_piltdm_output($output);        
    }
 
    function _piltdm_output($output = null)
    {
        $this->load->view('gestpara_template_desk.php',$output);    
    }

// THIS SHOULD DISPLAY A LIST FROM THE RELATED TABLE
function listpara_field_add_callback()
{

       
	$html = 'bla<ul>';
	$positions = $this->db->get_where('parachutes',array('piltdm_id'=>$post_array['piltdm_id']))->result_array();
	if($positions)
	{
	   foreach ($positions as $items)
	  {
	   $html.='<li>'.$items['nom'].'</li>';
	  }
	}
	$html.='</ul>bla';
	return $html;
}



function checkDate($value, $row)
{
    return $value.' LIMITE';
}



#8 jaorsoftware

jaorsoftware

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 11 May 2013 - 08:12 AM

Hi Folks !

 

I am using the function callbacks but I have a trouble.

 

The function work correctly whene I "ADD" a record

but when I try to edit the record, the function don't work

 

WHy?

 

Thank you in advice 

 



#9 davidoster

davidoster

    Grocery CRUD Ninja

  • Advanced Member
  • PipPipPip
  • 1,068 posts
  • LocationAthens, Greece

Posted 12 May 2013 - 06:27 AM

Hi Folks !

 

I am using the function callbacks but I have a trouble.

 

The function work correctly whene I "ADD" a record

but when I try to edit the record, the function don't work

 

WHy?

 

Thank you in advice 

 

Please post a new question to the forum with your controller's code.


____________________________________________________________

 

rtfm_small.jpg---!!!Please read these guidelines before asking to the forums!!!---

____________________________________________________________

 

David Oster aka George Pasparakis,
http://odphotography.com
http://eletter.gr


#10 Ali Kazmi

Ali Kazmi

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 24 December 2014 - 01:23 PM

Thanks for the help, a good deed is always appreciated

 

 

Recently you asked the a question and again you provide the answer solved, I don't get what's you actually want. When you know real code why did create thread.


 

 






Also tagged with one or more of these keywords: fields, custom fields, add, edit

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users