field_type
void field_type( string $field , string $field_type [ , string $value ] )
The field type is a string and can take the following options:
- hidden
- invisible
- password
- enum
- set
- dropdown
- multiselect
- integer
- true_false
- string
- text
- date
- datetime
- readonly
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);
$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'));
function test_callback($post_array){ $post_array['field4'] = 'test'; return $post_array; }
$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
$crud->field_type('status','enum',array('active','private','spam','deleted'));
Set field
$crud->field_type('fruits','set',array('banana','orange','apple','lemon'));
Dropdown field
$crud->field_type('status','dropdown', array('1' => 'active', '2' => 'private','3' => 'spam' , '4' => 'deleted'));
Multiselect field
$crud->field_type('fruits','multiselect', array( "1" => "banana", "2" => "orange", "3" => "apple"));