Posted on by

Setting the Default Value of a Field in a Custom Template

Sometimes you need to set the default value of a field depending on the context of the form you are using. Or perhaps you know what the field should default to because of which page the form is on or some other factor.

This is easy to do using a custom template. If you’re not familiar with how to set up a custom template in Participants Database, take a look at this article before you begin: Using Participants Database Custom Templates

So, for the sake of our tutorial here, we have a radio control that we want to pre-set with a certain value. In this template that is a fixed value, but it could be a dynamic value, you will just need to determine that value and use it in place of the fixed value in this template. For our example, we have a radio field that can be select “blue” or “red.” It normally defaults to “blue,” but in this case we want it to have “red” selected when the user opens the form.

The code that makes the change is this:

<?php
 // first identify the field we need to change
 if ( $this->field->name === 'selector' ) {
    /* 
     * now set it's default value, 
     * which is the field's pre-set value
     */
    $this->field->value = 'red';
 } ?>

That will pre-set the value of your selector to “red.” Here is the full template so you can see where it goes:

Leave a Reply
You have to agree to the comment policy.

Leave a Reply
You have to agree to the comment policy.