Magento – Fatal error: Call to a member function isRequired() on a non-object in app/code/core/Mage/Captcha/Model/Observer.php on line 167

captchaerrorfatal errorfrontend-errormagento-1.7

I am getting a the fatal error message below appear on random occasions:

Fatal error: Call to a member function isRequired() on a non-object in
{SERVER_PATH}app/code/core/Mage/Captcha/Model/Observer.php on line 167

The fatal error takes down both the frontend and admin. The only way that I can seem to resolve the issue when it occurs is by restarting the server. After restarting the server the website can go up to 2 – 3 weeks without the issue repeating itself.

Has anybody come across this same issue before or know how it can be resolved?

Best Answer

We had the same issue. I have overloaded the core method and added an additional check. Maybe this will help, if the error occurs the next time.

app/code/local/Stackexchange/CaptchaExtension/Model/Observer.php

<?php
class Stackexchange_CaptchaExtension_Model_Observer extends Mage_Captcha_Model_Observer
{
    public function checkUserLoginBackend($observer)
    {
        $formId = 'backend_login';
        $captchaModel = Mage::helper('captcha')->getCaptcha($formId);
        $captchaModel = false;

        if (is_bool($captchaModel)) {
            // this should not happen (see Mage_Captcha_Model_Observer::checkUserLoginBackend)
            // PHP message: PHP Fatal error:  Call to a member function isRequired() on boolean in /srv/storage/www/htdocs/magento/app/code/core/Mage/Captcha/Model/Observer.php on line 167
            // so we log this!

            Mage::log(sprintf('Error during Captcha loading. Instead of model instance we got boolean "%s" in %s line %s.', $captchaModel ? 'true' : 'false', __FILE__, __LINE__), Zend_Log::DEBUG, 'system.log');
            return $this;
        }

        return parent::checkUserLoginBackend($observer);
    }
}

app/code/local/Stackexchange/CaptchaExtension/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Stackexchange_CaptchaExtension>
            <version>1.0.0</version>
        </Stackexchange_CaptchaExtension>
    </modules>
    <global>
        <models>
            <captcha>
                <rewrite>
                    <observer>Stackexchange_CaptchaExtension_Model_Observer</observer>
                </rewrite>
            </captcha>
        </models>
    </global>
</config>

app/etc/modules/Stackexchange_CaptchaExtension.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Stackexchange_CaptchaExtension>
            <active>true</active>
            <codePool>local</codePool>
        </Stackexchange_CaptchaExtension>
    </modules>
</config>