Skip to content

Instantly share code, notes, and snippets.

@obinoob
Created September 9, 2017 16:40
Show Gist options
  • Save obinoob/7ea8636b4abb0dc8d61227b8aac6954a to your computer and use it in GitHub Desktop.
Save obinoob/7ea8636b4abb0dc8d61227b8aac6954a to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: proudbyte
* Date: 29/08/17
* Time: 11:52
*/
namespace App\View\Widget;
use Cake\View\Widget\BasicWidget;
use Cake\View\Form\ContextInterface;
use Exception;
class CustomCheckboxWidget extends BasicWidget
{
protected $_compactAttributes = [
'displayMessage' => true
];
/**
* Render a checkbox element.
*
* Data supports the following keys:
*
* - `name` - The name of the input.
* - `value` - The value attribute. Defaults to '1'.
* - `val` - The current value. If it matches `value` the checkbox will be checked.
* You can also use the 'checked' attribute to make the checkbox checked.
* - `disabled` - Whether or not the checkbox should be disabled.
*
* Any other attributes passed in will be treated as HTML attributes.
*
* @param array $data The data to create a checkbox with.
* @param \Cake\View\Form\ContextInterface $context The current form context.
* @return string Generated HTML string.
*/
public function render(array $data, ContextInterface $context)
{
$data += [
'name' => '',
'value' => 1,
'val' => null,
'disabled' => false,
'templateVars' => []
];
if ($this->_isChecked($data)) {
$data['checked'] = true;
}
unset($data['val']);
$attrs = $this->_templates->formatAttributes(
$data,
['name', 'value']
);
return $this->_templates->format('checkbox', [
'checkboxstyle' => (function() use ($data){
return isset($data['checkboxstyle']) ? $data['checkboxstyle'] : 'checkbox checkbox-primary';
})(),
'name' => $data['name'],
'value' => $data['value'],
'templateVars' => $data['templateVars'],
'attrs' => $attrs,
'icon' => (function() use ($data) {
if(isset($data['icon'])){
return $icon = " <i class='{$data['icon']}'></i>";
}
})(),
'text' => $data['text'],
'error' => (function() use ($context, $data) {
if($context->hasError($data['name']) && $this->_setDisplayMessage($data)){
$errors = $context->error($data['name']);
$messages = "";
foreach ($errors as $key => $value) {
$messages .= '<span class="custom-checkbox-message-error">'.$value.'</span>';
}
return $messages;
}
})(),
'bold' => (function() use ($context, $data) {
if($context->hasError($data['name'])){
return 'custom-checkbox-label-error';
}
})()
]);
}
private function _setDisplayMessage(array $data = null)
{
if(isset($data['displayMessage'])) {
if (is_bool($data['displayMessage'])) {
return $data['displayMessage'];
} else {
throw new Exception("Please provide a boolean, set 'displayMessage' to true/false");
}
}else {
return $this->_compactAttributes['displayMessage'];
}
}
/**
* Check whether or not the checkbox should be checked.
*
* @param array $data Data to look at and determine checked state.
* @return bool
*/
protected function _isChecked($data)
{
if (array_key_exists('checked', $data)) {
return (bool)$data['checked'];
}
return (string)$data['val'] === (string)$data['value'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment