2

i've added some attributes for each user on my Magento installation. Each attribute is correctly showed in the registration fields, user can fill them, they're mandatory and they are also validated by Magento's javascript validation (followed the tutorial here : http://inchoo.net/magento/programming-magento/validate-your-input-magento-style/)

I need to validate also server side, i started creating a module following a tutorial online.

Every example that i've found overrided Magento's standard attributes. What should i override to validate my custom attributes? Which event should i catch? I'm a bit confused...

UPDATE : here is a zip file of my module https://www.dropbox.com/s/7nxjx6mh0hly6yn/MyModule.zip?dl=0. Let me know if you spot the error.

2

The validate() method of Mage_Customer_Model_Customer is called at multiple occations which are all not influencable with observers. Unfortunately the method itself hard-wires quite a few things, so if you want to change or extend customer validation, the most reliable way is to rewrite the customer model and override validate().

public function validate()
{
    $errors = parent::validate();
    if ($errors = true) { // means: "no errors" - don't ask!
        $errors = array();
    }
    if (!my_custom_validation($this->getMyCustomAttribute()) {
        $errors[] = "That's invalid!";
    }
    if (empty($errors)) {
        return true;
    }
    return $errors;
}
1

You could try either

  1. Rewrite Mage_Customer_AccountController see Overriding Frontend Core Controllers

or

  1. Create an observer for customer_save_before

Then do your validation see Magento custom model with custom validation rules

<?xml version="1.0"?>
<config>
  <modules>
        <ElementiCreativi_Checker>
            <version>0.5</version>
        </ElementiCreativi_Checker>
  </modules>
  <global>
    <models>
        <elementicreativichecker>
             <class>ElementiCreativi_Checker_Model</class>
        </elementicreativichecker>
    </models>
    <events>
      <customer_save_before>
        <observers>
          <controllaparametrireg>
            <type>singleton</type>
            <class>checker/observer</class>
            <method>controllaParametri</method>
          </controllaparametrireg>
        </observers>
      </customer_save_before>     
    </events>
  </global>
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.