I was wondering if it was possible to pass a variable from 1 page to another with a User Defined Form. I have a property site that i am building and when the person visits the property they like and want more details i want them to click on a link that takes them to a user defined form. I would like to pass a variable to the form so that the client does not have to type in the name of the property they are interested in.
In normal php i would have just appended the ?myvar=x to the url and gotten my form to read it with echo $_GET['myvar']
How do i do this in silverstripe...preferably on the User Defined Form Module.
UserDefinedForms can load data from Session, but not GET parameters. So you have a couple options. What I would recommend you do is you create a new EditableFormField which takes your parameters and puts the data into the form. You then add an instance of that EditableFormField to your Form.
1) I tried the code you supplied and got the following error:
[User Error] Uncaught Exception: Object->__call(): the method 'requestvar' does not exist on 'EditableHiddenField'
2) If i understand your code correctly (i hope) we are creating a field in the CMS that is instructed to look for a specific pattern in the url of the page. So lets say the parameter i was passing from my property page to the user defined form page was called 'house'.... the url would then look like this: www.example.com?house=99.
The getFormField function then tries to match the url part with the value i entered into the cms called 'house' extracts the rest of the string eg house=99 and puts it into a hidden text field that displays in the front end and gets submitted when the user completes the form which then sends an email.
So the new editable form field we created will add an option 'Editable Hidden Field' to the CMS Field Editor. Add one to your form and under 'Options' enter the value you want in the hidden field (e.g "GET[house]" as the string).
Just a question, i see that when i enter GET[house] into the new TextField created in the CMS that stores the parameter and save it, when the CMS refreshes it disappears. You cant see it in the CMS, but it works correctly on the front end...is it supposed to do that?
Excellent!! This works really well. Thanks so much.
Personally i think that this should form part of the default UserForm module because it makes it so easy for developers to use UserForm and pass important bits of information to the form the user submits.
Just a few things
1) I quickly made a icon for it...see attached
2) Something i forgot to mention earlier was that when i tested the code initially i got this error:
Fatal error: Access level to EditableHiddenField::$singular_name must be public (as in class EditableFormField) in /usr/www/users/obdemrzzgc/p3prop/userforms/code/model/formfields/EditableHiddenField.php on line 43
I just changed private static $singular_name to static $singular_name AND private static $plural_name to static $plural_name and now it works