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.
This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.
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.
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.
new TextareaField( name, title, rows, cols, value, form)
documentation here
To set a default value for a DB field use the $defaults array:
public static $defaults = array(
'Name' => 'John Doe'
);
Mitch - Where in the code (which file) would your suggested code go?
Thanks!
June
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. :)