Back to CaptchaField class

Method setup

public bool
setup
(\SimpleXMLElement $element, mixed $value, mixed $group = null)
Method to attach a Form object to the field.
Parameters
  • \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
  • mixed $value The form field value to validate.
  • string $group The field name group control value. This acts as an array container for the field. For example if the field has name="foo" and the group value is set to "bar" then the full field name would end up being "bar[foo]".
Returns
  • bool True on success.
Since
  • 2.5
Class: CaptchaField
Project: Joomla

Method setup - Source code

/**
 * Method to attach a Form object to the field.
 *
 * @param   \SimpleXMLElement  $element  The SimpleXMLElement object representing the `<field>` tag for the form field object.
 * @param   mixed              $value    The form field value to validate.
 * @param   string             $group    The field name group control value. This acts as an array container for the field.
 *                                       For example if the field has name="foo" and the group value is set to "bar" then the
 *                                       full field name would end up being "bar[foo]".
 *
 * @return  boolean  True on success.
 *
 * @since   2.5
 */
public function setup(\SimpleXMLElement $element, $value, $group = null)
{
    $result = parent::setup($element, $value, $group);
    $app = Factory::getApplication();
    $default = $app->get('captcha');
    if ($app->isClient('site')) {
        $default = $app->getParams()->get('captcha', $default);
    }
    $plugin = $this->element['plugin'] ? (string) $this->element['plugin'] : $default;
    $this->plugin = $plugin;
    if ($plugin === 0 || $plugin === '0' || $plugin === '' || $plugin === null) {
        $this->hidden = true;
        return false;
    } else {
        // Force field to be required. There's no reason to have a captcha if it is not required.
        // Obs: Don't put required="required" in the xml file, you just need to have validate="captcha"
        $this->required = true;
        if (strpos($this->class, 'required') === false) {
            $this->class .= ' required';
        }
    }
    $this->namespace = $this->element['namespace'] ? (string) $this->element['namespace'] : $this->form->getName();
    try {
        // Get an instance of the captcha class that we are using
        $this->_captcha = Captcha::getInstance($this->plugin, array('namespace' => $this->namespace));
        /**
         * Give the captcha instance a possibility to react on the setup-process,
         * e.g. by altering the XML structure of the field, for example hiding the label
         * when using invisible captchas.
         */
        $this->_captcha->setupField($this, $element);
    } catch (\RuntimeException $e) {
        $this->_captcha = null;
        Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
        return false;
    }
    return $result;
}