⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

default rafio button value



superdan
  • profile picture
  • Member

Posted 13 August 2013 - 13:03 PM

Hi all

 

i have some radio buttons in my method

 

es.

isAdmin

yes /no

 

isMale

yes/no

 

isWorker

yes/no

 

 

i would like to add default values for each one only on add form.

 

 

at the moment i used a callback like this

public function SERVIZI()
        {
            
            $this->grocery_crud->set_table('SERVIZI');
            
            //callback
            $this->grocery_crud->callback_add_field('BL_PACCHETTO',array($this,"checkboxDefaultNo"));
           
            $output = $this->grocery_crud->render($this->user);
            $this->_example_output($output);        
        }

and a function like this

function checkboxDefaultNo()
    {
        return '<label><input  class="radio-uniform" type="radio" name="BL_PACCHETTO" value="1" />Si</label><label><span class="checked"><input class="radio-uniform" type="radio" name="BL_PACCHETTO" value="0" checked/>No</span></label>';
    }

But in this way i must use a function for each radio buttons set cause the name values must change ( i have tons )

it seems really too much code to write (values are always yes/no)!

 

is there a way to make it easy/smaller?

 

it would be wonderfull to set a call like this

 

$this->grocery_crud->defaultValue('"BL_PACCHETTO','1');

etc.

 

Is this impossibile?

Thanks a lot.


superdan
  • profile picture
  • Member

Posted 13 August 2013 - 14:51 PM

ok i found a solution

 

thanks to this post

 

/topic/61-default-field-values-for-add-form/

 

 

now i can set

 

//default value checkbox radio

$this->custom_grocery_crud->field_set_defaults('BL_PACCHETTO' , 'true_false', '0');

 

 

but also!!

$this->custom_grocery_crud->field_set_defaults('DT_DATAACQUISIZIONE' , 'date', date("Y-m-d"));

 

custom grocery crud class is a GREAT addon!


superdan
  • profile picture
  • Member

Posted 13 August 2013 - 15:48 PM

just the dropdown option seems wont work

 

i write

$this->custom_grocery_crud->field_set_defaults('IDNAZIONE' , 'dropdown', '1');

and the error is

Fatal error: Unsupported operand types in /var/www/........./crud/application/libraries/Grocery_CRUD.php on line 2313

heruprambadi
  • profile picture
  • Member

Posted 14 August 2013 - 02:33 AM

grocerycrud 1.4 has some new feature. you can use field_type like this :

$crud->field_type('fruits','true_false', array( "0" => "banana", "1" => "orange"));

superdan
  • profile picture
  • Member

Posted 14 August 2013 - 05:19 AM

hi

heruprambadi

thanks for your answer

thats true

 

BUT

 

in this way i loose ALL my old (database) values in the dropdown.

$this->custom_grocery_crud->field_set_defaults('IDNAZIONE' , 'dropdown', array( "0" => "banana", "1" => "orange"));

if i implement a code like yours all my dropdown, db values will disappear and i have only the array values.

That is not the right way, cause i want to have my database values in my dropdown,

i want only implement a default one, without changing the others.

 

So the problem persists


heruprambadi
  • profile picture
  • Member

Posted 14 August 2013 - 05:23 AM

oh, sorry. maybe it will help you.


heruprambadi
  • profile picture
  • Member

Posted 14 August 2013 - 05:26 AM

i think we have same problem here :D


superdan
  • profile picture
  • Member

Posted 14 August 2013 - 06:13 AM

i dont think its the same,

 

i think is an incompatibility between

 

custom_grocery_crud

 

and the new version of grocery crud so maybe

 

custom_grocery_crud

 

have to be fixed.

 

I will report it here so maybe someone can help

 

 

 

 

To use it - drop the subclass into the application/libraries folder

right beside grocery_crud.php

call the new file custom_grocery_crud.php

 

Edit application/controllers/examples.php

 

class Examples extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        
        $this
->load->database();
        $this->load->helper('url');
        
        
