⚠ 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

A Label/Uneditable Field



Sparkle

Sparkle
  • profile picture
  • Member

Posted 13 April 2012 - 04:05 AM

If you would like to have an uneditable field (or a field value that you set through code using callback_before_insert) - let us say the timestamp of when an entry in the crud has been added - and you would like to have it included in the add/edit form, you can do the following:

In the 'grocery_CRUD_Field_Types' class under function 'get_field_input', you can a case as follows:


case 'label':
$field_info->input = $this->get_label($field_info,$value);
break;


Then under the class 'grocery_CRUD_Layout' add the following function:


protected function get_label($field_info,$value)
{
$value = !is_string($value) || $value=='' ? 'N/A' : $value;
$input = "<span id='{$field_info->name}'>$value </span>";
return $input;
}



Now in your code you can do something like:


$crud->change_field_type('dateCreated', 'label');



And it would look like this:
[sharedmedia=core:attachments:102]

It would display N/A when blank as in the add screen.

@web-johnny I had troubles extending the library myself, so couldn't include the proper way of doing it.

Hope this might come in handy!

web-johnny

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

Posted 14 April 2012 - 10:31 AM

Great, thanks [member='Sparkle'] for this, it will help for sure many people.
I cannot understand why so much people ask me for this and it is a simple callback:

$crud->callback_edit_field('your_field',function($value= ''){
return $value;
});

jcasanova

jcasanova
  • profile picture
  • Member

Posted 16 April 2012 - 15:11 PM

excelent! I'll test it,

@web-johnny, I think the reason why not using the callback, because the user can still play with the input value, and that's not a good idea, even if the value inserted doesn't work at the end.

web-johnny

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

Posted 16 April 2012 - 19:32 PM

Just to inform you there will be a "readonly" field type at the next version 1.2.1 that do exactly what [member='Sparkle']'s example do. The only difference is that it is a bit more safety as it also checks if someone adds an insert or update input by hack. The syntax will be simple:


$crud->change_field_type('dateCreated', 'readonly');

HumbleMonk

HumbleMonk
  • profile picture
  • Member

Posted 18 April 2012 - 16:13 PM

@web-johnny: Do you have a timeline/estimated date when 1.2.1 will be released? I need to use this feature and I'm just wondering if I should wait for the official feature or use Sparkle's example.

Huge thanks for GroceryCRUD - it is awesome! With just a couple of lines of code I can get 90% of what I need (of course it's the other 10% that I have to figure out how to implement, things like this read-only field, so expect many questions from me ;) ).

web-johnny

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

Posted 18 April 2012 - 18:16 PM

version 1.2.1 is released yesterday. Enjoy :)

HumbleMonk

HumbleMonk
  • profile picture
  • Member

Posted 18 April 2012 - 23:23 PM

That's too awesome :D

Works perfectly.