⚠ 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

[ANSWERED] Using unset_fields with unset_add_fields and unset_edit_fields



HumbleMonk

HumbleMonk
  • profile picture
  • Member

Posted 19 April 2012 - 04:04 AM

There is a bug/unexpected behaviour when using unset_fields at the same time as using unset_add_fields or unset_edit_fields.

For instance, I want the 'name' field to not be in any fields and I want the 'age' field to be in the edit field but not the add field:

$crud->unset_fields('name');
$crud->unset_add_fields('age');


The actual result of this code is that 'name' is in both add and edit forms and age is not in the add forms (in other words, the first line of code is ignored).

If we switch the order the reverse happens:

$crud->unset_add_fields('age');
$crud->unset_fields('name');


'age' will be in the add form but 'name' will not be in any form (again, the first line of code is being ignored).

The workaround is to just not use unset_fields with unset_add_fields and unset_edit_fields and instead use unset_add_fields and unset_edit_fields to explicitly remove a field from both.

However, to me this is unexpected behaviour and the above code should actually work.

web-johnny

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

Posted 19 April 2012 - 06:07 AM

This is the expected behaviour actually. I know that I still haven't documented so it's ok to not understand how it works.

The way it works it is really easy so let me explain


$crud->unset_fields('name');


it is just a shortcut to:


$crud->unset_add_fields('name');
$crud->unset_edit_fields('name');


nothing more, nothing more complicated than that. So when you have


$crud->unset_fields('name');
$crud->unset_add_fields('age');


it is like you have:

$crud->unset_add_fields('name');
$crud->unset_edit_fields('name');
$crud->unset_add_fields('age');


and grocery CRUD just add the last one, don't add more. So you can NOT do something like this:


$crud->unset_add_fields('name');
$crud->unset_add_fields('age');
$crud->unset_add_fields('lastname');


it will just unset the "lastname".

However you can work with arrays, so in your case you can simply do:


$unset_fields = array('name');
$crud->unset_edit_fields($unset_fields);
$unset_fields[] = "age";
$crud->unset_add_fields($unset_fields);