bkg2

Search

Sharing is Caring

Subscribe via RSS

Web Design Tag Cloud

$allowed_extensions $member $title 2011 ai allowedextensions amazon analytics apple attachedfiles attachedimages bad widget class name given blog blog date brands business news classes click here digital cms columns commission conflict content control creating a facebook following css data dataobject::get date format default search results design digital driving traffic east london embed embed twitter estate agent websites facebook facebook advertising facebook marketing fan pages fast hosts favicon favicon generator file is not a valid upload follow button football form future geo targeting customers google google adwords google local business centre google logo google maps google places has_many hmv if conditions if pos image inbound linking include blog entry integrate twitter internet turns 20 ip ip address javascript jquery link building livingsocial local businesses local businesses on google local business web design london london web design london web designers london web developers making money on the web menus migration mobile mobile site mobile version modeladmin mod_fcgid: http request length multiple class mutliple images new website nofollow oauth onlien marketing online ads online discounts online marketing online media online offers outbound links pac-man pageclass php warning: fopen promotions property web design property web devlopers property website random child rar real estate web design recommend recommended agency recommended agency register referral rel="nofollow" responsive rooms & studios search seo shoreditch show full fields shreditch web designers sidebar signups silverstripe silvestripe small business social media social social media social networks split strategy styles technology timeline twitter twitter api twitter api 1.1 twitter feed undefined uploadify http error url icon urlsegment userdefinedforms userforms user forms ux validationexception video vouchering vouchers web web builds web design web design development web designers web design shoreditch web design web development web developers web developers in shoreditch web developers london web developers shoreditch web development widgets wysiwyg youtube

Including $member, $title data, IP Address, or a hidden field, in user defined forms with SilverStripe

The user form module, is excellent, it allows content authors to be able to easily create fully functional forms. However, sometimes we need to extend it slightly, one of the most common requirements, are including member data automatically within the form, or a page title. Here's what you need to do:

Create a file named to suit your function inside userforms/code/editor: such as "CurrentMemberField.php"

Inside this file, define your data type and setup the field that will be included in the form:

<?php
class EditableCurrentMemberField extends EditableFormField {

   static $singular_name = 'Member Name Hidden field';

   function getFormField() {
       $member = Member::currentUser();
      return new HiddenField($this->Name, $this->Title, $member->Email);
}
    public function getIcon() {
        return 'userforms/images/editableliteralfield.png';
    }
}

Save that and build your database, now within your form tab of your CMS, you will see a field type called Current Member Field (or what ever you called it) If you include that in your form now, it will automatically create a hidden field, and insert the member email address (obviously they will need to be logged in for this to work)

If you wanted to include the page title instead, you'd just use this:

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

If you want to include an object via page relationship, you'll need some conditional formatting too, otherwise when the form submit it will return: "DataObjectManager [Notice] Trying to get property of non-object"

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

Also note that we've included an icon for this field type, to stop ugly alt tags appearing in the CMS.

 public function getIcon() {
        return 'userforms/images/editableliteralfield.png';
    }

Capturing an IP Address of User Form submission

If you want to keep track of who's submitting via your form, just use this:

<?php
class IPaddressField extends EditableFormField {

   static $singular_name = 'User IP Address';

   function getFormField() {
       $member = Member::currentUser();
       $ip = $_SERVER['REMOTE_ADDR'];
      return new HiddenField($this->Name, $this->Title, $ip);
}
    public function getIcon() {
        return 'userforms/images/editableliteralfield.png';
    }
}

END!

As ever, if you see any problems with the above or can improve it, comments welcome below!

Posted by Ryan Badger on 13 June 2011 | 0 Comments

Tags: , , , , , , , ,

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments