Editing just one record in Grocerycrud v2

28 Views Asked by At

I’m porting a grocerycrud 1.x app to version 2.x and I can’t find an equivalent for unset_list and unset_back_to_list functions that were in version 1. How is this done in version 2?

Basically what I’m trying to accomplish is to display the edit page for one specific record without the option of returning to the list of all records, or ever displaying that list. In the old version, you could call unset_list() and unset_return_to_list and use a URI like /controllername/edit/primarykey_id to open an edit page for just the record specified. Has anyone done this in version 2? If so, how?

1

There are 1 best solutions below

0
Chris Nadovich On

I looked into this pretty thoroughly and it appears that there's no direct support for editing just one record in GC2. Some workarounds are possible.

One is to simply use a where clause to restrict the list to just one record. Sort-of a preview read-only view. The user then needs to click edit to change anything. Not quite as good as going directly to the edit page, but it’s OK in some cases.

Another workaround is to filter the $output of render() to remove the buttons, maybe like this

 $output->output = str_replace("value='Update changes'", "value='Save'", $output->output);
 $output->output = str_replace("value='Update and go back to list'", "value='Save and Return'", $output->output);

and then use logic in the controller and CodeIgniter 4 routes to jail into edit form and make sure the record id is correct. Some typical logic would be

    $state = $crud->getState();
    $state_info = $crud->getStateInfo();
    switch ($state) {
        case 'edit':
        case 'update_validation':
        case 'update':
        case 'success':
            $primary_key = $state_info->primary_key;
            $member_id = $this->session->get('user_id');
            if ($primary_key != $member_id) {
                trigger_error("ID Mismatch: pk=$primary_key mi=$member_id",E_USER_ERROR);
            }
            break;
        default:
            trigger_error("This function can only be called in states edit/update_validation/success. State=$state", E_USER_ERROR);
            break;
    }

I’ve used both these techniques to port my v1.x forms that use unset_list()