1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 16:26:59 +02:00

Add InputfieldCheckbox checked() method as an optional shortcut for getting/setting checkbox state

This commit is contained in:
Ryan Cramer
2019-06-07 12:13:02 -04:00
parent 08bea26a17
commit 50a6f3585e

View File

@@ -160,7 +160,6 @@ class InputfieldCheckbox extends Inputfield {
if($this->autocheck || $this->getSetting('formBuilder')) $this->attr('checked', 'checked'); if($this->autocheck || $this->getSetting('formBuilder')) $this->attr('checked', 'checked');
} }
return parent::setAttribute($key, $value); return parent::setAttribute($key, $value);
} }
@@ -169,15 +168,50 @@ class InputfieldCheckbox extends Inputfield {
return parent::set($key, $value); return parent::set($key, $value);
} }
/**
* Get or set current checkbox boolean attribute state
*
* ~~~~~
* // the following two lines are equivalent to GET checkbox state
* $checked = $f->checked();
* $checked = !empty($f->attr('checked'));
*
* // the following two lines are equivalent to SET checkbox state
* $f->checked(true);
* $f->attr('checked', 'checked');
* ~~~~~
*
* @param bool|null Specify boolean to set checkbox state
* @return bool
* @since 3.0.133
*
*/
public function checked($checked = null) {
if($checked !== null) {
if($checked) {
$this->attr('checked', 'checked');
$checked = true;
} else {
$this->removeAttr('checked');
$checked = false;
}
} else {
$checked = $this->attr('checked');
$checked = empty($checked) ? false : true;
}
return $checked;
}
/** /**
* Is checkbox currently checked? * Is checkbox currently checked?
* *
* #pw-internal
*
* @return bool * @return bool
* *
*/ */
public function isChecked() { public function isChecked() {
$checked = $this->attr('checked'); return $this->checked();
return !empty($checked);
} }
/** /**
@@ -188,7 +222,7 @@ class InputfieldCheckbox extends Inputfield {
*/ */
public function isEmpty() { public function isEmpty() {
// return $this->attr('value') != $this->checkedValue; // return $this->attr('value') != $this->checkedValue;
return !$this->isChecked(); return !$this->checked();
} }
/** /**