[eluser]BrianDHall[/eluser]
Say Mr O-to-the-Z, I'd like your opinion on my technique here.
I wanted to use the htmlform to automatically build forms for me, but I wanted a simple option where I could blacklist certain fields for use in some forms. For instance in a "create new xyx" form I would not want the id, updated, or created fields to show for any model. I also wanted to be lazy and not to have to define what fields I want to show - just define what fields I DO NOT want to show.
So I implemented this as a custom function in my model:
Code:
var $forbidden_form_fields = array('id', 'created', 'updated');
function public_form_fields()
{
$public_fields = $this->fields;
foreach ($public_fields as $field_key => $field_value)
{
if (in_array($field_value, $this->forbidden_form_fields))
{
unset($public_fields[$field_key]);
}
}
Aaaaand...done! Only non-blacklisted fields will be shown, so if you add a new field like "secondary contact phone number" it will automagically show up on existing forms unless you choose to have it blacklisted.
If you want to dynamically add blacklisted fields you can do that, too, such as:
Code:
// Hide email and phone number
$model->forbidden_form_fields[] = array('email', 'phone');
// Whitelist 'id' for editing, but keep other fields hidden
if ($model->forbidden_form_fields['id'])
{
unset($model->forbidden_form_fields['id']);
}
Am I missing a built-in function or anything?
It works really great, I'm manually putting them in a few models for now and if goes well I'll probably rip out and put into an extension instead.