1

I have done client-side validation but how to do the server side validation for the login form.

2 Answers 2

0

You can write your custom controller to post data and validate it in execute method when submit.

For example:

<?php
namespace [Vendor]\[Module]\Controller\Account;  

use Magento\Customer\Api\AccountManagementInterface;  

class customPost{
    ...
    protected $customerAccountManagement;
    ...
    public function __construct(
        AccountManagementInterface $customerAccountManagement
    ){
        $this->customerAccountManagement = $customerAccountManagement;
        .....
    }

    public function execute()
    { 
        if ($this->getRequest()->isPost()) {
            $login = $this->getRequest()->getPost('login');
            if (!empty($login['username']) && !empty($login['password'])) {
                try {
                        $customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
                        ...
                    }
            }
        }
    }
}

For reference you can check core file:

/var/www/html/magento/vendor/magento/module-customer/Controller/Account/LoginPost.php

0

You can use plugin for that :

Write the following in your module's app/code/YourNamespace/YourModule/etc/di.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Controller\Account\LoginPost">
        <plugin name="yourModuleAccountLoginPost" type="YourNamespace\YourModule\Plugin\Customer\LoginPost" sortOrder="10" disabled="false"/>
    </type>
</config>

Create a new file app/code/YourNamespace/YourModule/Plugin/Customer/LoginPost.php and write the following code in it.

<?php

namespace YourNamespace\YourModule\Plugin\Customer;

class LoginPost
{
    public function aroundExecute(\Magento\Customer\Controller\Account\LoginPost $subject, \Closure $proceed)
    {
    // Add your custom code before the original execute function
        $this->doSomethingBeforeExecute(); // call the original execute function
        $returnValue = $proceed();
        // your custom code after the original execute function
        if ($returnValue) { 
            $this->doSomethingAfterExecute();
        }
        return $returnValue;
    }
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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