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.

All other Modules /

Discuss all other Modules here.

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

Userform - hidden forms with $Title


Go to End
Reply


11 Posts   2366 Views

Avatar
CHD

Community Member, 218 Posts

1 October 2010 at 3:46am

Hi,

im using the userform, and i've created a function so that the same form appears on every page (instructions here: http://www.silverstripe.org/all-other-modules/show/267822?showPost=293032 )

but now i would like to include a hidden field that captures that page that the form was submitted via, i dont think i can use $Title in the "hidden field" section of the admin, as thats not allowed.

so i assumed i could place something like:

<input type="hidden" name="Page" value="$Title" />

to the template...but where?

any help would be much appreciated.

Avatar
swaiba

Forum Moderator, 1808 Posts

1 October 2010 at 5:17am

Could you (and I am asking either you to look into it or "old & wiser") do something like this to display the form and other items and then add your value into it as requested...

<% control MyForm %>
   <form $FormAttributes>
      <fieldset>
         <% control Fields %>
            <!-- do stuff to render form items -->
         <% end_control %>
      </fieldset>
      <!-- add your hidden field here -->
   </form>
<% end_control %>

the "do stuff to render form items" should be possible because FormField has ->forTemplate() method, I have done something like it before, so you should be able to do this. Hope this helps.

Barry

Avatar
JoshuaLewis

Community Member, 76 Posts

1 October 2010 at 5:56am

I haven't tested this but my first guess would be to create a copy of /sapphire/templates/includes/Form.ss in the includes folder of your theme. Then between lines fourteen and fifteen add your hidden field, you'll probably need to place the title as $Top.Title to get the right value though.

Avatar
CHD

Community Member, 218 Posts

2 October 2010 at 6:28am

i tried that Joshua... the hidden field appears correctly in the form, but the value is blank.
i tried $Title & $Top.Title - no luck.
any ideas?

Avatar
CHD

Community Member, 218 Posts

2 October 2010 at 6:30am

also, when i replaced the value with "Test" that appears OK on the page (hidden) but doesn't come through in the email :(

Avatar
MarijnKampf

Community Member, 164 Posts

6 October 2010 at 3:31am

Edited: 06/10/2010 3:32am

I would create a new field type and add that to the field, something along the lines of:

/userforms/code/editor/CurrentPageField.php

<?php
class EditableCurrentHiddenPageField extends EditableFormField {

   static $singular_name = 'Current Page Hidden field';

   static $plural_name = 'Current Page Hidden fields';

   function getFormField() {
      return new HiddenField($this->Name, null, $this->Parent()->Title);
   }
}

EDIT: For easy upgrading etc., you would ideally place the additional code somewhere in the mysite/code folder, but it didn't seem to detect it running a quick test.

Avatar
CHD

Community Member, 218 Posts

6 October 2010 at 3:33am

cool!

i think that makes sense to me... but i wont be able to test this until about 1 hour from now, so would you be able to add some more instructions for me??

im worried when i try (and probably fail) you wont be online and i'll be stuck!

thanks so much!

Avatar
CHD

Community Member, 218 Posts

6 October 2010 at 4:01am

OK!
it worked...but it ALWAYS says services, because im repeating the form on EVERY page, but it originally resides on the "services" page...so how can i get it to pick up the page its appearing on, rather than the page it was made on?

does that make sense?

Go to Top