-
- mongolito404

-
Total Posts: 11
Joined: 2008-01-28
|
Hoi,
je me permet de cross-poster en français au cas de des Magentiens francophones pourraient éclairer de leur lumières.
J’ai ajouté quatres attributs à la classe Customer en ajoutant les entrées suivante dans ma DB
INSERT INTO `eav_attribute` (`entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_global`, `is_visible`, `is_required`, `is_user_defined`, `default_value`, `is_searchable`, `is_filterable`, `is_comparable`, `is_visible_on_front`, `is_unique`, `apply_to`, `use_in_super_product`) VALUES (1, 'vatnumber', NULL, 'vatcustomer/entity_customer_attribute_backend_vatnumber', 'varchar', NULL, NULL, 'text', 'VAT Number', NULL, NULL, 1, 1, 1, 0, NULL, 1, 0, 0, 0, 0, 0, 1); INSERT INTO `eav_attribute` ( `entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_global`, `is_visible`, `is_required`, `is_user_defined`, `default_value`, `is_searchable`, `is_filterable`, `is_comparable`, `is_visible_on_front`, `is_unique`, `apply_to`, `use_in_super_product`) VALUES (1, 'vatcountry', NULL, NULL, 'varchar', NULL, NULL, 'select', 'VAT Country', NULL, 'vatcustomer/entity_customer_attribute_source_vatcountry', 1, 1, 1, 0, 'BE', 1, 1, 1, 1, 0, 0, 1); INSERT INTO `eav_attribute` ( `entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_global`, `is_visible`, `is_required`, `is_user_defined`, `default_value`, `is_searchable`, `is_filterable`, `is_comparable`, `is_visible_on_front`, `is_unique`, `apply_to`, `use_in_super_product`) VALUES (1, 'vatvalid', NULL, NULL, 'int', NULL, NULL, NULL, 'VAT Number is valided by VIES', NULL, NULL, 1, 1, 0, 0, NULL, 1, 1, 0, 0, 0, 0, 1); INSERT INTO `eav_attribute` (`entity_type_id`, `attribute_code`, `attribute_model`, `backend_model`, `backend_type`, `backend_table`, `frontend_model`, `frontend_input`, `frontend_label`, `frontend_class`, `source_model`, `is_global`, `is_visible`, `is_required`, `is_user_defined`, `default_value`, `is_searchable`, `is_filterable`, `is_comparable`, `is_visible_on_front`, `is_unique`, `apply_to`, `use_in_super_product`) VALUES (1, 'vatlastcheck', NULL, NULL, 'datetime', NULL, NULL, NULL, 'Date of the last VAT Number validity check.', NULL, NULL, 1, 1, 0, 0, NULL, 0, 0, 0, 0, 0, 0, 1);
J’ai ensuite ajouté un module comprenant les classes Mage_VatCustomer_Model_Entity_Customer_Attribute_Backend_Vatnumber et Mage_VatCustomer_Model_Entity_Customer_Attribute_Source_Vatcountry dans les fichiers adéquats.
La “backend_class” pour l’attribut VatCustomer est responsable de la validation et donne une valeur aux attributs vatvalid et vatlastcheck:
class Mage_VatCustomer_Model_Entity_Customer_Attribute_Backend_Vatnumber extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract { public function beforeSave($object) { $vatNumber = $object->getVatnumber(); $vatCountry = $object->getVatcountry(); $validation = $this->checkVat($vatCountry, $vatNumber); if(!$validation['syntaxValid']) Mage::throwException(Mage::helper('customer')->__('Invalid VAT Number.')); $object->setVatvalid((bool)$validation['valid']); $object->setVatlastcheck($validation['requestDate']); return $object; }
/** * Check a VAT Number. Validate it's syntax and then * call the VIES Web Service. * Result array has the following fields: * - 'countryCode': equals to $countryCode, * - 'vatNumber': equals to $vatNumber, * - 'requestDate': equals to date('Y-m-d H:i:s'). * - 'syntaxValid'': indicates if the $vatNumber syntax is valid, * - 'valid'': indicates if the VAT Number was found in the VIES Database, * - 'name': the name associated to the VAT Number in the VIES Database, * - 'address': the address associated to the VAT Number in the VIES * Database * * If the syntax is not valid or if the VIES Database could not be * contacted, the 'valid' field is set to FALSE and the 'name' and 'address' * fields are set to the empty string (''). * * @param countryCode The VAT Country code * @param vatNulber The VAT Number (without country code) * * @return array An array with validation results. */ private function checkVat($countryCode, $vatNumber) { //... } }
Quand j’édite les attributs d’un client depuis l’interface d’administration, tout se passe bien. Des entrées de formulaire sont proposées pour les champs vatnumber (un champs texte) et vatcountry (une dropdown). Quand je save le client, la validation (le méthode Mage_VatCustomer_Model_Entity_Customer_Attribute_Backend_Vatnumber::beforeSave($object)) est bien appelée. Et les attributs vatvalid et vatlastcheck sont bien valués (du moins en DB).
J’ai ajouté des champs de formulaire pour les deux attributs dans les templates d’inscription et d’edition. Les client peuvent donc leur donner une valeur à l’inscription et les modifier comme le reste de leur attributs. Là par contre ça coince, les valeurs des attributs sont bien changées, mais mon code de validation n’est pas appelé.
Je vois deux possibilité:
- Soit une ‘backend_class’ n’est pas utilisée lors de l’entrée de données via l’interface “frontend” Si tel est le cas, où et comment faire la validation de données entrées via “frontend”.
- Soit le code devrait être appelé et c’est un bug.
|