I am trying to create a simple login form with an email field and password field. I am running into issues when trying to display individual fields in my view. The Album tutorial on Zends' website didn't use a FIELDSET
and the Blog tutorial only used echo $this->formCollection($form);
. So I assumed there wouldn't be much of a difference, and everything I find online suggests there isn't a difference in syntax. Everything I have seems to match up to the Blog and Album tutorials on Zends' website respectively as far as I can tell.
The error does not occur if I move my field definitions to my FORM
class (bypassing the FIELDSET
) or if I dump all of the fields using:
echo $this->formCollection($form);
This is the error I am getting:
No element by the name of [USER_LOGIN] found in form
I am trying to display a single field using:
echo $this->formRow($form->get('USER_LOGIN'));
Here is the result of the formCollection
call:
(*NOTE: I tried using "login-fieldset[USER_LOGIN]" in the $form->get()
call and got the same behavior)
<fieldset>
<fieldset>
<label>
<span>Username</span>
<input type="text" name="login-fieldset[USER_LOGIN]" value="">
</label>
<label>
<span>Password</span>
<input type="password" name="login-fieldset[USER_PWD]" value="">
</label>
</fieldset>
<input type="submit" name="submit" value="Login">
</fieldset>
Here is the relevant code:
CSAdmin\Controller\LoginController:
namespace CSAdmin\Controller;
use Zend\View\Model\ViewModel;
use Zend\Form\FormInterface;
class LoginController extends AdminController
{
protected $loginService;
protected $loginForm;
public function __construct(
\CSAdmin\Service\LoginServiceInterface $loginService,
FormInterface $loginForm) {
parent::__construct();
$this->loginService = $loginService;
$this->loginForm = $loginForm;
}
public function indexAction()
{
array_push($this->layoutVars['customStyles'], 'css/admin/form.css');
array_push($this->layoutVars['customStyles'], 'css/admin/styles.css');
$request = $this->getRequest();
$login = $this->loginService->findUser($this->params('USER_LOGIN'));
$this->loginForm->bind($login);
if ($request->isPost()) {
//Nothing here yet
}
//Override view to use predefined Admin Views
$view = new ViewModel(array('data'=>$this->data,
'form'=>$this->loginForm
));
$view->setTemplate('CSAdmin/login/login.phtml'); // path to phtml file under view folder
//Set the Admin Layout
$layout = $this->layout();
$layout->setVariable('layout', $this->layoutVars);
$layout->setTemplate('layout/CSAdmin/login.phtml');
//Render Page
return $view;
}
}
CSAdmin\Form\LoginForm:
namespace CSAdmin\Form;
use Zend\Form\Form;
use Zend\Stdlib\Hydrator\ClassMethods;
use \CSAdmin\Model\User;
class LoginForm extends Form
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);
$this->setHydrator(new ClassMethods(false));
$this->setObject(new User());
$this->add(array(
'name' => 'login-fieldset',
'type' => 'CSAdmin\Form\LoginFieldset',
'options' => array(
'use_as_base_fieldset' => true
)
));
$this->add(array(
'type' => 'submit',
'name' => 'submit',
'attributes' => array(
'value' => 'Login'
)
));
}
}
CSAdmin\Form\LoginFieldset:
namespace CSAdmin\Form;
use Zend\Form\Fieldset;
use Zend\Stdlib\Hydrator\ClassMethods;
use \CSAdmin\Model\User;
class LoginFieldset extends Fieldset
{
public function __construct($name = null, $options = array())
{
parent::__construct($name, $options);
$this->setHydrator(new ClassMethods(false));
$this->setObject(new User());
$this->add(array(
'type' => 'text',
'name' => 'USER_LOGIN',
'options' => array(
'label' => 'Username'
)
));
$this->add(array(
'type' => 'password',
'name' => 'USER_PWD',
'options' => array(
'label' => 'Password'
)
));
}
}