//$this->load->library('grocery_Crud');    
        $this->load->library('custom_grocery_crud');    
    
}

...

function offices_management()
    {
        try{
            //$crud = new grocery_CRUD();
            $crud = new custom_grocery_crud();

            $crud->set_theme('datatables');
            $crud->set_table('offices');
            $crud->set_subject('Office');
            $crud->required_fields('city');
            $crud->columns('city','country','phone','addressLine1','postalCode');

            //our demo
$crud->field_set_defaults('city' , 'readonly', 'Hobart,Tasmania');

            $output = $crud->render();
            
            $this
->_example_output($output);
            
        
}catch(Exception $e){
            show_error($e->getMessage().' --- '.$e->getTraceAsString());
        }
    }

 

 

 

 

 

 

use field_set_defaults exactly the same way you would use field_type

 

Here's the subclass attachicon.gifcustom_grocery_crud.php

 

and here it is inline for you to peruse:

 


<?php
/**
* PHP grocery CRUD
*
* A Codeigniter library that creates a CRUD automatically with just few lines of code.
*
* Copyright © 2010 - 2012 John Skoumbourdis.
*
* LICENSE
*
* Grocery CRUD is released with dual licensing, using the GPL v3 (license-gpl3.txt) and the MIT license (license-mit.txt).
* You don't have to do anything special to choose one license or the other and you don't have to notify anyone which license you are using.
* Please see the corresponding license file for details of these licenses.
* You are free to use, modify and distribute this software, but all copyright information must remain.
*
* @package     grocery CRUD
* @copyright     Copyright (c) 2010 through 2012, John Skoumbourdis
* @license     https://github.com/scoumbourdis/grocery-crud/blob/master/license-grocery-crud.txt
* @version     1.3
* @author     John Skoumbourdis <scoumbourdisj@gmail.com>
*/

// ------------------------------------------------------------------------
include_once(dirname(__FILE__)."/grocery_crud.php");


/**
* PHP grocery CRUD - Customized
*
* Creates a full functional CRUD with few lines of code.
*
* @package     grocery CRUD
* @author     John Skoumbourdis <scoumbourdisj@gmail.com>
* @license https://github.com/scoumbourdis/grocery-crud/blob/master/license-grocery-crud.txt
* @link        http://www.grocerycrud.com/documentation
*
* @hacker Ez (ezgoen@gmail.com) - email me for my real identity
*/

class custom_grocery_crud extends grocery_CRUD{

    protected $field_default_values = array();
    
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Helper Function
     *
     * Created by Ez (ezgoen@gmail.com) - email me for my real identity
     *
     */
    protected function get_field_default_values($field){
        return array_key_exists($field,$this->field_default_values) ? $this->field_default_values[$field] : null;
    }

    /**
     * Use this function exactly the same way you would use field_type()
     * see : http://www.grocerycrud.com/documentation/options_functions/field_type
     *
     * Created by Ez (ezgoen@gmail.com) - email me for my real identity
     *
     * @explanation:
     *                in the case of an ADD data form I want to provide default values to the fields.
     *                I may also want to make the fields with default values readonly or completely hidden
     *                The parent object does not allow/facilitate addidtion of default values to visible
     *                fields other than:
     *                    hidden
     *                    invisible
     *                    password
     *                    enum
     *                    set
     *                    dropdown
     *                    multiselect
     *
     *                I havent fooled around with invisible - so I cant comment on anything to do with that.
     *                however I want my other fields - most commonly string and integer to have default values
     *                additionally I may also want thos values to either editable OR readonly and visible at
     *                the same time AND I want my default values (if they are readonly) to go into the database
     *                
     */
    public function field_set_defaults($field,$type,$value){
         $this->field_default_values[$field]=array('type'=>$type,'value'=>$value);
    }


