Added new containerAttributes property to form fields

This commit is contained in:
Sam Georges 2014-09-25 18:05:35 +10:00
parent 5f12d9fc9b
commit 746f425d88
14 changed files with 54 additions and 11 deletions

View File

@ -1,5 +1,6 @@
* **Build 14x** (2014-09-xx)
- Added new `hint` form field type (see Backend > Forms docs).
- Added new `containerAttributes` property to form fields (see Backend > Forms docs).
* **Build 145** (2014-09-13)
- Standard setting pages now have **Save** and **Save and Close** buttons.

View File

@ -1,6 +1,7 @@
<?php namespace Backend\Classes;
use Str;
use HTML;
/**
* Form Field definition
@ -237,7 +238,8 @@ class FormField
if (isset($config['placeholder'])) $this->placeholder = $config['placeholder'];
if (isset($config['default'])) $this->defaults = $config['default'];
if (isset($config['cssClass'])) $this->cssClass = $config['cssClass'];
if (isset($config['attributes'])) $this->attributes = $config['attributes'];
if (isset($config['attributes'])) $this->attributes($config['attributes']);
if (isset($config['containerAttributes'])) $this->attributes($config['containerAttributes'], 'container');
if (isset($config['depends'])) $this->depends = $config['depends'];
if (isset($config['path'])) $this->path = $config['path'];
@ -266,6 +268,41 @@ class FormField
return $this;
}
/**
* Sets the attributes for this field in a given position.
* - field: Attributes are added to the form field element (input, select, textarea, etc)
* - container: Attributes are added to the form field container (div.form-group)
* @param array $items
* @param strubg $position
* @return void
*/
public function attributes($items, $position = 'field')
{
if (!is_array($items))
return;
$multiArray = array_filter($items, 'is_array');
if (!$multiArray) {
$this->attributes[$position] = $items;
return;
}
foreach ($items as $_position => $_items) {
$this->attributes($_items, $_position);
}
}
/**
* Returns the attributes for this field at a given position.
* @param string $position
* @return array
*/
public function getAttributes($position = 'field', $htmlBuild = true)
{
$result = array_get($this->attributes, $position, []);
return $htmlBuild ? HTML::attributes($result) : $result;
}
/**
* Returns a value suitable for the field name property.
* @param string $arrayName Specify a custom array name

View File

@ -116,7 +116,8 @@ class Relation extends FormWidgetBase
// by joining its pivot table. Remove all joins from the query.
$query->getQuery()->getQuery()->joins = [];
if (in_array('October\Rain\Database\Traits\NestedTree', class_uses($relatedObj)))
$treeTraits = ['October\Rain\Database\Traits\NestedTree', 'October\Rain\Database\Traits\SimpleTree'];
if (count(array_intersect($treeTraits, class_uses($relatedObj))) > 0)
$field->options = $query->listsNested($this->nameFrom, $relatedObj->getKeyName());
else
$field->options = $query->lists($this->nameFrom, $relatedObj->getKeyName());

View File

@ -2,6 +2,7 @@
class="form-group <?= $this->previewMode ? 'form-group-preview' : '' ?> <?= $field->type ?>-field span-<?= $field->span ?> <?= $field->required?'is-required':'' ?> <?= $field->stretch?'layout-relative':'' ?> <?= $field->cssClass ?>"
<?php if ($depends = $this->getFieldDepends($field)): ?>data-field-depends="<?= $depends ?>"<?php endif ?>
data-field-name="<?= $field->fieldName ?>"
<?= $field->getAttributes('container') ?>
id="<?= $field->getId('group') ?>">
<?= $this->makePartial('field', ['field' => $field]) ?>
</div>

View File

@ -2,7 +2,7 @@
$fieldOptions = $field->options();
?>
<!-- Balloon selector -->
<div data-control="balloon-selector" id="<?= $field->getId() ?>" <?= HTML::attributes($field->attributes) ?>>
<div data-control="balloon-selector" id="<?= $field->getId() ?>" <?= $field->getAttributes() ?>>
<ul>
<?php foreach ($fieldOptions as $value => $text): ?>
<li data-value="<?= e($value) ?>" class="<?= $value == $field->value ? 'active' : '' ?>"><?= e(trans($text)) ?></li>

View File

@ -7,7 +7,7 @@
value="1"
<?= $this->previewMode ? 'disabled="disabled"' : '' ?>
<?= $field->value == 1 ? 'checked="checked"' : '' ?>
<?= HTML::attributes($field->attributes) ?>>
<?= $field->getAttributes() ?>>
<label for="<?= $field->getId() ?>">
<?= e(trans($field->label)) ?>

View File

@ -9,7 +9,7 @@
id="<?= $field->getId() ?>"
name="<?= $field->getName() ?>"
class="form-control custom-select"
<?= HTML::attributes($field->attributes) ?>>
<?= $field->getAttributes() ?>>
<?php if ($field->placeholder): ?>
<option value=""><?= e(trans($field->placeholder)) ?></option>
<?php endif ?>

View File

@ -12,6 +12,6 @@
autocomplete="off"
maxlength="255"
pattern="\d+"
<?= HTML::attributes($field->attributes) ?>
<?= $field->getAttributes() ?>
/>
<?php endif ?>

View File

@ -10,6 +10,6 @@
class="form-control"
autocomplete="off"
maxlength="255"
<?= HTML::attributes($field->attributes) ?>
<?= $field->getAttributes() ?>
/>
<?php endif ?>

View File

@ -20,7 +20,7 @@
value="<?= $value ?>"
type="radio"
<?= $field->value == $value ? 'checked="checked"' : '' ?>
<?= HTML::attributes($field->attributes) ?>>
<?= $field->getAttributes() ?>>
<label for="<?= $fieldId ?>">
<?= e(trans($option[0])) ?>

View File

@ -13,7 +13,7 @@
value="1"
<?= $this->previewMode ? 'readonly="readonly"' : '' ?>
<?= $field->value == 1 ? 'checked="checked"' : '' ?>
<?= HTML::attributes($field->attributes) ?>>
<?= $field->getAttributes() ?>>
<span><span><?= e(trans('backend::lang.form.field_on')) ?></span><span><?= e(trans('backend::lang.form.field_off')) ?></span></span>
<a class="slide-button"></a>
</label>

View File

@ -11,6 +11,6 @@
class="form-control"
autocomplete="off"
maxlength="255"
<?= HTML::attributes($field->attributes) ?>
<?= $field->getAttributes() ?>
/>
<?php endif ?>

View File

@ -8,5 +8,5 @@
autocomplete="off"
class="form-control field-textarea size-<?= $field->size ?>"
placeholder="<?= e(trans($field->placeholder)) ?>"
<?= HTML::attributes($field->attributes) ?>><?= e($field->value) ?></textarea>
<?= $field->getAttributes() ?>><?= e($field->value) ?></textarea>
<?php endif?>

View File

@ -202,6 +202,9 @@ trait AssetMaker
*/
public function getAssetPath($fileName, $assetPath = null)
{
if (preg_match("/(\/\/|http|https)/", $fileName))
return $fileName;
if (!$assetPath)
$assetPath = $this->assetPath;