Jump to content


[ANSWERED] Using unset_fields with unset_add_fields and unset_edit_fields


  • Please log in to reply
1 reply to this topic

#1 HumbleMonk

HumbleMonk

    Member

  • Members
  • PipPip
  • 25 posts

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.

#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,143 posts
  • LocationLondon

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);

Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users