12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505 |
- <?php
- /**
- *
- * Cube Framework $Id$ 3sSTOjxP87cZKBtQeSSxZyj51wCJf3IH7ncypRzht28=
- *
- * @link http://codecu.be/framework
- * @copyright Copyright (c) 2017 CodeCube SRL
- * @license http://codecu.be/framework/license Commercial License
- *
- * @version 1.10 [rev.1.10.02]
- */
- namespace Cube\Form;
- use Cube\Validate\AbstractValidate,
- Cube\Filter\AbstractFilter,
- Cube\Validate\NotEmpty,
- Cube\Translate\Adapter\AbstractAdapter as TranslateAdapter,
- Cube\Translate,
- Cube\Controller\Front;
- /**
- * form elements generator class
- * creates an element and outputs it to the view directly
- *
- * Class Element
- *
- * @package Cube\Form
- */
- class Element
- {
- /**
- *
- * the type of the element
- * accepted values: text|hidden|select|textarea|button|submit
- *
- * @var string
- */
- protected $_type;
- /**
- *
- * the name of the element
- *
- * @var string
- */
- protected $_name;
- /**
- *
- * the element's label
- *
- * @var string
- */
- protected $_label;
- /**
- *
- * the subtitle tag, used for display purposes only
- *
- * @var string
- */
- protected $_subtitle;
- /**
- *
- * the name of the subform the element belongs to
- *
- * @var string
- */
- protected $_subForm = null;
- /**
- *
- * a string prefix for the element, for display purposes only
- * applies to text elements for now only
- *
- * @var string
- */
- protected $_prefix;
- /**
- *
- * a string suffix for the element, for display purposes only
- * applies to text elements for now only
- *
- * @var string
- */
- protected $_suffix;
- /**
- *
- * a description of the element
- *
- * @var string
- */
- protected $_description;
- /**
- *
- * setts if an element is hidden or not
- *
- * @var bool
- */
- protected $_hidden = false;
- /**
- *
- * set if we have an array of elements with the same name
- *
- * @var bool
- */
- protected $_multiple = false;
- /**
- *
- * brackets display for elements that will render an array
- *
- * @var string
- */
- protected $_brackets = '[]';
- /**
- *
- * the element's value if it accepts or set of values if accepts multiple values
- *
- * @var mixed
- */
- protected $_value;
- /**
- *
- * data resulted from a previous form submit, used to pre-fill the form element
- *
- * @var mixed
- */
- protected $_data;
- /**
- *
- * the element's attributes, format: 'id' => value
- *
- * @var array
- */
- protected $_attributes = array();
- /**
- *
- * the end tag of the html element
- *
- * @var string
- */
- protected $_endTag = '>';
- /**
- *
- * an array of objects of type \Cube\Validate\AbstractValidate that apply to the element
- *
- * @var array
- */
- protected $_validators = array();
- /**
- *
- * an array of objects of type \Cube\Filter\AbstractFilter that apply to the element
- *
- * @var array
- */
- protected $_filters = array();
- /**
- *
- * element is required or not in the form
- *
- * @var mixed bool or array if we have fields dependencies
- */
- protected $_required = false;
- /**
- *
- * disabled attribute
- *
- * @var bool
- */
- protected $_disabled = false;
- /**
- *
- * options array for select, radio, checkbox form elements
- *
- * @var array
- */
- protected $_multiOptions = array();
- /**
- *
- * attributes that apply for individual options on elements that allow selections
- *
- * @var array
- */
- protected $_multiOptionsAttributes = array();
- /**
- *
- * html code that is to be appended in the <head> tag of the output page
- *
- * @var array
- */
- protected $_headerCode = array();
- /**
- *
- * html code that is to be appended in the <body> tag of the output page
- *
- * @var array
- */
- protected $_bodyCode = array();
- /**
- *
- * set the custom data that applies to the form element in key => value array format
- *
- * @var array
- */
- protected $_customData;
- /**
- *
- * translate adapter
- *
- * @var \Cube\Translate\Adapter\AbstractAdapter
- */
- protected $_translate;
- /**
- *
- * an array of validator messages resulted from the element validation method
- *
- * @var array
- */
- protected $_messages = array();
- /**
- *
- * class constructor
- *
- * @param string $element the type of the element text|hidden etc
- * @param string $name the name of the element
- */
- public function __construct($element, $name)
- {
- $this->setType($element);
- $this->setName($name);
- }
- /**
- *
- * get the element type
- *
- * @return string
- */
- public function getType()
- {
- return $this->_type;
- }
- /**
- *
- * set the type of the element
- *
- * @param string $type
- *
- * @return $this
- */
- public function setType($type)
- {
- $this->_type = (string)$type;
- $this->setHidden(false);
- if (in_array($this->_type, array('hidden', 'csrf'))) {
- $this->setHidden(true);
- }
- return $this;
- }
- /**
- *
- * add a single attribute to the attributes array
- *
- * @param string $key the attribute key (eg. class, id, placeholder etc)
- * @param string $value the attribute value
- * @param bool $append set whether to append data to an existing attribute (default = true)
- * if set to false, the attribute will be overridden
- *
- * @return $this
- */
- public function addAttribute($key, $value, $append = true)
- {
- if (isset($this->_attributes[$key]) && $append === true) {
- $this->_attributes[$key] .= ' ' . $value;
- }
- else {
- $this->_attributes[$key] = $value;
- }
- return $this;
- }
- /**
- *
- * remove an attribute from the attributes array
- *
- * @param string $key
- *
- * @return $this
- */
- public function removeAttribute($key)
- {
- if (isset($this->_attributes[$key])) {
- unset($this->_attributes[$key]);
- }
- return $this;
- }
- /**
- *
- * get an attribute from the attributes array
- *
- * @param string $key
- *
- * @return mixed|null
- */
- public function getAttribute($key)
- {
- if (isset($this->_attributes[$key])) {
- return $this->_attributes[$key];
- }
- return null;
- }
- /**
- *
- * set the attributes of the element
- *
- * @param array $attributes attributes, format 'id' => value
- *
- * @return $this
- */
- public function setAttributes($attributes)
- {
- foreach ((array)$attributes as $key => $value) {
- $this->addAttribute($key, $value);
- }
- return $this;
- }
- /**
- *
- * clear attributes array
- *
- * @return $this
- */
- public function clearAttributes()
- {
- $this->_attributes = array();
- return $this;
- }
- /**
- *
- * return the label of the element
- *
- * @return string
- */
- public function getLabel()
- {
- $translate = $this->getTranslate();
- if (null !== $translate) {
- return $translate->_($this->_label);
- }
- return $this->_label;
- }
- /**
- *
- * set the label of the element
- *
- * @param string $label
- *
- * @return $this
- */
- public function setLabel($label)
- {
- $this->_label = (string)$label;
- return $this;
- }
- /**
- *
- * return the subtitle of a set of elements (like a subform)
- *
- * @return string
- */
- public function getSubtitle()
- {
- $translate = $this->getTranslate();
- if (null !== $translate) {
- return $translate->_($this->_subtitle);
- }
- return $this->_subtitle;
- }
- /**
- *
- * set the subtitle for a set of elements
- *
- * @param string $subtitle
- *
- * @return $this
- */
- public function setSubtitle($subtitle)
- {
- $this->_subtitle = (string)$subtitle;
- return $this;
- }
- /**
- *
- * clear the subtitle variable
- *
- * @return $this
- */
- public function clearSubtitle()
- {
- $this->_subtitle = null;
- return $this;
- }
- /**
- *
- * get the subform(s) the element belongs to
- *
- * @return string
- */
- public function getSubForm()
- {
- return $this->_subForm;
- }
- /**
- *
- * set the subform(s) the element belongs to
- *
- * @param mixed $subForm string or array
- *
- * @return $this
- */
- public function setSubForm($subForm)
- {
- $this->_subForm = $subForm;
- return $this;
- }
- /**
- *
- * get the prefix of the element
- *
- * @return string
- */
- public function getPrefix()
- {
- $translate = $this->getTranslate();
- if (null !== $translate) {
- return $translate->_($this->_prefix);
- }
- return $this->_prefix;
- }
- /**
- *
- * set the prefix of the element
- *
- * @param string $prefix
- *
- * @return $this
- */
- public function setPrefix($prefix)
- {
- $this->_prefix = (string)$prefix;
- return $this;
- }
- /**
- *
- * get the suffix of element
- *
- * @return string
- */
- public function getSuffix()
- {
- $translate = $this->getTranslate();
- if (null !== $translate) {
- return $translate->_($this->_suffix);
- }
- return $this->_suffix;
- }
- /**
- *
- * set a suffix for the element
- *
- * @param string $suffix
- *
- * @return $this
- */
- public function setSuffix($suffix)
- {
- $this->_suffix = (string)$suffix;
- return $this;
- }
- /**
- *
- * return the value(s) of the element, either the element's data or default value(s)
- *
- * @return mixed
- */
- public function getValue()
- {
- return (!empty($this->_data) || $this->_data === '0') ? $this->_data : $this->_value;
- }
- /**
- *
- * set the value(s) of the element
- *
- * @param mixed $value
- *
- * @return $this
- */
- public function setValue($value)
- {
- $this->_value = $value;
- return $this;
- }
- /**
- *
- * set the data of the element, resulted from a previous form input
- * apply any filters that have been enabled for the element
- *
- * @param mixed $data form data
- *
- * @return $this
- */
- public function setData($data)
- {
- $this->_data = $this->applyFilters($data);
- return $this;
- }
- /**
- *
- * clear element data
- *
- * @return $this
- */
- public function clearData()
- {
- $this->_data = null;
- return $this;
- }
- /**
- *
- * get the name of the element
- *
- * @return string
- */
- public function getName()
- {
- return $this->_name;
- }
- /**
- *
- * set the name for the element
- *
- * @param string $name
- *
- * @return $this
- */
- public function setName($name)
- {
- $this->_name = (string)$name;
- return $this;
- }
- /**
- *
- * proxy to getType()
- *
- * @return string
- */
- public function getElement()
- {
- return $this->getType();
- }
- /**
- *
- * get validators array
- *
- * @return array
- */
- public function getValidators()
- {
- return $this->_validators;
- }
- /**
- *
- * get a validator from the element
- *
- * @param string $name
- *
- * @return mixed
- * @throws \InvalidArgumentException
- */
- public function getValidator($name)
- {
- if (isset($this->_validators[$name])) {
- return $this->_validators[$name];
- }
- else {
- throw new \InvalidArgumentException(
- sprintf("The validator named '%s' could not be found
- in the '%s' element.", $name, $this->_name));
- }
- }
- /**
- *
- * remove a validator from the element
- *
- * @param string $name
- *
- * @return $this
- */
- public function removeValidator($name)
- {
- if (isset($this->_validators[$name])) {
- unset($this->_validators[$name]);
- }
- return $this;
- }
- /**
- *
- * clear the validators of the element
- *
- * @return $this
- */
- public function clearValidators()
- {
- $this->_validators = array();
- return $this;
- }
- /**
- *
- * add an array of validator methods that will apply to the element
- *
- * @param array $validators
- *
- * @return $this
- */
- public function setValidators(array $validators)
- {
- foreach ($validators as $validator) {
- $this->addValidator($validator);
- }
- return $this;
- }
- /**
- *
- * adds a new validator to the element
- * accepts a string if creating standard validators
- *
- * @param string|array|\Cube\Validate\AbstractValidate $validator the name of the validator method to be added
- *
- * @return $this
- * @throws \InvalidArgumentException
- */
- public function addValidator($validator)
- {
- if ($validator instanceof AbstractValidate) {
- $this->_validators[get_class($validator)] = $validator;
- }
- else {
- $options = array();
- if (is_array($validator)) {
- $array = $validator;
- $validator = $array[0];
- $options = $array[1];
- }
- // create the validator class
- $validatorClass = '\\Cube\\Validate\\' . ucfirst($validator);
- if (class_exists($validator)) {
- $this->_validators[$validator] = new $validator($options);
- }
- else if (class_exists($validatorClass)) {
- $this->_validators[$validator] = new $validatorClass($options);
- }
- else {
- throw new \InvalidArgumentException(
- sprintf("Class '%s' doesn\'t exist.", $validatorClass));
- }
- }
- return $this;
- }
- /**
- *
- * get filters array
- *
- * @return array
- */
- public function getFilters()
- {
- return $this->_filters;
- }
- /**
- *
- * get a filter from the element
- *
- * @param string $name
- *
- * @return mixed
- * @throws \InvalidArgumentException
- */
- public function getFilter($name)
- {
- if (isset($this->_filters[$name])) {
- return $this->_filters[$name];
- }
- else {
- throw new \InvalidArgumentException(
- sprintf("The filter named '%s' could not be found
- in the '%s' element.", $name, $this->_name));
- }
- }
- /**
- *
- * remove a filter from the element
- *
- * @param string $name
- *
- * @return $this
- */
- public function removeFilter($name)
- {
- if (isset($this->_filters[$name])) {
- unset($this->_filters[$name]);
- }
- return $this;
- }
- /**
- *
- * clear the filters of the element
- *
- * @return $this
- */
- public function clearFilters()
- {
- $this->_filters = array();
- return $this;
- }
- /**
- *
- * add an array of filter methods that will apply to the element
- *
- * @param array $filters
- *
- * @return $this
- */
- public function setFilters(array $filters)
- {
- foreach ($filters as $filter) {
- $this->addFilter($filter);
- }
- return $this;
- }
- /**
- *
- * adds a new filter to the element
- * accepts a string if creating standard filters
- *
- * @param string|array|\Cube\Filter\AbstractFilter $filter the name of the filter method to be added
- *
- * @return $this
- * @throws \InvalidArgumentException
- */
- public function addFilter($filter)
- {
- if ($filter instanceof AbstractFilter) {
- $this->_filters[get_class($filter)] = $filter;
- }
- else {
- $options = array();
- if (is_array($filter)) {
- $array = $filter;
- $filter = $array[0];
- $options = $array[1];
- }
- // create the filter class
- $filterClass = '\\Cube\\Filter\\' . ucfirst($filter);
- if (class_exists($filter)) {
- $this->_filters[$filter] = new $filter($options);
- }
- else if (class_exists($filterClass)) {
- $this->_filters[$filter] = new $filterClass($options);
- }
- else {
- throw new \InvalidArgumentException(
- sprintf("Class '%s' doesn\'t exist.", $filterClass));
- }
- }
- return $this;
- }
- /**
- *
- * apply all filters to the element data
- *
- * @param mixed $data
- *
- * @return mixed
- */
- public function applyFilters($data)
- {
- /** @var \Cube\Filter\AbstractFilter $filter */
- foreach ($this->_filters as $filter) {
- $data = $filter->filter($data);
- }
- return $data;
- }
- /**
- *
- * check if element is required
- *
- * @return mixed
- */
- public function getRequired()
- {
- return $this->_required;
- }
- /**
- *
- * set required status
- *
- * @param mixed $required
- *
- * @return $this
- */
- public function setRequired($required = true)
- {
- $this->_required = (bool)$required;
- return $this;
- }
- /**
- *
- * set disabled attribute
- *
- * @param bool $disabled
- *
- * @return $this
- */
- public function setDisabled($disabled = true)
- {
- $this->_disabled = (bool)$disabled;
- if ($disabled === true) {
- $this->addAttribute('disabled', 'disabled');
- }
- else {
- $this->removeAttribute('disabled');
- }
- return $this;
- }
- /**
- *
- * get disabled attribute
- *
- * @return bool
- */
- public function getDisabled()
- {
- return $this->_disabled;
- }
- /**
- *
- * checks if an element is of hidden type
- *
- * @return bool
- */
- public function isHidden()
- {
- return $this->_hidden;
- }
- /**
- *
- * sets an element as hidden
- *
- * @param bool $hidden
- *
- * @return $this
- */
- public function setHidden($hidden = true)
- {
- $this->_hidden = (bool)$hidden;
- return $this;
- }
- /**
- *
- * get multiple status
- *
- * @return bool
- */
- public function getMultiple()
- {
- return $this->_multiple;
- }
- /**
- *
- * set multiple status
- *
- * @param bool $multiple
- *
- * @return $this
- */
- public function setMultiple($multiple = true)
- {
- $this->_multiple = (bool)$multiple;
- return $this;
- }
- /**
- *
- * get brackets
- *
- * @return string
- */
- public function getBrackets()
- {
- return $this->_brackets;
- }
- /**
- *
- * set custom brackets for the multiple setting
- *
- * @param string $brackets
- *
- * @return $this
- */
- public function setBrackets($brackets)
- {
- $this->_brackets = $brackets;
- return $this;
- }
- /**
- *
- * get the description of the element
- *
- * @return string
- */
- public function getDescription()
- {
- $translate = $this->getTranslate();
- if (null !== $translate) {
- return $translate->_($this->_description);
- }
- return $this->_description;
- }
- /**
- *
- * sets a description for the element
- *
- * @param string $description
- *
- * @return $this
- */
- public function setDescription($description)
- {
- $this->_description = (string)$description;
- return $this;
- }
- /**
- *
- * get the multi options array
- *
- * @return array
- */
- public function getMultiOptions()
- {
- return $this->_multiOptions;
- }
- /**
- *
- * set the multi options array
- *
- * @param array $multiOptions
- *
- * @return $this
- */
- public function setMultiOptions($multiOptions)
- {
- $this->_multiOptions = (array)$multiOptions;
- return $this;
- }
- /**
- *
- * add single multi option key value pair
- *
- * @param string $key
- * @param string $value
- *
- * @return $this
- */
- public function addMultiOption($key, $value)
- {
- $this->_multiOptions[$key] = $value;
- return $this;
- }
- /**
- *
- * clear multi options array
- *
- * @return $this
- */
- public function clearMultiOptions()
- {
- $this->_multiOptions = array();
- return $this;
- }
- /**
- *
- * get multi options attributes
- *
- * @return array
- */
- public function getMultiOptionsAttributes()
- {
- return $this->_multiOptionsAttributes;
- }
- /**
- *
- * set multi options attributes
- *
- * @param array $optionsAttributes
- *
- * @return $this
- */
- public function setMultiOptionsAttributes($optionsAttributes)
- {
- $this->_multiOptionsAttributes = $optionsAttributes;
- return $this;
- }
- /**
- *
- * add single multi option attributes
- *
- * @param string $key
- * @param string $value
- *
- * @return $this
- */
- public function addMultiOptionAttributes($key, $value)
- {
- $this->_multiOptionsAttributes[$key] = $value;
- return $this;
- }
- /**
- *
- * clear multi options attributes
- *
- * @return $this
- */
- public function clearMultiOptionsAttributes()
- {
- $this->_multiOptionsAttributes = array();
- return $this;
- }
- /**
- *
- * get header code
- *
- * @return array
- */
- public function getHeaderCode()
- {
- return $this->_headerCode;
- }
- /**
- *
- * set the header code needed by the element
- *
- * @param string $code
- *
- * @return $this
- */
- public function setHeaderCode($code)
- {
- $this->_headerCode[] = (string)$code;
- return $this;
- }
- /**
- *
- * get body code
- *
- * @return array
- */
- public function getBodyCode()
- {
- return $this->_bodyCode;
- }
- /**
- *
- * set the body code needed by the element
- *
- * @param string $code
- *
- * @return $this
- */
- public function setBodyCode($code)
- {
- $this->_bodyCode[] = (string)$code;
- return $this;
- }
- /**
- *
- * get the custom data of the element
- *
- * @return array
- */
- public function getCustomData()
- {
- return $this->_customData;
- }
- /**
- *
- * set the custom data for the element
- *
- * @param array $customData
- *
- * @return $this
- */
- public function setCustomData($customData)
- {
- $this->_customData = $customData;
- return $this;
- }
- /**
- *
- * set translate adapter
- *
- * @param \Cube\Translate\Adapter\AbstractAdapter $translate
- *
- * @return $this
- */
- public function setTranslate(TranslateAdapter $translate)
- {
- $this->_translate = $translate;
- return $this;
- }
- /**
- *
- * get translate adapter
- *
- * @return \Cube\Translate\Adapter\AbstractAdapter
- */
- public function getTranslate()
- {
- if (!$this->_translate instanceof TranslateAdapter) {
- $translate = Front::getInstance()->getBootstrap()->getResource('translate');
- if ($translate instanceof Translate) {
- $this->setTranslate(
- $translate->getAdapter());
- }
- }
- return $this->_translate;
- }
- /**
- *
- * get all validation messages for the element
- *
- * @return array
- */
- public function getMessages()
- {
- return (array)$this->_messages;
- }
- /**
- *
- * set multiple validation messages
- *
- * @param array $messages
- *
- * @return $this
- */
- public function setMessages(array $messages = null)
- {
- foreach ($messages as $message) {
- $this->setMessage($message);
- }
- return $this;
- }
- /**
- *
- * clear element validator messages
- *
- * @return $this
- */
- public function clearMessages()
- {
- $this->_messages = array();
- return $this;
- }
- /**
- *
- * add a new validation message, but only if the message is not empty
- *
- * @param string $message
- */
- public function setMessage($message)
- {
- if (!empty($message)) {
- $translate = $this->getTranslate();
- if (null !== $translate) {
- $message = $translate->_($message);
- }
- $this->_messages[] = $message;
- }
- }
- /**
- *
- * render element attributes
- * @1.9 rendered only if the value is string or numeric
- * @1.10 rendered only if the value is string, numeric or boolean
- *
- * @return string
- */
- public function renderAttributes()
- {
- $attributes = array();
- foreach ($this->_attributes as $key => $value) {
- if (is_string($value) || is_numeric($value) || is_bool($value)) {
- $attributes[] = $key . '="' . $value . '"';
- }
- }
- return implode(' ', $attributes);
- }
- /**
- *
- * render attributes for individual element option
- *
- * @param string $key
- *
- * @return string
- */
- public function renderOptionAttributes($key)
- {
- $attributes = array();
- if (!empty($this->_multiOptionsAttributes[$key])) {
- $optionAttributes = $this->_multiOptionsAttributes[$key];
- foreach ($optionAttributes as $k => $value) {
- if (is_string($value) || is_numeric($value) || is_bool($value)) {
- $attributes[] = $k . '="' . $value . '"';
- }
- }
- }
- return implode(' ', $attributes);
- }
- /**
- *
- * check if the element is valid
- *
- * @return bool
- */
- public function isValid()
- {
- $valid = true;
- $request = Front::getInstance()->getRequest();
- if (!$request->isPost()) {
- return true;
- }
- if ($this->_required === true) {
- $this->addValidator(
- new NotEmpty());
- }
- foreach ($this->getValidators() as $validator) {
- $valid = ($this->_checkValidator($validator) === true) ? $valid : false;
- }
- return (bool)$valid;
- }
- /**
- *
- * renders the html form element
- * the method is run by all subclasses who don't have it overridden
- *
- * @return string the html code of the element
- */
- public function render()
- {
- $value = $this->getValue();
- if (!is_string($value)) {
- $value = '';
- }
- else {
- $value = str_replace('"', '"', $value);
- }
- $multiple = ($this->getMultiple() === true) ? $this->_brackets : '';
- $attributes = array(
- 'type="' . $this->_type . '"',
- 'name="' . $this->_name . $multiple . '"',
- 'value="' . $value . '"',
- $this->renderAttributes()
- );
- return $this->getPrefix() . ' '
- . '<input ' . implode(' ', array_filter($attributes))
- . $this->_endTag . ' '
- . $this->getSuffix();
- }
- /**
- *
- * check single validator
- *
- * @param AbstractValidate $validator
- * @param string $name
- *
- * @return bool
- */
- protected function _checkValidator(AbstractValidate $validator, $name = null)
- {
- $elementLabel = $this->getLabel();
- if (empty($elementLabel)) {
- $elementLabel = $this->getPrefix();
- }
- if ($name === null) {
- $name = $this->_name;
- }
- $validator->setName($name)
- ->setValue($this->_data);
- $valid = $validator->isValid();
- if (!$valid) {
- $this->setMessage(sprintf($validator->getMessage(), $elementLabel));
- }
- return $valid;
- }
- /**
- *
- * toString magic method, render element
- * enables <code> echo $this->formElement(); ?>
- *
- * @return string
- */
- public function __toString()
- {
- return $this->render();
- }
- protected function _generateOptionAttributes($key, $data)
- {
- }
- }
|