/** * Method to validate the form data. * * @param JForm $form The form to validate against. * @param array $data The data to validate. * @param string $group The name of the field group to validate. * * @see JFormRule * @see JFilterInput * * @return mixed Array of filtered data if valid, false otherwise. */ public function validate($form, $data, $group = null) { $ok = parent::validate($form, $data); $input = $this->app->input; // Standard jForm validation failed so we shouldn't test further as we can't be sure of the data if (!$ok) { return false; } $db = FabrikWorker::getDbo(true); $elementModel = $this->getElementPluginModel($data); $nameChanged = $data['name'] !== $elementModel->getElement()->name; $elementModel->getElement()->bind($data); $listModel = $elementModel->getListModel(); if ($data['id'] == '') { // Have to forcefully set group id otherwise listmodel id is blank $elementModel->getElement()->group_id = $data['group_id']; if ($listModel->canAddFields() === false && $listModel->noTable() === false) { $this->setError(FText::_('COM_FABRIK_ERR_CANT_ADD_FIELDS')); } if (!FabrikWorker::validElementName($data['name'])) { $this->setError(FText::_('COM_FABRIK_RESERVED_NAME_USED')); } } else { if ($listModel->canAlterFields() === false && $nameChanged && $listModel->noTable() === false) { $this->setError(FText::_('COM_FABRIK_ERR_CANT_ALTER_EXISTING_FIELDS')); } if ($nameChanged && !FabrikWorker::validElementName($data['name'], false)) { $this->setError(FText::_('COM_FABRIK_RESERVED_NAME_USED')); } } $listModel = $elementModel->getListModel(); /** * Test for duplicate names * un-linking produces this error */ if (!$input->get('unlink', false) && (int) $data['id'] === 0) { /** * @FIXME - if a repeat group is created through the Group settings, we don't add the auto-created * table to the #_fabrik_lists table, so the following query obviously doesn't find it ... so we * barf when creating element in the repeat group with a "duplicate name", even though it's going * to be on a separate table. */ $query = $db->getQuery(true); $query->select('t.id')->from('#__{package}_joins AS j'); $query->join('INNER', '#__{package}_lists AS t ON j.table_join = t.db_table_name'); $query->where('group_id = ' . (int) $data['group_id'] . ' AND element_id = 0'); $db->setQuery($query); $joinTblId = (int) $db->loadResult(); $ignore = array($data['id']); if ($joinTblId === 0) { if ($listModel->fieldExists($data['name'], $ignore)) { $this->setError(FText::_('COM_FABRIK_ELEMENT_NAME_IN_USE')); } } else { /** @var FabrikFEModelList $joinListModel */ $joinListModel = JModelLegacy::getInstance('list', 'FabrikFEModel'); $joinListModel->setId($joinTblId); $joinEls = $joinListModel->getElements(); foreach ($joinEls as $joinEl) { if ($joinEl->getElement()->name == $data['name']) { $ignore[] = $joinEl->getElement()->id; } } if ($joinListModel->fieldExists($data['name'], $ignore)) { $this->setError(FText::_('COM_FABRIK_ELEMENT_NAME_IN_USE')); } } } // Strip <p> tag from label $data['label'] = JString::str_ireplace(array('<p>', '</p>'), '', $data['label']); return count($this->getErrors()) == 0 ? $data : false; }