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 😍

Tutorial - Basic Methods (continue)

<< Tutorial - Basic Methods

After some times using grocery CRUD you will realize that you need more. For example if you want to have different fields at the add form and different in the edit form you can easily use:

add_fields and edit_fields

The add_fields and edit_fields it is really easy to use it as you already have using it ... without knowing it! What I mean? The method fields is nothing more than a shortcut and an alias to add_fields and edit_fields. For example if we have:


Is equal to:

Are you tired of copy-pasting all the time the most common functions that you are using such as setTable or setSubject and new Grocery_CRUD ? See how you can prevent this with a simple quick video on how to re-use Grocery CRUD common methods

It is very useful when we need different fields at add and different fields at edit form, for example let's say that the password field is not needed to the edit form we can simply do:

Did you know that in every function of grocery CRUD that you can handle unlimited fields with a comma seperated such as columns, fields, add_fields, unset_fields... e.t.c. you can also have an array there? For example the
$crud->add_fields('username','password','first_name','last_name') can also be written as


Very important and useful method to add required fields. You can simply add your required fields with this simple syntax:


With this line of code you will have your field name with a red asterisk (that means that the field is required) and when the user press the button of save without having complete all the required fields it will appear a message like this:

required fields grocery CRUD


The set_rules method works exactly with the same way as codeigniter's set_rules method of form_validation library. The only difference is that you will add the set_rules at your grocery_CRUD object. For example:

$crud->set_rules('quantityInStock','Quantity In Stock','integer');

For more you can read the set_rules codeigniter documentation. The cool thing about grocery CRUD is that only with this line of code you will have all the validation messages when a user submits an invalid form.

unset_add, unset_edit,unset_delete and unset_list

You can simply unset any operation you like with using those methods. So for example if you add the line:

You will see that the edit button is missing like the below image:

unset edit image

The methods unset_add, unset_edit,unset_delete and unset_list doesn't just unset the button. There are also intelligent enough to understand that even if you add with the hard way the url (eg. /examples/customers_management/edit/103 ) You will have an error with message: "You don't have permissions for this operation" . As it is an exception and not just a show_error you can handle it with your way.

For more about those methods you can read the documentation API for functions unset_add, unset_edit, unset_delete and unset_operations


It simply unsets the texteditor from the fields that you had specified with the below syntax:


For example:

    function films_without_editor()
        $crud = new grocery_CRUD();
        $crud->fields('title', 'description', 'release_year', 'rental_duration', 'rental_rate', 'length', 'replacement_cost', 'rating', 'special_features');
        $output = $crud->render();

You can also check the live example below to see that the texteditor has been disappeared at the field description when you are adding or editing

Note: The below example is an iframe so it might appeared with a scroll bar. If you like you can view the example on a new tab