In case you've missed it, you are looking at an older version of the website. Checkout our latest version, we promise you will love it 😍

field_type

void field_type( string $field , string $field_type [ , string $value ] )
Quick Description: Just an alias to the change_field_type method.
Available for version >= 1.2.3
Changes the default field type.
The field type is a string and can take the following options:

Note: The third parameter ($value) only works for the hidden, enum and set field and it is not a default value for all the other types.

Hidden field

An example of how to use hidden field:
 
function hidden_test($office_id = 0)
{
    $crud = new grocery_CRUD();
 
    $crud->set_table('customers');
    $crud->columns('customerName','contactLastName','phone','city','country','salesRepEmployeeNumber','creditLimit');
    $crud->display_as('salesRepEmployeeNumber','from Employeer');
    $crud->set_subject('Customer');
    $crud->set_relation('salesRepEmployeeNumber','employees','lastName');
    $crud->add_fields('customerName','contactLastName','phone','city','country','salesRepEmployeeNumber','creditLimit','office_id');
    $crud->edit_fields('customerName','contactLastName','phone','city','country','salesRepEmployeeNumber','creditLimit');
 
    $crud->field_type('office_id', 'hidden', $office_id);
 
    $output = $crud->render();
 
    $this->_example_output($output);
}    
 

With this code you can see that office_id will disappear from the add and edit form. The office_id will be a hidden field with value of $office_id . You can of course add a static value, for example:
 
$crud->field_type('office_id', 'hidden', 3);
 
As you can see the office_id is ONLY in the $crud->add_fields and not $crud->edit_fields. This is just to understand that even if we write the field_type method , you must add it to your fields (add_fields / edit_fields). The most common is to use the method fields that is for add form and edit form. In this case it would be:
 
$crud->fields('customerName','contactLastName','phone','city','country','salesRepEmployeeNumber','creditLimit','office_id');
 

Invisible Field

Many people are confused about how and why to use "invisible" fields. So for example if you have:

 
$crud->fields('field1','field2','field3');
$crud->callback_before_insert(array($this,'test_callback'));
 
and :
 
function test_callback($post_array){
    $post_array['field4'] = 'test';
    return $post_array;
}
 
This will NOT work as expected unless you add the invisible field so you have to do it like this:
 
$crud->fields('field1','field2','field3','field4');
$crud->field_type('field4','invisible');
$crud->callback_before_insert(array($this,'test_callback'));
 

So this WILL WORK as you expected and without showing the field "field4" in the form. The "invisible" field type is created for security reasons

Still confused about invisible field? Then maybe this forum topic will help you : how to use invisible field


Password field

The password field just transforms the input type name to input type password, nothing more. To use it, you just add this line of code:

$crud->field_type('field_name', 'password');

and your input appears to the add and edit form as type = password

Below we can see a full example for using the password field for encrypt and decrypting password:

public function users(){
    $crud = new grocery_CRUD();
 
    $crud->set_table('db_user');
    $crud->set_subject('User');
    $crud->required_fields('user_name');          
    $crud->columns('user_name','email','real_name','active', 'groups');
    $crud->fields('user_name','email','password','real_name','active', 'groups');
 
    $crud->field_type('password', 'password');
 
    $crud->callback_before_insert(array($this,'encrypt_password_callback'));
    $crud->callback_before_update(array($this,'encrypt_password_callback'));
    $crud->callback_edit_field('password',array($this,'decrypt_password_callback'));
 
    $output = $crud->render();
    $this->_example_output($output);
}
 
function encrypt_password_callback($post_array, $primary_key = null)
{
    $this->load->library('encrypt');
 
    $key = 'super-secret-key';
    $post_array['password'] = $this->encrypt->encode($post_array['password'], $key);
    return $post_array;
}
 
function decrypt_password_callback($value)
{
    $this->load->library('encrypt');
 
    $key = 'super-secret-key';
    $decrypted_password = $this->encrypt->decode($value, $key);
    return "<input type='password' name='password' value='$decrypted_password' />";
}

Enum field

Available for version >= 1.2.3
$crud->field_type('status','enum',array('active','private','spam','deleted'));

Set field

Available for version >= 1.2.3
$crud->field_type('fruits','set',array('banana','orange','apple','lemon'));
Available for version >= 1.3.2
$crud->field_type('status','dropdown',
            array('1' => 'active', '2' => 'private','3' => 'spam' , '4' => 'deleted'));

Multiselect field

Available for version >= 1.3.2
$crud->field_type('fruits','multiselect',
                                array( "1"  => "banana", "2" => "orange", "3" => "apple"));