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

Extend user defined form with 'Keep with next' configuration


Go to End
Reply


9 Posts   2586 Views

Avatar
MarijnKampf

Community Member, 164 Posts

21 January 2011 at 1:20am

I would like to add a checkbox field labeled 'Keep with next' to Field Configuration of user defined form fields. When checked it should add css class to the fields' markup that allows for CSS styling to display fields side by side e.g. First name + Last name.

I thought I could do this by extending the EditableFormField class:
CustomEditableFormField.php

<?php
/**
* Represents the base class of a editable form field
* object like {@link EditableTextField}.
*
* @package userforms
*/

class CustomEditableFormField extends Extension {
function extraStatics() {
      return array(
         'db' => array(
            "KeepWithNext" => "Boolean"
         )
      );
   }

   /**
    * Implement custom field Configuration on this field. Includes such things as
    * settings and options of a given editable form field
    *
    * @return FieldSet
    */
   public function getFieldConfiguration() {
      //Debug::Show($fields);
      $fields = parent::getFieldConfiguration();

      $extraFields = new FieldSet(
         new CheckboxField($this->owner->getFieldName('KeepWithNext'), _t('CustomEditableFormField.KEEPWITHNEXT', 'Keep with next?'), $this->owner->KeepWithNext)
      );

      $fields->merge($extraFields);
      return $fields;
   }
}

And adding DataObject::add_extension('EditableFormField', 'CustomEditableFormField'); to _config.php.

However, this doesn't seem to work.

Am I doing something wrong? Should I extend each FormField individually?

Avatar
Willr

Forum Moderator, 5513 Posts

22 January 2011 at 1:06pm

You would also need to alter the built in EditableFormField class so that it actually looked at extensions (which I don't believe it does) as SilverStripe doesn't do this by default. Add in $this->extend('functionName', $this); to the end (or beginning) of any function you need to add hooks into (like getFieldConfiguration).

Avatar
MarijnKampf

Community Member, 164 Posts

24 January 2011 at 10:56pm

Edited: 24/01/2011 11:02pm

I couldn't get the $this->extend('functionName', $this); to work so I ended up hacking into the code (as the extend needed to be added to the existing code anyway).

I've posted my code below in case someone finds this thread looking for how to add fields to user defined form.

Index: code/editor/EditableFormField.php
===================================================================
--- code/editor/EditableFormField.php   (revision 115687)
+++ code/editor/EditableFormField.php   (working copy)
@@ -1,7 +1,7 @@
<?php
/**
- * Represents the base class of a editable form field
- * object like {@link EditableTextField}.
+ * Represents the base class of a editable form field
+ * object like {@link EditableTextField}.
*
* @package userforms
*/
@@ -16,6 +16,7 @@
      "Default" => "Varchar",
      "Sort" => "Int",
      "Required" => "Boolean",
+      "KeepWithPrevious" => "Boolean",
      "CustomErrorMessage" => "Varchar(255)",
      "CustomRules" => "Text",
      "CustomSettings" => "Text",
@@ -291,6 +292,7 @@
      $this->Default       = (isset($data['Default'])) ? $data['Default'] : "";
      $this->Sort       = (isset($data['Sort'])) ? $data['Sort'] : null;
      $this->Required    = !empty($data['Required']) ? 1 : 0;
+      $this->KeepWithPrevious   = !empty($data['KeepWithPrevious']) ? 1 : 0;
      $this->Name       = $this->class.$this->ID;
      $this->CustomRules   = "";
      $this->CustomErrorMessage   = (isset($data['CustomErrorMessage'])) ? $data['CustomErrorMessage'] : "";
@@ -341,7 +343,8 @@
            $this->getSettingFieldName('RightTitle'),
            _t('EditableFormField.RIGHTTITLE', 'Right Title'),
            $this->getSetting('RightTitle')
-         )
+         ),
+         new CheckboxField($this->getFieldName('KeepWithPrevious'), _t('EditableFormField.KeepWithPrevious', 'Keep with previous?'), $this->KeepWithPrevious)
      );
   }
   
Index: code/UserDefinedForm.php
===================================================================
--- code/UserDefinedForm.php   (revision 115571)
+++ code/UserDefinedForm.php   (working copy)
@@ -389,7 +389,11 @@
            if($right = $editableField->getSetting('RightTitle')) {
               $field->setRightTitle($right);
            }
-            
+
+            if($editableField->KeepWithPrevious) {
+               $field->addExtraClass('keepWithPrevious');
+            }
+
            // if this field is required add some
            if($editableField->Required) {
               $field->addExtraClass('requiredField');

Avatar
Garrett

Community Member, 245 Posts

27 January 2011 at 10:25am

Hi, great thread -- very helpful. Where does this code live:

@@ -341,7 +343,8 @@
$this->getSettingFieldName('RightTitle'),
_t('EditableFormField.RIGHTTITLE', 'Right Title'),
$this->getSetting('RightTitle')
- )
+ ),
+ new CheckboxField($this->getFieldName('KeepWithPrevious'), _t('EditableFormField.KeepWithPrevious', 'Keep with previous?'), $this->KeepWithPrevious)
);
}

I'm not seeing any instances of "RightTitle" anywhere in EditableFormField.php.

Thanks,
Garrett

Avatar
MarijnKampf

Community Member, 164 Posts

27 January 2011 at 10:42am

Around line 332 you should see the following code, where the above changes need to be made.

/**
    * Implement custom field Configuration on this field. Includes such things as
    * settings and options of a given editable form field
    *
    * @return FieldSet
    */
   public function getFieldConfiguration() {
      return new FieldSet(
         new TextField(
            $this->getSettingFieldName('RightTitle'),
            _t('EditableFormField.RIGHTTITLE', 'Right Title'),
            $this->getSetting('RightTitle')
         )
      );
   }

Avatar
Garrett

Community Member, 245 Posts

29 January 2011 at 9:12am

Hi Marijn,
Thanks Marijn,

Ok I've copied all the code indicated in this thread,

ERROR [User Error]: Uncaught Exception: Object->__call(): the method 'getfieldname' does not exist on 'EditableTextField'

And indeed, I do not see this function anywhere in Userforms [v0.3.0]. What am I missing? Basically, I am just trying to add a field that goes along with every text field called "html_id", which will allow a user to attach a custom ID to the field for the purposes of hooking into other systems.

Thanks again,
Garrett

Avatar
Garrett

Community Member, 245 Posts

1 February 2011 at 8:21am

Good news -- I've now got my html_id field appearing on the Form tab in the CMS!

HOWEVER

Now the task is to get the server to write it in the published page. I.e.: <input id="$html_id".../> It looks as though I'll need to do this using the "id()" function in FormField.php. But this class doesn't know about this field, so I am trying to add it to the constructor as follows:

function __construct($name, $title = null, $value = null, $form = null, $rightTitle = null, $html_id) {

But this gives me an error:

ERROR [Warning]: Missing argument 6 for FormField::__construct(), called in C:\Workspace\DDEX\sapphire\forms\DatalessField.php on line 27 and defined
IN POST /admin/getitem?ID=112&ajax=1
Line 84 in C:\Workspace\DDEX\sapphire\forms\FormField.php

At this point this is deeper than I know Sapphire. Marijn or Willr, can you guys help me to get this custom field to show up on the front end?

Thanks so much,
Garrett

Attached Files
Avatar
MarijnKampf

Community Member, 164 Posts

1 February 2011 at 9:51pm

Have you tried to adjust that in one of the templates in userforms/templates? You will have to take a look in the templates to see which one you will need to alter exactly.

Go to Top