Skip to content

Instantly share code, notes, and snippets.

@Modicrumb
Created July 15, 2019 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Modicrumb/e848e4f610e52d9b9884650eb3016608 to your computer and use it in GitHub Desktop.
Save Modicrumb/e848e4f610e52d9b9884650eb3016608 to your computer and use it in GitHub Desktop.
<?php
echo $this->Form->control(
'is_active',
[
'type' => 'toggleInput',
'templateVars' => [
'labelLeft' => 'OFF',
'labelRight' => 'ON'
],
'id' => false,
]
);
<?php
//make default errorClass form-control-danger in set up
return [
// General grouping container for control(). Defines input/label ordering.
'formGroup' => '{{label}}{{input}}',
//grouping container for control with prepend and append enabled
'inputWithIcon' => '{{label}}<div class="input-group">{{inputPrepend}}{{input}}{{inputAppend}}</div>',
//used for inputWithIcon
'inputPrepend' => '<div class="input-group-prepend"><span class="input-group-text">{{prepend}}</span></div>',
//used for inputWithIcon
'inputAppend' => '<div class="input-group-append"><span class="input-group-text">{{append}}</span></div>',
// Label element when inputs are not nested inside the label.
'label' => '<label{{attrs}}>{{text}} <span class="help">{{labelHelp}}</span></label>',
// Error message wrapper elements.
'error' => '<div class="form-control-feedback">{{content}}</div>',
// Container for error items.
'errorList' => '<ul>{{content}}</ul>',
// Error item wrapper.
'errorItem' => '<li>{{text}}</li>',
// Container element used by control().
'inputContainer' => '
<div class="form-group form-group-{{type}} {{required}}">
{{content}}
<small class="form-text text-muted">{{help}}</small>
{{after}}
</div>
',
//container only used for checkbox on login page
'materialCheckboxContainer' => '
<div class="checkbox checkbox-primary">
{{content}}
<small class="form-text text-muted">{{help}}</small>
{{after}}
</div>
',
// Container element used by control() when a field has an error.
'inputContainerError' => '
<div class="form-group form-group-{{type}} {{required}} has-danger">
{{content}}
{{error}}
</div>',
//Generic input element.
'input' => '<input type="{{type}}" name="{{name}}" class="form-control {{extraClass}}" {{attrs}}>',
// Textarea input element,
'textarea' => '<textarea class="form-control {{extraClass}}" name="{{name}}"{{attrs}}>{{value}}</textarea>',
// Select element,
'select' => '<select class="form-control {{extraClass}}" name="{{name}}"{{attrs}}>{{content}}</select>',
// Used for checkboxes in checkbox() and multiCheckbox().
'checkbox' => '<input type="checkbox" class="custom-control-input {{extraClass}}" name="{{name}}" value="{{value}}"{{attrs}}>',
//material checkbox, only used in login page
'materialCheckbox' => '<input type="checkbox" class="filled-in chk-col-light-blue" name="{{name}}" value="{{value}}"{{attrs}}>',
// Radio input element,
'radio' => '<input type="radio" class="custom-control-input {{extraClass}}" name="{{name}}" value="{{value}}"{{attrs}}>',
//nesting label for checkbox only on login page
'nestingLabelMaterialCheckbox' => '{{hidden}}{{input}}<label {{attrs}}>{{text}}</label>',
// nesting label for checkboxes.
'nestingLabelCheckbox' => '{{hidden}}<label class="custom-control custom-checkbox" {{attrs}}>{{input}}<span class="custom-control-label">{{text}}</span></label>',
//nesting label for radio
'nestingLabelRadio' => '{{hidden}}<label class="custom-control custom-radio" {{attrs}}>{{input}}<span class="custom-control-label">{{text}}</span></label>',
// File input used by custom file
'customFile' => '<div class="file-loading"><input class="custom-file-input {{extraClass}}" type="file" name="{{name}}"{{attrs}}></div>{{hiddenCheckbox}}',
//hidden checkbox
'hiddenCheckbox' => '<div class="d-none">{{input}}</div>',
// Multi-select element,
'selectMultiple' => '<select class="form-control multiselect {{extraClass}}" name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>',
//toggle widget container
'toggleWidget' => '<div class="switch {{additionalContainerClass}}"><label>{{labelLeft}} {{content}}<span class="lever"></span> {{labelRight}}</label></div>',
//toggleWidgetNestingLabel
'nestingLabelToggleWidget' => '{{hidden}}{{input}}',
//toggleWidgetCheckbox
'toggleWidgetCheckbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>'
];
<?php
namespace Theme\View\Widget;
use Cake\View\Form\ContextInterface;
use Cake\View\Widget\WidgetInterface;
class ToggleInputWidget implements WidgetInterface
{
/**
* StringTemplate instance.
*
* @var \Cake\View\StringTemplate
*/
protected $_templates;
/**
* Constructor.
*
* @param \Cake\View\StringTemplate $templates Templates list.
*/
public function __construct($templates)
{
$templates->add(
[
'inputContainer' => $templates->get('toggleWidget'),
'checkbox' => $templates->get('toggleWidgetCheckbox'),
'nestingLabel' => $templates->get('nestingLabelToggleWidget')
]
);
$this->_templates = $templates;
}
/**
* Render a text widget or other simple widget like email/tel/number.
*
* This method accepts a number of keys:
*
* - `name` The name attribute.
* - `val` The value attribute.
* - `escape` Set to false to disable escaping on all attributes.
*
* Any other keys provided in $data will be converted into HTML attributes.
*
* @param array $data The data to build an input with.
* @param \Cake\View\Form\ContextInterface $context The current form context.
* @return string
*/
public function render(array $data, ContextInterface $context)
{
$data += [
'name' => '',
'val' => null,
'type' => 'checkbox',
'escape' => true,
'templateVars' => [
'labelLeft' => 'OFF',
'labelRight' => 'ON',
'additionalContainerClass' => '',
],
'nestedInput' => true
];
$data['value'] = $data['val'];
$data['type'] = 'checkbox';
unset($data['val']);
return $this->_templates->format(
'input',
[
'name' => $data['name'],
'type' => $data['type'],
'templateVars' => $data['templateVars'],
'attrs' => $this->_templates->formatAttributes(
$data,
['name', 'type', 'labelLeft', 'labelRight', 'additionalContainerClass']
),
]
);
}
/**
* {@inheritDoc}
*/
public function secureFields(array $data)
{
if (!isset($data['name']) || $data['name'] === '') {
return [];
}
return [$data['name']];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment