⚠ 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

make fields readonly on edit



w0w

w0w
  • profile picture
  • Member

Posted 01 August 2013 - 09:45 AM

instead of making callback_edit_field on each fields that i want to be readonly

is there another way to make my specified fields make readonly but for edit page only not in add page?

 

 

 

thanks


davidoster

davidoster
  • profile picture
  • Member

Posted 01 August 2013 - 10:01 AM

Hello and welcome to the forums [member=w0w].

First of all, there is a function that is called field_type and you can use it like this,

$crud->field_type('office_id', 'readonly');

 

And if you combine this with something like this on your controller's function,

$state = $crud->getState();
    $state_info = $crud->getStateInfo();
 
    if($state == 'add')
    {
        //Do your cool stuff here . You don't need any State info you are in add
    }
    elseif($state == 'edit')
    {
        $primary_key = $state_info->primary_key;
        //Do your awesome coding here. 
    }
    else
    {
        $this->_example_output($output);
    }

you'll get the functionality you want.


w0w

w0w
  • profile picture
  • Member

Posted 01 August 2013 - 10:10 AM

Thank you!


davidoster

davidoster
  • profile picture
  • Member

Posted 01 August 2013 - 12:24 PM

I am afraid the above proposed solution will not work.

Sorry about this.

I think the only other possible solution is by using the callbacks.


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 01 August 2013 - 13:37 PM

why you think the above solution wont work,.. i have been using it the same way .. it works for me..


davidoster

davidoster
  • profile picture
  • Member

Posted 01 August 2013 - 22:45 PM

why you think the above solution wont work,.. i have been using it the same way .. it works for me..

 

If you put the field_type within the if($state == 'edit') you have already rendered the output of the form.

How do you rerender the form?

Unless you do it another way that I can't think of.


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 02 August 2013 - 06:10 AM

Hi David,

 

Think you got confused.. the state info is based on the action associated with the call... it is not decided post the generation of the form.. I am sharing you a live working example that i work along with and it surely works.

function users()
	{
		$this->load->helper('user');
		$crud = new grocery_CRUD();
		$crud->set_table('users');
	
		$crud->set_subject('User');
		$crud->unset_read();
	
		//Set the columns for display
		$crud->columns('username','email', 'active', 'first_name', 'last_name');
		
		$crud->add_action('Change Password', '', $this->config->item('base_url') . 'auth/change_password/','','', '_blank');
	
		//Set fields for add
		$crud->add_fields('username','password', 'confirm_password', 'email', 'first_name', 'last_name', 'user_type');
	
		//Set fields for edit
		$crud->edit_fields('username', 'email', 'first_name', 'last_name', 'user_type');
	
		//Set display as for fields
		$crud->display_as("username", "Login Name");
	
		$currentState = $crud->getState();
		if($currentState == 'add') {
			$crud->change_field_type('password', 'password');
			$crud->change_field_type('confirm_password', 'password');

			//Set the rules
			$crud->set_rules('username', 'Login Name', 'trim|required|alphanumeric');
			$crud->set_rules('password', 'Password', 'trim|required|matches[confirm_password]|md5');
			$crud->set_rules('confirm_password', 'Confirm Password', 'trim|required');
			$crud->set_rules('confirm_password', 'Confirm Password', 'trim|email');
			
			$level = getUserLevel();
			if($level == 'Client Admin') {
				$crud->change_field_type('user_type', 'hidden', 'Inspector');
			}
		}
	
		//Set the callback escape insert call
		$crud->callback_insert('register_new_user');
	
		//Render the stuff
		$output = $crud->render();
		$data = array();
		$data['current_view'] = 'Users';
		$output->data=$data;		
		$this->load->view('crud',$output);
	}

Here if you notice .. i have made some alterations in the code to be available if the state is add and the same effect is not expected in case if it is edit. And this works like a charm.

 

/topic/887-force-default-edit-or-delete-state/  even here - the code that i shared.. before i render the output of the crud, i simply check what state the call is in and based on it, the decision to render the form with desired changes are taken care of.

 

Hope i made this clear :)


davidoster

davidoster
  • profile picture
  • Member

Posted 02 August 2013 - 07:48 AM

Hi [member=amit shah]. Well I must have been very confused since I have a very similar algorithm of my own that works as yours and this is why I said in the first place that you can do it like this. But then I thought, let me check the code that is posted as an example to the getState function.

 

Well this is the example code of the getState.

function employees_management()
{
    $crud = new grocery_CRUD();
 
    $crud->set_theme('datatables');
    $crud->set_table('employees');
    $crud->set_relation('officeCode','offices','city');
    $crud->display_as('officeCode','Office City');
    $crud->set_subject('Employee');
 
    $output = $crud->render();
 
      $state = $crud->getState();
    $state_info = $crud->getStateInfo();
 
    if($state == 'add')
    {
        //Do your cool stuff here . You don't need any State info you are in add
    }
    elseif($state == 'edit')
    {
        $primary_key = $state_info->primary_key;
        //Do your awesome coding here. 
    }
    else
    {
        $this->_example_output($output);
    }
}

Try to make it work like this with the render function called before the getState !!!

Well it doesn't work with this way of having the render function before the getState and hence any code that you might have within an if statement.

 

All this got me seriously confused as you can understand.

But it is interesting to note the way the example is written and the other way that can be written in order to accommodate the functionality that [member=w0w] was asking.


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 02 August 2013 - 08:49 AM

Well i totally agree with you David, that post rendering you cannot.. but this state can be achieved prior to rendering of the form. What his requirement were...

 

instead of making callback_edit_field on each fields that i want to be readonly

is there another way to make my specified fields make readonly but for edit page only not in add page?

 

If you notice .. he have a scenario that he wants to make a few fields as readonly when he is in edit mode / state. So the above code / pattern can surely be used.. where if, in state of edit, he can change the field type of all the respective fields as read only. It surely should workout.


davidoster

davidoster
  • profile picture
  • Member

Posted 02 August 2013 - 09:19 AM

Well apparently we can use the getState both ways actually!

 

Version 1. Render before the getState.

This is used when you want to change the way you output, e.g. if we look at the latest example of version 1.4 for the multigrids first we render and then we use the getState,

public function offices_management2()
	{
		$crud = new grocery_CRUD();
		$crud->set_table('offices');
		$crud->set_subject('Office');

		$crud->set_crud_url_path(site_url(strtolower(__CLASS__."/".__FUNCTION__)),site_url(strtolower(__CLASS__."/multigrids")));

		$output = $crud->render();

		if($crud->getState() != 'list') {
			$this->_example_output($output);
		} else {
			return $output;
		}
	}

Version 2. Render after the getState.

This is used when you want to do special manipulation in fields or other special formatting and logic for your ouptut and then render the example,

e.g. the case you need to have some fields changed let's say using field_type as above, or when you want to have very specific rulesets. Then you use a code similar to this,

public function om()
	{
		try{
			$crud = new 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');

			$state = $crud->getState();
			$state_info = $crud->getStateInfo();
 
			if($state == 'edit')
			{
				$primary_key = $state_info->primary_key;
				$crud->field_type('phone','readonly');
			}
			$output = $crud->render();
			$this->_example_output($output);

		}catch(Exception $e){
			show_error($e->getMessage().' --- '.$e->getTraceAsString());
		}
	}

OK. Now we know what we do!

But I need to ask [member=web-johnny] why this is happening! I believe he didn't plan for this dual way of handling stuff.

To be honest with you, I am not sure why Version 2 does work but it does!


web-johnny

web-johnny
  • profile picture
  • Administrator
  • 1,166 posts

Posted 05 August 2013 - 18:57 PM

OK. Now we know what we do!

But I need to ask [member=web-johnny] why this is happening! I believe he didn't plan for this dual way of handling stuff.

To be honest with you, I am not sure why Version 2 does work but it does!

 

Hello  [member='davidoster']

 

Actually the reason that this is happening is because the field_type was introduced later. It was actually to easily change the field_type of the default database field. For example: a string field to be a dropdown field ... e.t.c. . As it was designed a bit fast I didn't even thought about it. I haven't thought that perhaps people will need to have different type for add and different type for edit. At the future yes it would be better to have:

$crud->field_type()

$crud->add_field_type()

$crud->edit_field_type()

I think this will solve your problem. For now the best solution is to use the:

$state = $crud->getState();

as   [member='davidoster'] mentioned as it is the only way to do it.

 

Also one more disadvantage of the field_type is that you can only have ONE field_type . For example, people was trying to do something like this:

$crud->field_type('test','dropdown',...);
$crud->field_type('test','readonly',...);

and they believe that it will actually be a disabled dropdown list. This is not happening but perhaps at the future I will implement this kind of functionality

 

Cheers


Diego Matos

Diego Matos
  • profile picture
  • Member

Posted 06 November 2015 - 04:52 AM

Hello  [member='davidoster']

 

Actually the reason that this is happening is because the field_type was introduced later. It was actually to easily change the field_type of the default database field. For example: a string field to be a dropdown field ... e.t.c. . As it was designed a bit fast I didn't even thought about it. I haven't thought that perhaps people will need to have different type for add and different type for edit. At the future yes it would be better to have:

$crud->field_type()

$crud->add_field_type()

$crud->edit_field_type()

I think this will solve your problem. For now the best solution is to use the:

$state = $crud->getState();

as   [member='davidoster'] mentioned as it is the only way to do it.

 

Also one more disadvantage of the field_type is that you can only have ONE field_type . For example, people was trying to do something like this:

$crud->field_type('test','dropdown',...);
$crud->field_type('test','readonly',...);

and they believe that it will actually be a disabled dropdown list. This is not happening but perhaps at the future I will implement this kind of functionality

 

Cheers

 

"at the future I will implement this kind of functionality"

 

Sorry , at this time 2015  (ci v3=>) , have you implemented this functionality? i was trying to make it work . greetings


seth

seth
  • profile picture
  • Member

Posted 30 March 2017 - 00:28 AM

Thanks it worked perfectly as i wanted, by the way I would like to add some spacing in the field / callback