⚠ 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

Remove field on read state



archerwisdom

archerwisdom
  • profile picture
  • Member

Posted 17 November 2013 - 10:29 AM

Hi,

 

I have the following crud. Can I remove 'function_time_selector' field on the READ state ? It is because I get this error message if I leave the function_time_selector on the READ page.

 

Error Massage

 

A PHP Error was encountered

Severity: Warning

Message: in_array() expects parameter 2 to be array, null given

Filename: libraries/Grocery_CRUD.php

Line Number: 161

 

 

 

CRUD code:

$crud->fields('function_address','contact_person','function_time', 'function_time_selector', 'function_purpose');


$crud->callback_field('function_time_selector',array($this,'functime_callback'));

function functime_callback($value = '', $primary_key = null)
    {
            $time_value = substr($value, 0, -2);
            $time_ampm = substr($value, -3);
            
            
            $functime = "";
            
            /*get the time value*/
            $functime  = '<select id="field-function_time_1" name="function_time_1">';
            for($i = 1.00; $i <= 12.00; $i++){
                
                $functime .= '<option value="'.$i.'" '.($time_value == $i ? "selected" : "").'>'.number_format($i, 2, '.', '').'</option>';
                
            }
            $functime .= '</select>';
            
            /*get the time AM/PM*/
            $functime .= '<select id="field-function_time_2" name="function_time_2">';
            $functime .= '<option value=" AM" '.($time_ampm == ' AM' ? "selected" : "").'>AM</option>';
            $functime .= '<option value=" PM" '.($time_ampm == ' PM' ? "selected" : "").'>PM</option>';
            $functime .= '</select>';

            return $functime;
        
        
    }

 

 

 


Robert

Robert
  • profile picture
  • Member

Posted 20 November 2013 - 14:31 PM

Yes, you can use  :
$crud->set_read_fields() to add only the fields and order you want
$crud->unset_read_fields() to remove the fields you want

manushimn

manushimn
  • profile picture
  • Member

Posted 12 May 2014 - 17:39 PM

@Robert

 

Where can I find the above two methods? I searched in the grocery-crud library, but I could not find.

I also faced the same problem which @archerwisdom  faced.

How can I solve it?

 

 


Paul Savostin

Paul Savostin
  • profile picture
  • Member

Posted 13 May 2014 - 13:22 PM

Hi! First of all why you want to disable field in read state?

1). You could just checking for state in your callback function and show different staff for read and no read state. For example:


In callback function check :

if(in_array('read',$this->uri->segment_array())){

            return         ->>>>>>>> // here just show value whatever you want for read state   

        } else {

          
            return   --------> // and here return input elements or select elements, whatever you want for edit and add state
        }

Is idea understood?


2) Here , look is that the same place of your warning like was mine? Then just add check in library Grocery Crud 155 line

 /** my change **/
                        if(!$this->required_fields){
                            $this->required_fields = array();
                        }
                    

//this is by grocery crud official

                        $field_info = (object)array(
                        'name' => $field_name,
                        'crud_type' => $this->change_field_type !== null && isset($this->change_field_type[$field_name]) ?
                                            $this->change_field_type[$field_name]->type :
                                            'string',
                        'display_as' => isset($this->display_as[$field_name]) ?
                                                $this->display_as[$field_name] :
                                                ucfirst(str_replace("_"," ",$field_name)),
   -> warning here!                     'required'    => in_array($field_name,$this->required_fields) ? true : false,  
                        'extras'    => $extras
                       );


And then tell me if thats resolve your problems:)

Have a nice day!


Paul Savostin

Paul Savostin
  • profile picture
  • Member

Posted 13 May 2014 - 13:24 PM

crap! post very old :lol: :lol: :lol: well hope my answer will help someone


larasmith

larasmith
  • profile picture
  • Member

Posted 30 August 2014 - 03:23 AM

I'm having the same problem... please elaborate further... Thank you!


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 04 September 2014 - 04:03 AM

well.. here let me share you the unset read functionality in case it is not present in your GC

under the class Grocery_CRUD

add 2 fields

protected $unset_read            = false;
protected $unset_read_fields    = null;

add a function below

public function unset_read_fields()
	{
		$args = func_get_args();

		if(isset($args[0]) && is_array($args[0]))
		{
			$args = $args[0];
		}

		$this->unset_read_fields = $args;

		return $this;
	}

i just give you my altered get_read_fields function

/**
	 *
	 * Enter description here ...
	 */
	protected function get_read_fields()
	{
		if($this->read_fields_checked === false)
		{
			$field_types = $this->get_field_types();
			if(!empty($this->read_fields))
			{
				foreach($this->read_fields as $field_num => $field)
				{
					if(isset($this->display_as[$field]))
						$this->read_fields[$field_num] = (object)array('field_name' => $field, 'display_as' => $this->display_as[$field]);
					else
						$this->read_fields[$field_num] = (object)array('field_name' => $field, 'display_as' => $field_types[$field]->display_as);
				}
			}
			else
			{
				$this->read_fields = array();
				foreach($field_types as $field)
				{
					//Check if an unset_read_field is initialize for this field name
					if($this->unset_read_fields !== null && is_array($this->unset_read_fields) && in_array($field->name,$this->unset_read_fields))
						continue;

					if(!isset($field->db_extra) || $field->db_extra != 'auto_increment')
					{
						if(isset($this->display_as[$field->name]))
							$this->read_fields[] = (object)array('field_name' => $field->name, 'display_as' => $this->display_as[$field->name]);
						else
							$this->read_fields[] = (object)array('field_name' => $field->name, 'display_as' => $field->display_as);
					}
				}
			}

			$this->read_fields_checked = true;
		}
		return $this->read_fields;
	}

just 1 more thing - i also share you my unset_fields altered function that u can use

public function unset_fields()
	{
		$args = func_get_args();

		if(isset($args[0]) && is_array($args[0]))
		{
			$args = $args[0];
		}

		$this->unset_add_fields = $args;
		$this->unset_edit_fields = $args;
		$this->unset_read_fields = $args;

		return $this;
	}

Hope this works for you

 

Happy GCing :)