I have a problem with trying to hide a field when editing an entry that must be visible when adding an entry.
My table structure is as follows:
equip_items
--------------
id (pk)
equip_type_id (fk to equip_types)
site_id (fk to sites)
equip_status_id (fk to equip_status)
name
(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');
BUT 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 :
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.
I have also tried:
$crud->callback_edit_field('equip_type_id', array($this,'edit_equip_type_field_callback'));
with
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.
Any suggestions?
I think the problem is that you have to add the field equip_status_id to the edit fields.
So in your case this will simply solve your problem:
$crud->edit_fields('equip_status_id', 'site_id',
'name', 'barcode_no','equip_status_id');
or
$crud->fields('equip_status_id', 'site_id', 'name',
'barcode_no','equip_status_id'); //for both add and edit form
of course you have to also use your hack:
if ($this->uri->segment(4)!= FALSE) {
$crud->field_type('equip_type_id', 'hidden');
}
or you can do it with a more proper way:
$crud = new grocery_CRUD();
if ($crud->getState() == 'edit') {
$crud->field_type('equip_type_id', 'hidden');
}
...
With this way you will also remember why you did this hacking to the code.