/** * Create service * * @param ServiceLocatorInterface $serviceLocator * * @return LoginForm */ public function createService(ServiceLocatorInterface $serviceLocator) { /** * @var $filter LoginFormInputFilter */ $filter = $serviceLocator->get(LoginFormInputFilter::class); $form = new LoginForm(); $form->setInputfilter($filter); return $form; }
public function testConstructor() { $this->assertInstanceOf(Form::class, $this->testedObj); $this->assertEquals($this->testedObj->count(), 4); $this->assertTrue($this->testedObj->has('login')); $this->assertTrue($this->testedObj->has('password')); $this->assertTrue($this->testedObj->has('csrf')); $this->assertTrue($this->testedObj->has('submit')); $loginInput = $this->testedObj->get('login'); $this->assertInstanceOf(Element\Text::class, $loginInput); $passwordInput = $this->testedObj->get('password'); $this->assertInstanceOf(Element\Password::class, $passwordInput); $csrfInput = $this->testedObj->get('csrf'); $this->assertInstanceOf(Element\Csrf::class, $csrfInput); $submitInput = $this->testedObj->get('submit'); $this->assertInstanceOf(Element\Submit::class, $submitInput); $this->assertEquals('Login', $submitInput->getValue()); }
public function indexAction() { if ($this->identity()) { return $this->redirect()->toRoute('home'); } /** @var \Zend\Http\Request $request */ $request = $this->getRequest(); try { if ($request->isPost()) { $this->form->setData($request->getPost()->toArray() ?: []); if ($this->form->isValid()) { if ($this->service->login($this->form->getInputFilter())) { $this->flashMessenger()->addSuccessMessage('User login successfully'); return $this->redirect()->toRoute('home'); } else { $this->form->get('password')->setValue(null); $this->flashMessenger()->addErrorMessage('User name/email or password are incorrect'); } } else { $this->form->get('password')->setValue(null); $this->flashMessenger()->addErrorMessage('Please fill form correctly'); } } } catch (\Exception $e) { $this->flashMessenger()->addErrorMessage('An unexpected error has occurred, please contact your system administrator'); } return ['form' => $this->form]; }