Back to FormModel class

Method validate

public array|bool
validate
(mixed $form, mixed $data, mixed $group = null)
Method to validate the form data.
Parameters
  • \Joomla\CMS\Form\Form $form The form to validate against.
  • array $data The data to validate.
  • string $group The name of the field group to validate.
Returns
  • array|bool Array of filtered data if valid, false otherwise.
Since
  • 1.6
-
  • \Joomla\CMS\Form\FormRule
  • \Joomla\CMS\Filter\InputFilter
Class: FormModel
Project: Joomla

Method validate - Source code

/**
 * Method to validate the form data.
 *
 * @param   Form    $form   The form to validate against.
 * @param   array   $data   The data to validate.
 * @param   string  $group  The name of the field group to validate.
 *
 * @return  array|boolean  Array of filtered data if valid, false otherwise.
 *
 * @see     FormRule
 * @see     InputFilter
 * @since   1.6
 */
public function validate($form, $data, $group = null)
{
    // Include the plugins for the delete events.
    PluginHelper::importPlugin($this->events_map['validate']);
    $dispatcher = Factory::getContainer()->get('dispatcher');
    if (!empty($dispatcher->getListeners('onUserBeforeDataValidation'))) {
        @trigger_error('The `onUserBeforeDataValidation` event is deprecated and will be removed in 5.0.' . 'Use the `onContentValidateData` event instead.', E_USER_DEPRECATED);
        Factory::getApplication()->triggerEvent('onUserBeforeDataValidation', array($form, &$data));
    }
    Factory::getApplication()->triggerEvent('onContentBeforeValidateData', array($form, &$data));
    // Filter and validate the form data.
    $data = $form->filter($data);
    $return = $form->validate($data, $group);
    // Check for an error.
    if ($return instanceof \Exception) {
        $this->setError($return->getMessage());
        return false;
    }
    // Check the validation results.
    if ($return === false) {
        // Get the validation messages from the form.
        foreach ($form->getErrors() as $message) {
            $this->setError($message);
        }
        return false;
    }
    // Tags B/C break at 3.1.2
    if (!isset($data['tags']) && isset($data['metadata']['tags'])) {
        $data['tags'] = $data['metadata']['tags'];
    }
    return $data;
}