⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

way to add other fields in the add/edit forms



crina
  • profile picture
  • Member

Posted 08 March 2012 - 11:30 AM

Hello again,

I was wondering, and trying to find a solution for a way to add some extra fields in the add/edit forms ...
except those in the DB ...

for example I need a checkbox that if is checked it will make an ajax that inserts the same values once more in the DB ... for a billing and delivery address ... if you check that the address is the same it will put the same infos in other fields too ...

hope you understand what I need to do ... and maybe help me to choose the best solution ...

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 08 March 2012 - 21:46 PM

Hello @crina,

This will be a long answer as you want something custom , but I hope this will help you.

First of all you can add as many fields and columns you like without being in the database, for example:


$crud->fields('title', 'description', 'actors' , 'category' ,'release_year', 'rental_duration','test1','test2','test3');


The fields test1, test2, test3 doesn't exist anywhere in the table on the database. But I just add them for the example. Grocery CRUD will check if the fields exist at the table and if not just render it as input text field.

The next step is to add a callback for the test1, test2, test3 to make it work with your way for example:

$crud->callback_field('test1',array($this,'callback_test1'));




function callback_test1($value = null, $primary_key = null)
{
//You can do it strait forward
$output = "<script type='text/javascript'>.....</script>";
$output .= "<input type='text' name='test1' value='test' id='test1'/>";

//Or with a view
//$output = $this->load->view('whatever',array('value'=>$value),true);

return $output;
}


After this you have to make sure that you have a callback_before_insert and a callback_before_update , just to ensure that you will not have any errors on your database, as the crud will try to add them to the table. But the table doesn't have these fields so it will throw an error that column doesn't exist.

I have an example below:



$crud->callback_before_insert(array($this,'callback_before_insert_or_update_test1'));
$crud->callback_before_update(array($this,'callback_before_insert_or_update_test1'));


and the callback:



function callback_before_insert_or_update_test1($post_array, $primary_key = null)
{
unset($post_array['test1'],$post_array['test2'],$post_array['test3']);

//Or do something with your post arrat here
if($primary_key !== null)//This means that you update and not insert something
{
$this->db->update(....);
}
else
{
$this->db->insert(....);
}

return $post_array;
}


And you are ready. If you follow the steps and you understand how grocery CRUD works I thing is the best solution (You see? This time without changing the core ;) )

I have also all the example to just copy-paste it and play with it.

function film_management()
{
$crud = new grocery_CRUD();

$crud->set_table('film');
$crud->set_subject('Film');
$crud->set_relation_n_n('actors', 'film_actor', 'actor', 'film_id', 'actor_id', 'fullname','priority');
$crud->set_relation_n_n('category', 'film_category', 'category', 'film_id', 'category_id', 'name');

$crud->unset_columns('description');
$crud->fields('title', 'description', 'actors' , 'category' ,'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features','test1','test2','test3');
//$crud->set_theme('datatables');
$crud->callback_field('test1',array($this,'callback_test1'));
$crud->callback_before_insert(array($this,'callback_before_insert_or_update_test1'));
$crud->callback_before_update(array($this,'callback_before_insert_or_update_test1'));

$crud->unset_texteditor('description');

$output = $crud->render();

$this->_example_output($output);
}

function callback_test1($value = null, $primary_key = null)
{
//You can do it strait forward
$output = "<script type='text/javascript'>.....</script>";
$output .= "<input type='text' name='test1' value='test' id='test1'/>";

//Or with a view
//$output = $this->load->view('whatever',array('value'=>$value),true);

return $output;
}

function callback_before_insert_or_update_test1($post_array, $primary_key = null)
{
unset($post_array['test1'],$post_array['test2'],$post_array['test3']);

//Or do something with your post arrat here
if($primary_key !== null)//This means that you update and not insert something
{
//$this->db->update(....);
}
else
{
//$this->db->insert(....);
}

return $post_array;
}


[b]Tip of the day:[/b] Did you know that you can use the callbacks with anonymous functions (such as jquery) at PHP 5.3 or later? for example you can also do:



$crud->callback_before_insert( function ($post_array, $primary_key = null){
unset($post_array['test1'],$post_array['test2'],$post_array['test3']);
return $post_array;
});

mandeep
  • profile picture
  • Member

Posted 06 August 2012 - 15:12 PM

Is it possible to add multiple data entry fields (in the add form) for one column. For example, I have a name Column that I want to be able to enter multiple entries for (so it will appear multiple times) but, I only want to enter data for the other columns once.

Rachma
  • profile picture
  • Member

Posted 23 December 2014 - 08:25 AM

I have a problem like this. And I've tried the way as web-johnny said. But I use callback_after_insert, because I need to join two tables that the child table have multiple value to insert. But it's not working. I don't understand. Can anyone help me please?

 

[sharedmedia=core:attachments:887]
[sharedmedia=core:attachments:884]

Amit Shah
  • profile picture
  • Member

Posted 24 December 2014 - 07:13 AM

To my understanding where you are failing in here is @the time of insert where what is happening is it is finding in much more fields then that are in the table and it is building up the insert statement on all the fields provided but it and thats where its failing in technically. So what i suggest a solution in such a scenario

 

1 - have a callback_before_insert ... here extract all the fields that are supposed to be posted in callback_after_insert

2 - Store the same in to the session

3 - Unset them (make the post_array clean and set it to have only those fields that are supposed to be going in the master table)

4 - Make a callback after insert

5 - Extract the fields to be used here from the session .. and store it in the respective tables.

 

Hope this solution helps you as it dose help me a lot.