    /**
     * Work out what to do with input fields for add record
     *
     *
     * Modified/Hacked by Ez (ezgoen@gmail.com) - email me for my real identity
     *
     * @explanation:
     *                in the case of an ADD data form I want to provide default values to the fields.
     *                I may also want to make the fields with default values readonly or completely hidden
     */
    protected function get_add_input_fields($field_values = null)
    {
        $fields = $this->get_add_fields();
        $types     = $this->get_field_types();
        
        $input_fields = array();
        
        foreach($fields as $field_num => $field)
        {    
            $field_info = $types[$field->field_name];
            
            $field_value = !empty($field_values) && isset($field_values->{$field->field_name}) ? $field_values->{$field->field_name} : null;
    

            $field_default_values=$this->get_field_default_values($field->field_name);
            if (! empty($field_default_values) and empty($field_value) ){
                //if $field_info is not set at this point we need to construct one
                if (!isset($field_info)){
                        $field_info = (object)array();
                }
                $field_info->custom_default=True; //this is what we look for to ensure we only change the behaviour we want to
                $field_info->crud_type=$field_default_values['type'];
                $field_info->type=$field_default_values['type'];
                $field_info->default=$field_default_values['value'];
                $field_info->extras=$field_default_values['value'];
                $field_value=$field_default_values['value'];
            }
            
            //no changes below here
            if(!isset($this->callback_add_field[$field->field_name]))
            {
                $field_input = $this->get_field_input($field_info, $field_value);
            }
            else
            {
                $field_input = $field_info;
                $field_input->input = call_user_func($this->callback_add_field[$field->field_name], $field_value, null, $field_info);
            }
            
            switch ($field_info->crud_type) {
                case 'invisible':
                    unset($this->add_fields[$field_num]);
                    unset($fields[$field_num]);
                    continue;
                break;
                case 'hidden':
                    $this->add_hidden_fields[] = $field_input;
                    unset($this->add_fields[$field_num]);
                    unset($fields[$field_num]);
                    continue;
                break;
            }            
            
            $input_fields[$field->field_name] = $field_input;
        }
        
        return $input_fields;
    }
    
    /**
     * Get the html input for the specific field with the
     * current value
     *
     * @param object $field_info
     * @param string $value
     * @return object
     *
     * Modified/Hacked by Ez (ezgoen@gmail.com) - email me for my real identity
     *
     * @explanation:
     *                in the case of an ADD data form I want my readonly default values to go into
     *                the database.
     *                The default behaviour of 'readonly' fields is to not actually "be a form field"
     *                they are instead just text in a div like this :
     *                    <div id="field-YOUR_FIELD" class="readonly_label">Your Default Value</div>
     *                this modification adds a hidden form field that will put the default values you
     *                want into the database like this :
     *                    <div id="field-YOUR_FIELD" class="readonly_label">Your Default Value</div>
     *                    <input id='field-YOUR_FIELD' type='hidden' name='YOUR_FIELD' value='Your Default Value' />
     *
     */
    protected function get_field_input($field_info, $value = null)
    {
            $real_type = $field_info->crud_type;
            
            $types_array = array(
                    'integer',
                    'text',
                    'true_false',
                    'string',
                    'date',
                    'datetime',
                    'enum',
                    'set',
                    'relation',
                    'relation_n_n',
                    'upload_file',
                    'hidden',
                    'password',
                    'readonly',
                    'dropdown',
                    'multiselect'
            );
            if (in_array($real_type,$types_array)) {
                /* A quick way to go to an internal method of type $this->get_{type}_input .
                 * For example if the real type is integer then we will use the method
                 * $this->get_integer_input
                 * */
                $field_info->input = $this->{"get_".$real_type."_input"}($field_info,$value);
                //my addition here...
                if ((property_exists($field_info,'custom_default')) and ( $real_type=="readonly" )) {
                    $field_info->crud_type='hidden';
                    $field_info->type='hidden';
                    $field_info->input .= $this->{"get_hidden_input"}($field_info,$value);
                    $field_info->crud_type=$real_type;
                    $field_info->type=$real_type;
                }
                //no changes below here
            }
            else
            {
                $field_info->input = $this->get_string_input($field_info,$value);
            }
        
        return $field_info;
    }
}
    
?>