Jump to content

Custom edit/add/read/etc form

form modal dialog

  • Please log in to reply
No replies to this topic

#1 genya



  • Members
  • Pip
  • 3 posts

Posted 03 June 2019 - 02:44 PM

Hey everyone! Thank you Johnny for this amazing GroceryCrud!


I would like to share an extension to the library, I attached the file named Grocery_CRUD_mod.php. Please be aware I am still using library 1.6.1, and do not know how it would transfer to other versions or the latest version 1.6.3.


This extension allows for rendering a completely custom form when clicking on your add_action button. It works by adding a custom state and then you can route the add_action url to that state, and have a validation url function and a final database insert/update/etc url function to the state. You can also add a callback function to the add_state that will pass additional data to the form before rendering it.



	public function update_validate($pri_key=null)
		$post_data = $this->input->post();
		$validation_result = (object)array('success'=>false);
		$validation_result->error_message = "yo yo error msg";
		$validation_result->success_message = "yo yo success msg <br /><br /><br />";
		$validation_result->error_fields = array('field1' => 'field1 error', 'field2' => 'field2 error');

		echo json_encode($validation_result);
	public function additional_data($primary_key)
		$output = array();
		$output['query_results'] = $this->db->query('SOME QUERY')->result_array();
		$output['another_variable'] = "additional data here";
		return $output;
	public function table()
 		$crud = new grocery_CRUD_mod();
		// make sure the state_name 'custom_edit' and the url for the add_action are the same 'my_controller/table/custom_edit'
		$crud->add_state('custom_edit', 'custom_edit_form.php', 'update_validate', NULL, array($this, 'additional_data'));
		$crud->add_action('My Custom Edit', '', 'my_controller/table/custom_edit', 'ui-icon-plus custom-action'); 
                // custom-action css class is for modal dialog box	
		$crud->columns('col1', 'col2', 'col3');
		$output = $crud->render();

		$this->load->view('my_view.php', $output);


(I am using flexigrid theme. This file does have to be in assets/grocery_crud/themes/'theme you are using'/views/)





<div class="flexigrid crud-form" style='width: 100%;' data-unique-hash="<?php echo $unique_hash; ?>">
    <div class="mDiv">
        <div class="ftitle">
            <div class='ftitle-left'>
                <?php echo $this->l('form_edit'); ?> <?php echo $subject?>
            <div class='clear'></div>
        <div title="<?php echo $this->l('minimize_maximize');?>" class="ptogtitle">
<div id='main-table-box'>

    <div class='form-div'>
    <?php echo form_open( 'my_controller/update_validate/'.$primary_key, 'method="post" id="crudForm"  enctype="multipart/form-data"'); ?>
            my custom form!

            Some of my additional vars from additional_data function callback

            echo $additional_data['another_variable'];


        <?php if ($is_ajax) { ?><input type="hidden" name="is_ajax" value="true" /><?php }?>
        <div id='report-error' class='report-div error'></div>
        <div id='report-success' class='report-div success'></div>
    <div class="pDiv">
        <div class='form-button-box'>
            <input  id="form-button-save" type='submit' value='<?php echo $this->l('form_update_changes'); ?>' class="btn btn-large"/>
<?php     if(!$this->unset_back_to_list) { ?>
        <div class='form-button-box'>
            <input type='button' value='<?php echo $this->l('form_update_and_go_back'); ?>' id="save-and-go-back-button" class="btn btn-large"/>
        <div class='form-button-box'>
            <input type='button' value='<?php echo $this->l('form_cancel'); ?>' class="btn btn-large" id="cancel-button" />
<?php     } ?>
        <div class='form-button-box'>
            <div class='small-loading' id='FormLoading'><?php echo $this->l('form_update_loading'); ?></div>
        <div class='clear'></div>
    <?php echo form_close(); ?>
<script type="text/javascript">    
    var validation_url = '<?php
        $my_controller_pos = strpos($list_url, 'my_controller');
        $my_controller_len = strlen('my_controller');
        $base_path = substr($list_url, 0, ($my_controller_pos + $my_controller_len));
        echo $base_path . '/update_validate/' . $primary_key;
    var list_url = '<?php echo $list_url?>';

    var message_alert_edit_form = "<?php echo $this->l('alert_edit_form')?>";
    var message_update_error = "<?php echo $this->l('update_error')?>";

The js validation_url variable needs to be taken from the php variables and passed to the view, that is a future improvement.



Also the 'custom-action' css class is for rendering a modal dialog box. I am not sure how to properly 'extend' to not modify the javascript files, but if you need the modal dialog box to pop up for your custom actions

in assets/grocery_crud/js/common/list.js these changes have to be made:






I hope this helps, and please give me feedback if possible.

Attached Files

Also tagged with one or more of these keywords: form, modal, dialog

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users