My zend framework 2 project concerns an online restaurant Menu and I am trying to make a form to add Pizzas to database. But something is wrong with my code. The form won't show up, instead this error shows up:
No element by the name of [pizza_name] found in form
Please help me to find what's wrong with my code.
Here are my files:
addPizzaForm.php:
<?php
namespace Pizza\Form;
use Zend\Form\Form;
use Pizza\Form\AddPizzaForm;
use Pizza\Model\Pizza;
class AddPizzaForm extends Form
{
public function construct() {
parent:: construct('addpizzaform');
$this->add(array(
'name' => 'pizza_name',
'type' => 'text',
'options' => array(label => 'Pizza name')));
$this->add(array(
'name' => 'ingredients',
'type' => 'textarea',
'options' => array(label => 'Ingredients')));
$this->add(array(
'name' => 'small_price',
'type' => 'text',
'options' => array(label => 'Small price')));
$this->add(array(
'name' => 'big_price',
'type' => 'text',
'options' => array(label => 'Big price')));
$this->add(array(
'name' => 'family_price',
'type' => 'text',
'options' => array(label => 'Family price')));
$this->add(array(
'name' => 'party_price',
'type' => 'text',
'options' => array(label => 'Party price')));
$this->add(array(
'name' => 'add_pizza',
'type' => 'submit',
'attributes' => array(
'value' => 'Add New Pizza',
'id' => 'submitbutton')));
}
}
add.phtml view:
<?php
echo $this->headTitle('Add new Pizza');
?>
<div class="row">
<div class="col-md-8"> <h1> Add new Pizza </h1>
</div>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('pizza',array('action'=>'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formRow($form->get('pizza_name')) . "</br>";
echo $this->formRow($form->get('ingredients')) . "</br>";
echo $this->formRow($form->get('small_price')) . "</br>";
echo $this->formRow($form->get('big_price')) . "</br>";
echo $this->formRow($form->get('family_price')) . "</br>";
echo $this->formRow($form->get('party_price')) . "</br>";
echo $this->formRow($form->get('add_pizza')) . "</br>";
echo $this->form()->closeTag();
?>
PizzaController.php:
<?php
namespace Pizza\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Pizza\Form\AddPizzaForm;
use Pizza\Model\Pizza;
class PizzaController extends AbstractActionController {
protected $pizzaTable;
public function addAction()
{
$add_form = new AddPizzaForm();
$request = $this->getRequest();
if($request->isPost())
{
$pizza = new Pizza();
$add_form->setInputFilter($pizza->getInputFilter());
$add_form->setData($request->getPost());
if($form->isValid())
{
$pizza->exchangeArray($form->getData());
$this->getPizzaTable()->save($pizza);
}
return $this->redirect()->toRoute('pizza');
}
return array('form' => $add_form);
}
public function getPizzaTable()
{
if( !$this->pizzaTable)
{
$sm = $this->getServiceLocator();
$this->pizzaTable = $sm->get('Pizza\Model\PizzaTable');
}
return $this->pizzaTable;
}
}
Welcome to stackoverflow!
You're getting that strange errors because there are FOUR important details that needs attention.
A - Your construct() method in AddPizzaForm has wrong name and signature. You have to rename construct to __construct change contents something like below:
class AddPizzaForm extends Form
{
public function __construct($name = null, $options = array() )
{
$formName = is_null($name) ? 'addpizza-form' : $name;
parent::__construct($formName, $options);
}
}
B - Create an init() method in your form and add your form elements inside that. This detail is clearly stated in official documentation:
If you are creating your form class by extending Zend\Form\Form, you
must not add the custom element in the __construct but rather in the init() method.
So, in your case:
class AddPizzaForm extends Form
{
// constructor etc..
public function init()
{
$this->add(array(
'name' => 'pizza_name',
'type' => 'text',
'options' => array(label => 'Pizza name')
)
);
// ... add other for elements ...
}
}
C. Finally, in your controller, you're trying to instantiate your form manually:
$add_form = new AddPizzaForm();
This is also bad practice. You need to get your AddPizzaForm instance from ServiceManager. This detail also stated in documentation:
You must not directly instantiate your form class, but rather get an
instance of it through the Zend\Form\FormElementManager.
To do that, introduce your AddPizzaForm in module.config.php like below:
'form_elements' => array(
'invokables' => array(
'add-pizza-form' => 'Pizza\Form\AddPizzaForm',
)
)
And grab it in your controller like this:
$add_form = $this->getServiceLocator()->get('FormElementManager')->get('add-pizza-form');
D - The last thing is; please read most of the documentation first before making some experiments. Reading documentation is most important step to getting better in any language or framework.
Hope it helps. Happy coding!
Related
I Have been following zf2 guide for blog I have created everything Controller, Factory, Form, Mapper, Model, Service, view etc
In my form I have a select element
$this->add(array(
'type' => 'select',
'name' => 'roleId',
'attributes' => array(
'id' => 'roleId',
'options' => array(
'1' => 'Admin',
'2' => 'Manager',
),
),
'options' => array(
'label' => 'Role',
),
));
Now in this form I want to load the option for the role from the database.
I tried loading the option by creating a simple function, which can be accessed in the element as below, but Am not able to fetch the result. I have already created Controller, Factory, Form, Mapper, Model, Service and view, Where I can do CRUD operation on Role.
$this->add(array(
'type' => 'select',
'name' => 'roleId',
'attributes' => array(
'id' => 'roleId',
'options' => $this->getAllRoles(),
),
'options' => array(
'label' => 'Role',
),
));
public function getAllRoles()
{
$roles = $this->getServiceLocator()->get('Admin\Service\RoleService');
$allRoles = $this->getAllTheRoles();
return $allroles;
}
Can anybody guide me how can I load all the Roles in option as listed in the IndexAction following Blog Post with ID and Name of the Role.
You could create a reusable form element that is pre-populated with the roles. To do so you must register the service with the form element manager in module.config.php.
return [
'form_elements' => [
'factories' => [
'RoleSelect' => 'MyModule\Form\Element\RoleSelectFactory',
],
],
];
There is not need to extend the standard select class as the changes are configuration only. This is best done in a factory class.
namespace MyModule\Form\Element;
use Zend\Form\Element\Select;
class RoleSelectFactory
{
public function __invoke($formElementManager, $name, $rname)
{
$select = new Select('role_id');
$select->setOptions(['label' => 'Role']);
$select->setAttributes(['id' => 'role_id']);
$serviceManager = $formElementManager->getServiceLocator();
$roleService = $serviceManager->get('Admin\Service\RoleService');
$options = [];
foreach($roleService->getAllTheRoles() as $role){
$options[$role->getId()] => $role->getName();
}
$select->setValueOptions($options);
return $select;
}
}
Adding the element within a form can then be updated to use the name of the service we registered.
$this->add([
'name' => 'role_id'
'type' => 'RoleSelect',
]);
One important point to remember is that the form using this element must be created using the $formElementManager->get('FormWithRoleSelect').
Finally found out the simple way to do this, Am really not sure if this is the correct way.
Added the Role service in the User Controller
Code in my userController.php
use Admin\Service\RoleServiceInterface;
class UserController extends AbstractActionController
{
/**
* #var \Admin\Service\UserServiceInterface
*/
protected $userService;
protected $roleService;
protected $userForm;
public function __construct(
UserServiceInterface $userService,
RoleServiceInterface $roleService,
FormInterface $userForm
)
{
$this->userService = $userService;
$this->roleService = $roleService;
$this->userForm = $userForm;
}
public function addAction()
{
$request = $this->getRequest();
$roles = $this->roleService->findAllRoles();
foreach ($roles as $role) {
if($role->getStatus() == 1) {
$allRoles[$role->getId()] = $role->getName();
}
}
$this->userForm->__construct(null, array('roleOptions'=>$allRoles));
}
}
My UserControllerFactory.php
<?php
namespace Admin\Factory;
use Admin\Controller\UserController;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class UserControllerFactory implements FactoryInterface
{
/**
* Create service
*
* #param ServiceLocatorInterface $serviceLocator
*
* #return mixed
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$realServiceLocator = $serviceLocator->getServiceLocator();
$userService = $realServiceLocator->get('Admin\Service\UserServiceInterface');
$roleService = $realServiceLocator->get('Admin\Service\RoleServiceInterface');
$userInsertForm = $realServiceLocator->get('FormElementManager')->get('Admin\Form\UserForm');
return new UserController(
$userService,
$roleService,
$userInsertForm
);
}
}
Finally the UserForm.php
<?php
namespace Admin\Form;
use Zend\Form\Form;
use Admin\Model\User;
use Zend\Stdlib\Hydrator\ClassMethods;
use Zend\Form\Element\Select;
class UserForm extends Form
{
public function __construct($name = null, $options = array())
{
$roleOptions = array();
if($options) {
$roleOptions = $options['roleOptions'];
}
parent::__construct($name, $options);
$this->setHydrator(new ClassMethods(false));
$this->setObject(new User());
$this->add(array(
'type' => 'hidden',
'name' => 'id'
));
$this->add(array(
'type' => 'select',
'name' => 'roleId',
'attributes' => array(
'id' => 'roleId',
'options' => $roleOptions
),
'options' => array(
'label' => 'Role',
),
));
}
}
This way using service manager I was successfully able to load the data in my for Select option.
Call getAllRoles() inside the controller then You can pass your custom array as parameter for form when you create form object. In form __construct function you can retrieve that array and set like this
'options' => $roles,
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'
)
));
}
}
You need to get the field set then the element so try:
echo $this->formRow($form->get('login-fieldset')->get('USER_LOGIN');
I am a Magento beginner so please bear with me...
I am creating a simple extension for my site to add a custom field to my Tags in adminhtml. The custom field is just a number which I need to identify a specific Z-block (cms block extension) so that I can access it as a widget and show it on the frontend in the Tag "category".
I have created a custom module which is working: I set a field in the form using $fieldset and have extended TagController.php, both of which are being used (I made a simple trial to see whether or not they had been recognized). However, I do not know how to go about saving my custom field to DB (whether amending saveAction is enough, and I haven't done it properly, or if I need to add a custom Model or sql install).
Sorry for the "basic" question but I'm new at this, and have mostly done frontend dev (so my extension knowledge is simply limited).
Thank you to anyone who can help...
Claudia
NEW TAG FORM:
public function __construct()
{
parent::__construct();
$this->setId('tag_form');
$this->setTitle(Mage::helper('tag')->__('Block Information'));
}
/**
* Prepare form
*
* #return Mage_Adminhtml_Block_Widget_Form
*/
protected function _prepareForm()
{
$model = Mage::registry('tag_tag');
$form = new Varien_Data_Form(
array('id' => 'edit_form', 'action' => $this->getData('action'), 'method' => 'post')
);
$fieldset = $form->addFieldset('base_fieldset',
array('legend'=>Mage::helper('tag')->__('General Information')));
if ($model->getTagId()) {
$fieldset->addField('tag_id', 'hidden', array(
'name' => 'tag_id',
));
}
$fieldset->addField('form_key', 'hidden', array(
'name' => 'form_key',
'value' => Mage::getSingleton('core/session')->getFormKey(),
));
$fieldset->addField('store_id', 'hidden', array(
'name' => 'store_id',
'value' => (int)$this->getRequest()->getParam('store')
));
$fieldset->addField('name', 'text', array(
'name' => 'tag_name',
'label' => Mage::helper('tag')->__('Tag Name'),
'title' => Mage::helper('tag')->__('Tag Name'),
'required' => true,
'after_element_html' => ' ' . Mage::helper('adminhtml')->__('[GLOBAL]'),
));
$fieldset->addField('zblock', 'text', array(
'name' => 'zblock_id',
'label' => Mage::helper('tag')->__('Z-Block Id'),
'title' => Mage::helper('tag')->__('Z-Block Id'),
'required' => true,
'after_element_html' => ' ' . Mage::helper('adminhtml')->__('[GLOBAL]'),
));
$fieldset->addField('status', 'select', array(
'label' => Mage::helper('tag')->__('Status'),
'title' => Mage::helper('tag')->__('Status'),
'name' => 'tag_status',
'required' => true,
'options' => array(
Mage_Tag_Model_Tag::STATUS_DISABLED => Mage::helper('tag')->__('Disabled'),
Mage_Tag_Model_Tag::STATUS_PENDING => Mage::helper('tag')->__('Pending'),
Mage_Tag_Model_Tag::STATUS_APPROVED => Mage::helper('tag')->__('Approved'),
),
'after_element_html' => ' ' . Mage::helper('adminhtml')->__('[GLOBAL]'),
));
$fieldset->addField('base_popularity', 'text', array(
'name' => 'base_popularity',
'label' => Mage::helper('tag')->__('Base Popularity'),
'title' => Mage::helper('tag')->__('Base Popularity'),
'after_element_html' => ' ' . Mage::helper('tag')->__('[STORE VIEW]'),
));
if (!$model->getId() && !Mage::getSingleton('adminhtml/session')->getTagData() ) {
$model->setStatus(Mage_Tag_Model_Tag::STATUS_APPROVED);
}
if ( Mage::getSingleton('adminhtml/session')->getTagData() ) {
$form->addValues(Mage::getSingleton('adminhtml/session')->getTagData());
Mage::getSingleton('adminhtml/session')->setTagData(null);
} else {
$form->addValues($model->getData());
}
$this->setForm($form);
return parent::_prepareForm();
}
NEW CONTROLLER:
public function saveAction()
{
if ($postData = $this->getRequest()->getPost()) {
if (isset($postData['tag_id'])) {
$data['tag_id'] = $postData['tag_id'];
}
$data['name'] = trim($postData['tag_name']);
$data['zblock'] = $postData['zblock_id'];
$data['status'] = $postData['tag_status'];
$data['base_popularity'] = (isset($postData['base_popularity'])) ? $postData['base_popularity'] : 0;
$data['store'] = $postData['store_id'];
if (!$model = $this->_initTag()) {
Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Wrong tag was specified.'));
return $this->_redirect('*/*/index', array('store' => $data['store']));
}
$model->addData($data);
if (isset($postData['tag_assigned_products'])) {
$productIds = Mage::helper('adminhtml/js')->decodeGridSerializedInput(
$postData['tag_assigned_products']
);
$model->setData('tag_assigned_products', $productIds);
}
try {
$model->save();
Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('The tag has been saved.'));
Mage::getSingleton('adminhtml/session')->setTagData(false);
if (($continue = $this->getRequest()->getParam('continue'))) {
return $this->_redirect('*/tag/edit', array('tag_id' => $model->getId(), 'store' => $model->getStoreId(), 'ret' => $continue));
} else {
return $this->_redirect('*/tag/' . $this->getRequest()->getParam('ret', 'index'));
}
} catch (Exception $e) {
Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
Mage::getSingleton('adminhtml/session')->setTagData($data);
return $this->_redirect('*/*/edit', array('tag_id' => $model->getId(), 'store' => $model->getStoreId()));
}
}
return $this->_redirect('*/tag/index', array('_current' => true));
}
The custom field I'm trying to add is "zblock"...thanks and, again, bear with me! :)
First add the field in database table.
For example if you want to add in your custom table.
ALTER TABLE myCustomModuleTable ADD COLUMN 'myCustomField' int(10);
Thenafter, In your controller action take the model object of that table and set the field.
If you are adding data in existing table row:
$value = 6;
$rowInWhichIWantToSave = Mage:getModel('companyname/modulename')->load($rowId);
$rowInWhichIWantToSave->setData('myCustomField',$value)->save();
If you are adding a new row:
$value = 6;
$rowInWhichIWantToSave = Mage:getModel('companyname/modulename');
$rowInWhichIWantToSave->setData('myCustomField',$value)->save();
Hope this helps!!
I am trying to set up an edit form for an entity (Place) in my app. For this purpose I have written the form (PlaceForm), fieldset (PlaceFieldset), .phtml pages and functions in the controller. Since I am new in zf2, I have concentrated on the tutorlias on their homepage. I have seen that from validation is provided by the framework but in my case it is not necessary. None of the fields added in the fieldset have not the required attribute, however when I submit the form under all empty fields I get the message "Value is required and can't be empty" although it is actually not required. I do not want to create a wall of code in this question, so I will start with the function from the controller. If there is someone who would like to help me with this issue, just let me know and I will update any other part of the code (form, fieldset etc.) Thx in advance.
...
public function editPlaceAction() {
$id = $this->params()->fromRoute('id');
$uiServiceProvider = $this->getServiceLocator()->get('FamilyTree\Service\UiServiceProvider');
$placeArray = $uiServiceProvider->fetchSinglePlaceById($id);
$place = new Places($placeArray);
$form = new PlaceForm();
$form->bind($place);
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
var_dump($place);
}
}
$view = new ViewModel(array(
'form' => $form,
'title'=>"Edit place"
));
$view->setTemplate("family-tree/family-tree-maintenance/editPlace");
return $view;
}
...
and here you can see how my Fieldset looks like
<?php
namespace Places\Form;
use Zend\Form\Fieldset;
class PlaceFieldset extends Fieldset {
public function __construct() {
parent::__construct('place');
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'name',
'type' => 'Text',
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'admUnit1',
'type' => 'Text',
'options' => array(
'label' => 'AdmUnit1',
),
));
}
}
I am working on an edit screen for my grid row. This is what I have so far for this form:
<?php
class Intellibi_Integration_Block_Adminhtml_Manageasendiapickinglists_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('integration_form', array(
'legend' => Mage::helper('integration')->__('Asendia Pick Information')
));
$fieldset->addField('order_number', 'label', array(
'label' => Mage::helper('integration')->__('Order Number'),
'name' => 'order_number'
));
// snipped
$fieldset->addField('pick_status', 'select', array(
'required' => false,
'class' => 'required-entry',
'label' => Mage::helper('integration')->__('Pick Status'),
'name' => 'pick_status',
'values' => Mage::getSingleton('ibi/asendiapickstatus')->getOptionArray(),
'readonly' => 'readonly'
));
// snipped
return parent::_prepareForm();
}
}
This produces the following output in the admin backend:
What I would like to do is change the pick_status column from a select to a label. When I do this, instead of showing the status value "New" it shows the array index like this:
My option array for asendiapickstatus is defined like this in my model:
class Intellibi_Integration_Model_Asendiapickstatus extends Varien_Object
{
const PICK_STATUS_NEW = 1;
const PICK_STATUS_SENT = 2;
const PICK_STATUS_SHIPPED = 3;
static public function getOptionArray()
{
return array(
self::PICK_STATUS_NEW => Mage::helper('integration')->__('New'),
self::PICK_STATUS_SENT => Mage::helper('integration')->__('Sent'),
self::PICK_STATUS_SHIPPED => Mage::helper('integration')->__('Shipped')
);
}
}
So my question is; on the edit form fieldset builder, how do I show the dropdown field "pick_status" value, rather than the current index it's at? So the output will say "New" instead of "1" as shown above. Will I need a custom renderer?
I've solved it like this (with a custom form rendered element):
Added custom fieldset type
$fieldset->addType('pickstatus', 'Intellibi_Integration_Block_Adminhtml_Manageasendiapickinglists_Edit_Tab_Form_Renderer_Fieldset_Pickstatus');
Used the fieldset like this
$fieldset->addField('pick_status', 'pickstatus', array(
'label' => Mage::helper('integration')->__('Pick Status'),
'name' => 'pick_status',
));
Coded the rendered like this
class Intellibi_Integration_Block_Adminhtml_Manageasendiapickinglists_Edit_Tab_Form_Renderer_Fieldset_Pickstatus extends Varien_Data_Form_Element_Abstract
{
protected $_element;
public function getElementHtml()
{
// Load Pick Status
$pick_status = (int)$this->getValue();
$pick_status_list = Mage::getSingleton('ibi/asendiapickstatus')->getOptionArray();
// Finish
return array_key_exists($pick_status, $pick_status_list) ? $pick_status_list[$pick_status] : 'Unknown';
}
}
And it renders like this