From 2becc0cbbacf9ba7eb88fbeafcfa2230a46b6755 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 27 Jan 2017 14:03:28 -0500 Subject: [PATCH] Add an val() method to the Inputfield class for consistency with jQuery. This method is a shortcut for getting or setting the value attribute of Inputfield objects --- wire/core/Inputfield.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/wire/core/Inputfield.php b/wire/core/Inputfield.php index d47ccc9d..358843e4 100644 --- a/wire/core/Inputfield.php +++ b/wire/core/Inputfield.php @@ -659,6 +659,25 @@ abstract class Inputfield extends WireData implements Module { } return $this->setAttribute($key, $value); } + + /** + * Shortcut for getting or setting “value” attribute + * + * When setting a value, it returns $this (for fluent interface). + * + * ~~~~~ + * $value = $inputfield->val(); * // Getting + * $inputfield->val('foo'); * // Setting + * ~~~~~ + * + * @param string|null $value + * @return string|int|float|array|object|Wire|WireData|WireArray|Inputfield + * + */ + public function val($value = null) { + if($value === null) return $this->getAttribute('value'); + return $this->setAttribute('value', $value); + } /** * Get all attributes specified for this Inputfield