⚠ 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

How do I hide a field when editing that is visible when adding?

  • profile picture
  • Member

Posted 29 November 2012 - 05:07 AM


Long time listener, first time caller...

My table structure is as follows:
id (pk)
equip_type_id (fk to equip_types)
site_id (fk to sites)
equip_status_id (fk to equip_status)

(equip_type_id, site_id, name) is a composite unique key constraint in the db. I have implemented a callback on the name field that deals with grocery_CRUD validation of the unique constraint - taking into account either editing existing or adding new equip_items.

function unique_equip_item_check($str, $edited_equip_item_id){
$var = $this->Equip_Item_model->is_unique_except($edited_equip_item_id,$this->input->post('site_id'),$this->input->post('equip_type_id'),$this->input->post('name'));

if ($var == FALSE) {
$s = 'You already have an equipment item of this type with this name.';
$this->form_validation->set_message('unique_equip_item_check', $s);
return FALSE;
} else {
return TRUE;

I have the site_id and equip_type_id set as hidden fields as I do not want the user to change these - no problem.

$crud->field_type('site_id', 'hidden', $site_id);
$crud->field_type('equip_status_id', 'hidden', iQS_EqStatus_InUse);

When users add an equip_item I want them to be able to select the equip_type from a list of types - No problem, this is default grocery_CRUD behaviour.

$crud->add_fields('equip_status_id', 'site_id', 'equip_type_id', 'name');

When users edit an equip_item I don't want the user to be able to edit the equip_type. I figured no problem I can just set the edit_fields to exclude equip_type_id:

$crud->edit_fields('equip_status_id', 'site_id', 'name', 'barcode_no');

[b]BUT[/b] this plays havoc with my validation callback because the value of the equip_type_id field is nowhere on the edit form and it is obviously needed by my validation routine.

So I need to have the equip_type_id field visible and acting normal when adding a new record but hidden when editing a record.

I've tried this hack of all hacks <yuck!>:

if ($this->uri->segment(4)!= FALSE){$crud->field_type('equip_type_id', 'hidden');}

My theory was that "$this->uri->segment(4)" only give a false result if adding a new record, but it does not work.

Any suggestions?

  • profile picture
  • Member

Posted 29 November 2012 - 05:31 AM

Oh I have also tried:

$crud->callback_edit_field('equip_type_id', array($this,'edit_equip_type_field_callback'));


function edit_equip_type_field_callback($value = '', $primary_key = null){
return '<input type="hidden" value="'.$value.'" name="equip_type_id"';

But that doesn't work it just screws up the layout of the form fields - the 'Type' label etc is still visible.

  • profile picture
  • Member

Posted 06 December 2012 - 06:36 AM

I'm still struggling with this... any ideas?

  • profile picture
  • Member

Posted 06 December 2012 - 08:18 AM

Hi kenilik !
I think http://www.grocerycrud.com/documentation/options_functions/getState link solve your problem.

  • profile picture
  • Member

Posted 07 December 2012 - 02:36 AM

Thanks, If anyone else has similar problem:

$crud = new grocery_CRUD();
$crud->edit_fields('equip_status_id', 'site_id', 'equip_type_id', 'name', 'barcode_no');

if ($crud->getState() == 'edit') {

$crud->field_type('equip_type_id', 'hidden');

It never ceases to amaze me just how full of awesomeness grocery_CRUD is!

Abid Rizvi
  • profile picture
  • Member

Posted 06 July 2015 - 05:28 AM

$crud->field_type('equip_type_id', 'hidden');