Page 1 of 1

Required input fields

Posted: Fri Aug 07, 2009 10:42 am
by yellowled
A. The comment form can have required fields through the spamblock plugin. Any chance to check from a comments.tpl whether any of the fields are set to be required? (Something like "if field is required, emit foo bar"?)

B. The contact form can have required fields as well. First of all: Have I gone blind overnight or are those only possible in the dynamic contact form? And second, same question here: Is it possible to check from plugin_contactform.tpl whether a field is required?

YL

Re: Required input fields

Posted: Fri Aug 07, 2009 12:28 pm
by garvinhicking
Hi!

A.

Hm, your templates config.inc.php could fetch the configuration value from the plugin and pass it along to the smarty template:

Code: Select all

PHP:
$required_fieldlist = serendipity_db_query("SELECT value FROM {$serendipity['dbPrefix']}config WHERE name LIKE '%spamblock%required_fields'", true, 'assoc');
$required_fields = explode(', $required_fieldlist['value']);
$smarty_required_fields = array();
foreach($required_fields AS $required_field) {
  $required_field = trim($required_field);
  if (empty($required_field)) continue;
  $smarty_required_fields[$required_field] = $required_field;
}
$serendipity['smarty']->assign('required_fields', $smarty_required_fields);

TPL:
{if $required_fields.author}Author wird benötigt!{/if}
B.
Also presets have required fields:

Code: Select all

        switch ($dynamic_tpl) {
            case 'small_biz':
                $fields = 'require;'.PLUGIN_CONTACTFORM_FNAME.';text:require;'.PLUGIN_CONTACTFORM_LNAME.';text:'.'require;'.EMAIL.';text:'
            break;
            case 'detailed':
                $fields = 'require;'.PLUGIN_CONTACTFORM_FNAME.';text:require;'.PLUGIN_CONTACTFORM_LNAME.';text:'.'require;'.EMAIL.';text:'
            break;
            default:
                $fields = $this->get_config('dynamic_fields','require;'.NAME.';text:'.'require;'.EMAIL.';text:'.'require;'.HOMEPAGE.';text
            break;
        }
Required fields can easily be detected inside the template file like

Code: Select all

{if $field.required}The field {$field.id} is required...{/if}
Regards,
Garvin

Re: Required input fields

Posted: Fri Aug 07, 2009 12:59 pm
by yellowled
Whoa, that's a lot of code ... :mrgreen:

Thanks, I'll check it out over the weekend and report back!

YL

Re: Required input fields

Posted: Fri Aug 07, 2009 4:39 pm
by onli
Code for A is working great (but it's name, not author, in that example, and a typo at the explode: ',', instead of ',). Tanks for the answer Garvin, thanks for the question yellowled.

Re: Required input fields

Posted: Fri Aug 07, 2009 6:51 pm
by yellowled
garvinhicking wrote:B.
Also presets have required fields:
But those only work in the dynamic contact form, right?

YL

Re: Required input fields

Posted: Fri Aug 07, 2009 9:24 pm
by Don Chambers
plugin_contactform.tpl is used only when using the contact form type of "standard" and is essentially the same as the comment form... so I suspect Garvin's method A should work there... whether or not a field is required is determined by the spamblock plugin (I think).

plugin_dynamicform.tpl (or any derivatives of it) is used to handle everything else except "standard" - which includes the presets small business, detailed, and custom. Small business (first name, last name, email, message) and "detailed" (first name, last name, email, mailing address, message) are hard coded as required in the plugin.

"custom" defaults to name (a single field, not first and last), email, homepage, and message and all fields are required. These, as you know, are the ones that can be changed, added to, etc...

So method "B" does not require anything in config.inc.php.. it is in fact, already in use to show superscripted asterisks for required fields in the default plugin_dynamicform.tpl:

Code: Select all

{if $field.required}<sup>*</sup>{/if}
So, for comments or the standard contact form, use method A (config.inc.php).... for all other contact form types, use method B.

You definitely do NOT want to require the field "replyTo" since this does not exist in the contact form.. well, not the default contact form anyway.

I'm in the middle of something else today, but when I get a bit of free time, I'll also try playing around with this.

Re: Required input fields

Posted: Fri Jan 27, 2012 4:30 pm
by yellowled
So now I have an actual use case for this.

This line:

Code: Select all

$required_fields = explode(', $required_fieldlist['value']);
seems to have a type, is it supposed to be ('', maybe?

Also: How do I get the proper field names for the tpl? Your code says author – the spamblock plugin lists name, email, url, replyTo and comment …

YL

Re: Required input fields

Posted: Fri Jan 27, 2012 4:44 pm
by yellowled
Figured it out. Call me PHP hacker! :mrgreen:

The code line is supposed to be:

Code: Select all

$required_fields = explode(',', $required_fieldlist['value']);
because the values are comma seperated in the db. And the field names are the same used by the spamblock plugin: name, email, url, replyTo (you probably don't want to use that as Don pointed out) and comment. Awesome.

YL