MyBB Community Forums

Full Version: Conditionally show form fields
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I am looking at ways to work with the template system here, without doing any HTML hardcoding in the plugin php.

I have come up against a bit of a dilemma in that I have the following scenario.

If the setting a, or b is set to 1 or 2, then I need to show a text field.
If it is set to 0 then I dont want the text field displayed.

I can do this in a round about way by hiding the text field by saying

if($settingA==1){
$show_field = "text";
} else {
$show_field = "hidden"
}

Whilst this will do what I want I would prefer to not have to hide a field and therefore not have people possibly tamper with the form and inject possible data. The result of which wouldn't be catastrophic and I am re-checking the settings before submitting the data to the database anyway. However this just seems untidy and ends up with data on the page that just does not need to be there.

The only other way that I can think to do this would be to either use conditionals in the template which I am not even going to entertain, or to have the plugin php page create the variable containing

$show_field = "<input type='text' name='field1' value='data'>";

or nulled if the setting isn't there. This however means I have hardcoded HTML even if it is a form field and takes the ability to redesign and tamper with this field out of the hands of the site owners.

Can anyone suggest and elegant way to get this to work?

Dan
Im not sure what you really want to do but maybe ZiNgA BuRgA's php in templates would be useful for you.
Its a plugin which lets you use conditionals (or even PHP) straight in templates.

With this, you could simply put in template something like this
<if $mybb->settings['whatever'] then>
  <div>
    this div is visible if 'setting whatever' is set<br>
  </div>
</if>
and it would show (or not) block of template depending on 'whatever' setting in ACP.
(you can add any setting manualy in ACP-configuration-add new setting)

Ofcourse you can use any variables you want not just settings,
so you may show or hide template parts depending on user group,
id, login status and anything you want.
Nah that ia not a way I want to go. It adds another layer of dependency and I am of the opinion php sjould not be used in the presentation layer at least not beyond variables.

Basically there is either going to be a text field or not and this is based on variables now the only way I can see to do that is to make my variable on the plugin.php side be the html of the input field.

This immediately breaks the whole principle of separation between presentation and logic.

Thinking I may just have to display a different template instead no big deal just wqs hoping a simplwy method was available
Still not sure what you want, but assuming, you want 'plugin which process stuff but not hardcode HTML within it' then for eg.

you have your {$show_field} which is your input, and you put it somewhere in template where you want it to be displayed (or not)
Then you may add manualy bit_my_input template with HTML input to your templates
(or make plugin add it automaticaly on plugin installation yourplugin_install / uninstall)
Then in your plugin on 'processing hook' you can load this template if (something) or not.
Then it will be (or not) output as {$show_field}

function yourplugin_install() {
    global $db;
    // create template bit with your HTML in templates
    $templatedat = array(
        "title" => "bit_my_input",
        "template" => "<input type='text' name='field1' value='data'>",
        "sid" => -1);
    $db->insert_query("templates", $templatedat);
}

function yourplugin_uninstall() {
    global $db;
    // remove your template with HTML input bit from templates
    $db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title='bit_my_input'");
}

function yourplugin_somehook() {
    global $mybb, $templates;

    // if your setting is not 0 then
    if ($mybb->settings['your_setting_name']) {
        // get your HTML input from templates and put it to $show_field variable
        eval("\$show_field = \"".$templates->get("bit_my_input")."\";");
    } else {
        // $show_field variable will be empty so nothing will be output
        $show_field = "";
    }
    return $show_field;
}
Would be good if you download fiew plugins from mybb mods and look inside how theyre made to learn about info, install, uninstall, activate, deactivate, ect. Also mybb hooks list and this or this ect may be usefull.
Lol thanks for your suggestion but you are missing the point.

Thanks for the suggestion of downloading and looking at other plugins. Im beyond that stage however and am quite comfortable reading the core classes.

Im not sure how to make what I want more clear but ill just do it by adding and additional template. Not ideal by any means but seems it can be avoided.

Essentially I loathe the idea of having a template with one line of html it seems pointless and will also end up adding an additional query to get the template. It all just seems messy but I guess it cant be helped and ill add cacheing to lessen that slight overhead
Quote:Essentially I loathe the idea of having a template with one line of html it seems pointless and will also end up adding an additional query to get the template. It all just seems messy but I guess it cant be helped and ill add cacheing to lessen that slight overhead

Unfortunately that's the only good way to do it at the minute. 2.0 will have a whole new template syntax with conditionals which will be very welcome.
(2013-05-15, 01:26 PM)Euan T Wrote: [ -> ]
Quote:Essentially I loathe the idea of having a template with one line of html it seems pointless and will also end up adding an additional query to get the template. It all just seems messy but I guess it cant be helped and ill add cacheing to lessen that slight overhead

Unfortunately that's the only good way to do it at the minute. 2.0 will have a whole new template syntax with conditionals which will be very welcome.

Ok cheers.
Use CSS to hide the form item?
Create a class callled myform0 with display:none
Then in the template add you form item with class=myform{$mybb->settings['typesetting']}
that's not too bad either. I will play with each and see which comes up as best.

Cheers
Dan

Actually along that same method I could use an inline style and have

display:none triggered from the php and that would have the same effect, that is probably lighter weight as well. I could always hook into incase people want to do more with it. The only issue is if someone wants to apply a css class to it