_request = Front::getInstance()->getRequest();
}
/**
*
* set field labels
*
* @param array $fieldLabels
* @return $this
*/
public function setFieldLabels($fieldLabels)
{
$this->_fieldLabels = $fieldLabels;
return $this;
}
/**
*
* get field labels
*
* @return array
*/
public function getFieldLabels()
{
return $this->_fieldLabels;
}
/**
*
* get individual field label
*
* @param $key
* @return string
*/
public function getFieldLabel($key)
{
if (isset($this->_fieldLabels[$key])) {
return $this->_fieldLabels[$key];
}
return $this->_label;
}
/**
*
* return the value(s) of the element, either the element's data or default value(s)
*
* @param string $key
* @return mixed
*/
public function getValue($key = null)
{
$value = parent::getValue();
if ($key !== null) {
if (array_key_exists($key, (array) $value)) {
return $value[$key];
}
else {
return null;
}
}
return $value;
}
/**
*
* render element attributes
*
* @param string $type
* @return \Cube\Form\Element
*/
public function renderAttributes($type = null)
{
$attributes = null;
foreach ($this->_attributes as $key => $value) {
$attributes .= $key . '="' . ((is_array($value)) ? $value[$type] : $value) . '" ';
}
return $attributes;
}
/**
*
* check if the composite element is valid
*
* @return bool
*/
public function isValid()
{
$valid = true;
if (!$this->_request->isPost()) {
return true;
}
if ($this->_required === true) {
$this->addValidator(
new NotEmpty());
}
$firstNameLabel = $this->getFieldLabel(self::KEY_FIRST);
$lastNameLabel = $this->getFieldLabel(self::KEY_LAST);
$firstNameValue = $this->getValue(self::KEY_FIRST);
$lastNameValue = $this->getValue(self::KEY_LAST);
// get original values
$label = $this->getLabel();
$data = $this->_data;
foreach ($this->getValidators() as $validator) {
// check first name
$this->setLabel($firstNameLabel);
$this->setData($firstNameValue);
$valid = ($this->_checkValidator($validator) === true) ? $valid : false;
// check last name
$this->setLabel($lastNameLabel);
$this->setData($lastNameValue);
$valid = ($this->_checkValidator($validator) === true) ? $valid : false;
}
// restore values
$this->setLabel($label);
$this->setData($data);
return (bool)$valid;
}
/**
*
* render composite element
*
* @return string
*/
public function render()
{
return $this->getPrefix() . ' '
. '_name . '[' . self::KEY_FIRST . ']" '
. $this->renderAttributes(self::KEY_FIRST)
. 'value="' . $this->getValue(self::KEY_FIRST) . '" '
. $this->_endTag . ' '
. $this->getSuffix()
. ' '
. $this->getPrefix() . ' '
. '_name . '[' . self::KEY_LAST . ']" '
. $this->renderAttributes(self::KEY_LAST)
. 'value="' . $this->getValue(self::KEY_LAST) . '" '
. $this->_endTag . ' '
. $this->getSuffix();
}
}