Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Default Value of Text Field in User Forms


Go to End


5 Posts   5777 Views

Avatar
mage

Community Member, 10 Posts

1 June 2010 at 3:40am

I'm trying to find out how to give a textfield or textareafield a default value, can anyone help me with this?

I've searched the docs and tried to search the code (as best I can), but can't find it.

Any help appreciated.

Avatar
kidcardboard

Community Member, 5 Posts

16 June 2010 at 3:25am

new TextareaField( name, title, rows, cols, value, form)

documentation here

Avatar
mitch

Community Member, 3 Posts

6 October 2010 at 10:06am

To set a default value for a DB field use the $defaults array:

public static $defaults = array(
   'Name' => 'John Doe'
);

Avatar
juneallison

Community Member, 110 Posts

24 September 2013 at 7:47am

Mitch - Where in the code (which file) would your suggested code go?

Thanks!

June

Avatar
Jare

Community Member, 39 Posts

5 January 2015 at 5:34am

I know that this is very old topic, but as this is the only related topic showing up in google when searching for default value for userforms, I'll add my note here just in case it helps somebody. This is definitively not an actual solution to the original question (which by the way concerned the userforms module, not custom coded forms) but may come in handy in some situations.

If you need to prefill some form fields by using data that comes from another page (through a link), you can specify the default value in the URL of your form:
1. First you have to find out the technical name of the field you wish to set a default value for. Go to your form page in your website (public or draft), open up the page's HTML source code (Ctrl+U usually does that).
2. Look for a string like <input type="text" name="EditableNumericField25" ...> near the human readable label of your field. The type and name attributes may differ from the example.
3. In the browser put something like this to the end of the form page's address and hit Enter: ?EditableNumericField25=MyDefaultValue
4. Now you should see MyDefaultValue showing up in the form as a default value. Use the same technique to create links from other pages to your form. Please remember to [i]urlencode[/i] the default value if needed. You can define default values also for other fields. Just use & instead of ? when chaining the additional default values in the URL.

So this is not the best approach in most of the cases, but perhaps it works for someone like it worked for me. :)