mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
Added segment field
This commit is contained in:
parent
766555928e
commit
85f47e3623
@ -15,6 +15,7 @@ class EditableCheckboxGroupField extends EditableMultipleOptionField {
|
||||
|
||||
public function getFormField() {
|
||||
$field = new UserFormsCheckboxSetField($this->Name, $this->EscapedTitle, $this->getOptionsMap());
|
||||
$field->setTemplate('forms/UserFormsCheckboxSetField');
|
||||
|
||||
// Set the default checked items
|
||||
$defaultCheckedItems = $this->getDefaultOptions();
|
||||
|
@ -1,9 +1,17 @@
|
||||
<?php
|
||||
|
||||
use SilverStripe\Forms\SegmentField;
|
||||
use SilverStripe\Forms\SegmentFieldModifier\IDSegmentFieldModifier;
|
||||
use SilverStripe\Forms\SegmentFieldModifier\SlugSegmentFieldModifier;
|
||||
|
||||
/**
|
||||
* Represents the base class of a editable form field
|
||||
* object like {@link EditableTextField}.
|
||||
*
|
||||
* @package userforms
|
||||
*
|
||||
* @property string Name
|
||||
*
|
||||
* @method DataList DisplayRules() List of EditableCustomRule objects
|
||||
*/
|
||||
class EditableFormField extends DataObject {
|
||||
@ -154,7 +162,11 @@ class EditableFormField extends DataObject {
|
||||
),
|
||||
TextField::create('Title'),
|
||||
TextField::create('Default', _t('EditableFormField.DEFAULT', 'Default value')),
|
||||
TextField::create('RightTitle', _t('EditableFormField.RIGHTTITLE', 'Right title'))
|
||||
TextField::create('RightTitle', _t('EditableFormField.RIGHTTITLE', 'Right title')),
|
||||
SegmentField::create('Name')->setModifiers(array(
|
||||
UnderscoreSegmentFieldModifier::create()->setDefault('Field'),
|
||||
DisambiguationSegmentFieldModifier::create(),
|
||||
))->setPreview($this->Name)
|
||||
)
|
||||
);
|
||||
|
||||
@ -281,7 +293,7 @@ class EditableFormField extends DataObject {
|
||||
|
||||
// Set a field name.
|
||||
if(!$this->Name) {
|
||||
$this->Name = $this->RecordClassName . $this->ID;
|
||||
$this->Name = get_class($this) . '_' . $this->ID;
|
||||
$this->write();
|
||||
}
|
||||
}
|
||||
|
54
code/modifiers/DisambiguationSegmentFieldModifier.php
Normal file
54
code/modifiers/DisambiguationSegmentFieldModifier.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
use SilverStripe\Forms\SegmentFieldModifier\AbstractSegmentFieldModifier;
|
||||
|
||||
class DisambiguationSegmentFieldModifier extends AbstractSegmentFieldModifier {
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPreview($value) {
|
||||
if($this->form instanceof Form && $record = $this->form->getRecord()) {
|
||||
$parent = $record->Parent();
|
||||
|
||||
$try = $value;
|
||||
|
||||
$sibling = EditableformField::get()
|
||||
->filter('ParentID', $parent->ID)
|
||||
->filter('Name', $try)
|
||||
->where('"ID" != ' . $record->ID)
|
||||
->first();
|
||||
|
||||
$counter = 1;
|
||||
|
||||
while($sibling !== null) {
|
||||
$try = $value . '_' . $counter++;
|
||||
|
||||
$sibling = EditableformField::get()
|
||||
->filter('ParentID', $parent->ID)
|
||||
->filter('Name', $try)
|
||||
->first();
|
||||
}
|
||||
|
||||
if ($try !== $value) {
|
||||
return $try;
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSuggestion($value) {
|
||||
return $this->getPreview($value);
|
||||
}
|
||||
}
|
27
code/modifiers/UnderscoreSegmentFieldModifier.php
Normal file
27
code/modifiers/UnderscoreSegmentFieldModifier.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use SilverStripe\Forms\SegmentFieldModifier\SlugSegmentFieldModifier;
|
||||
|
||||
class UnderscoreSegmentFieldModifier extends SlugSegmentFieldModifier {
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPreview($value) {
|
||||
return str_replace('-', '_', parent::getPreview($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*
|
||||
* @param string $value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getSuggestion($value) {
|
||||
return str_replace('-', '_', parent::getSuggestion($value));
|
||||
}
|
||||
}
|
@ -11,7 +11,8 @@
|
||||
"require": {
|
||||
"silverstripe/framework": ">=3.1.0",
|
||||
"silverstripe/cms": ">=3.1.0",
|
||||
"silverstripe-australia/gridfieldextensions": "1.1.0"
|
||||
"silverstripe-australia/gridfieldextensions": "1.1.0",
|
||||
"silverstripe/segment-field": "^1.0"
|
||||
},
|
||||
"suggest": {
|
||||
"colymba/gridfield-bulk-editing-tools": "Allows for bulk management of form submissions"
|
||||
|
6
templates/forms/CheckboxField_holder.ss
Normal file
6
templates/forms/CheckboxField_holder.ss
Normal file
@ -0,0 +1,6 @@
|
||||
<div data-id="$ID" class="field<% if extraClass %> $extraClass<% end_if %>">
|
||||
$Field
|
||||
<label class="right" for="$Name">$Title</label>
|
||||
<% if $Message %><span class="message $MessageType">$Message</span><% end_if %>
|
||||
<% if $Description %><span class="description">$Description</span><% end_if %>
|
||||
</div>
|
12
templates/forms/CheckboxSetField.ss
Normal file
12
templates/forms/CheckboxSetField.ss
Normal file
@ -0,0 +1,12 @@
|
||||
<ul data-id="$ID" class="$extraClass">
|
||||
<% if $Options.Count %>
|
||||
<% loop $Options %>
|
||||
<li class="$Class">
|
||||
<input id="$Name[$Value]" class="checkbox" name="$Name" type="checkbox" value="$Value"<% if $isChecked %> checked="checked"<% end_if %><% if $isDisabled %> disabled="disabled"<% end_if %> />
|
||||
<label for="$Name[$Value]">$Title</label>
|
||||
</li>
|
||||
<% end_loop %>
|
||||
<% else %>
|
||||
<li>No options available</li>
|
||||
<% end_if %>
|
||||
</ul>
|
9
templates/forms/FormField_holder.ss
Normal file
9
templates/forms/FormField_holder.ss
Normal file
@ -0,0 +1,9 @@
|
||||
<div data-id="$ID" class="field<% if $extraClass %> $extraClass<% end_if %>">
|
||||
<% if $Title %><label class="left" for="$Name">$Title</label><% end_if %>
|
||||
<div class="middleColumn">
|
||||
$Field
|
||||
</div>
|
||||
<% if $RightTitle %><label class="right" for="$Name">$RightTitle</label><% end_if %>
|
||||
<% if $Message %><span class="message $MessageType">$Message</span><% end_if %>
|
||||
<% if $Description %><span class="description">$Description</span><% end_if %>
|
||||
</div>
|
@ -18,7 +18,10 @@ class UserDefinedFormControllerTest extends FunctionalTest {
|
||||
|
||||
// load the form
|
||||
$this->get($form->URLSegment);
|
||||
$response = $this->submitForm('UserForm_Form', null, array('basic-text-name' => 'Basic Value'));
|
||||
|
||||
$field = $this->objFromFixture('EditableTextField', 'basic-text');
|
||||
|
||||
$response = $this->submitForm('UserForm_Form', null, array($field->Name => 'Basic Value'));
|
||||
|
||||
// should have a submitted form field now
|
||||
$submitted = DataObject::get('SubmittedFormField', "\"Name\" = 'basic-text-name'");
|
||||
|
Loading…
Reference in New Issue
Block a user