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.

Fabian Schmengler
66.2k25 gold badges191 silver badges422 bronze badges
asked Sep 16, 2014 at 15:57

2 Answers 2

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;
}
answered Nov 7, 2014 at 22:17
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>
answered Sep 16, 2014 at 16:36
5
  • Built this module but i was not able to catch the event. Could you please check? dropbox.com/s/7nxjx6mh0hly6yn/MyModule.zip?dl=0 Commented Sep 16, 2014 at 17:39
  • Can you add your config.xml above.. Also take a look @ stackoverflow.com/questions/21853010/… Commented Sep 16, 2014 at 17:48
  • It is included in the zip file. Commented Sep 16, 2014 at 17:49
  • Any news? Thank you very much. Commented Sep 16, 2014 at 22:57
  • Take a look at my updated answer. Next time add the code directly to your question Commented Sep 18, 2014 at 13:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.