- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-10-2021
11:44 PM
02-10-2021
11:44 PM
Validation customer attribute
Hello everyone, i'm a newbie in magento 2 and i got a issues related in my task, hope you guys give to me some advices.
I created an customer eav attribute with name tenant_name and want to add validation to check tenant already exist in database or not when user register a new account.
i have has reserching and apply some solution but seems its run not correct, here is my code
images
<?php namespace Magenest\CustomerAttribute\Model\Customer\Attribute\Backend; use Magento\Framework\Exception\LocalizedException; class Tenant extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend { protected $_tenantFactory; /** * @param \Magento\Store\Model\StoreManagerInterface $storeManager */ public function __construct(\Magenest\CustomerAttribute\Model\TenantFactory $tenantFactory) { $this->_tenantFactory = $tenantFactory; } /** * @param \Magento\Framework\DataObject $object * @return void */ public function beforeSave($object) { $value = $object->getData($this->getAttribute()->getAttributeCode()); $tenant = $this->_tenantFactory->create(); $resultColections = $tenant->getCollection()->getData(); foreach ($resultColections as $keyValues) { # code... if($keyValues['tenant_name'] === $value){ throw new LocalizedException( __( 'Please regis other tenant name' ) ); } } } /** * @param \Magento\Framework\DataObject $object * @return void */ public function afterSave($object) { $value = $object->getData($this->getAttribute()->getAttributeCode()); $tenant = $this->_tenantFactory->create(); $data = [ 'tenant_name' => $value, 'status' => 1 ]; $tenant->addData($data)->save(); } }
and here issues occurs, alway in case 'Please regis other tenant name'
imag
So do i miss understood or missing any case? please help
Thanks for reading.
Labels: