2 August 2013 at 9:50am
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.
11 August 2013 at 6:37pm
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.
One I prepared quickly - https://gist.github.com/wilr/6203714
Add it, dev/build and you should now be able to attach a hidden field in the CMS with the name of the GET value to use. Haven't added nice icons or anything yet (or even tested it)
13 August 2013 at 10:02am
Thanks for looking at this.
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.
13 August 2013 at 7:03pm
Updated the code to fix that warning.
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).
13 August 2013 at 7:25pm
Thanx Willr...it works like a charm!!!!
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?
13 August 2013 at 11:52pm
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