In my Zend application i have separate models for each table and data is retrieved using TableGateway
Now i need to implement form to create edit page. I could able to create form of one table/model as mentioned in http://framework.zend.com/manual/2.2/en/user-guide/forms-and-actions.html
Here is my edit action -
public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('candidate', array(
'action' => 'index'
));
}
try {
$candidate = $this->getCandidateTable()->getCandidate($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('candidate', array(
'action' => 'index'
));
}
$form = new CandidateForm();
$form->bind($candidate);
$form->get('submit')->setAttribute('value', 'Edit');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($candidate->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->getCandidateTable()->saveCandidate($candidate);
return $this->redirect()->toRoute('candidate');
}
}
return array(
'id' => $id,
'form' => $form,
);
}
edit view -
<?php
$title = 'Edit Candidate';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url(
'candidate',
array(
'action' => 'edit',
'id' => $this->id,
)
));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('title'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
This edit action binds a form with one table (CandidateTable). But in my application, that page has data from multiple tables (CandidateSkills, CandidateQualifications etc). When i click submit it should save data in separate tables.
You can use setFromArray
You fetch the row object and than just setFromArray and save, commit.
To populate form value use populate method see this Populating and Retrieving Values
$form = new My_Form_Edit();
if( !$this->getRequest()->isPost() )// If the form isn't posted than populate the value
{
$form->populate($myArrayValueToPopulate);//$myArrayValueToPopulate this is your array to populate for the form
return;
}
// Than check validation and save data
For zend framework 2 you can use bind to populate data Binding an object
straight from documentation
When you bind() an object to the form, the following happens:
The composed Hydrator calls extract() on the object, and uses the values returned, if any, to populate the value attributes of all elements. If a form contains a fieldset that itself contains another fieldset, the form will recursively extract the values.
When isValid() is called, if setData() has not been previously set, the form uses the composed Hydrator to extract values from the object, and uses those during validation.
If isValid() is successful (and the bindOnValidate flag is enabled, which is true by default), then the Hydrator will be passed the validated values to use to hydrate the bound object. (If you do not want this behavior, call setBindOnValidate(FormInterface::BIND_MANUAL)).
If the object implements Zend\InputFilter\InputFilterAwareInterface, the input filter it composes will be used instead of the one composed on the form.
This is easier to understand in practice.
$contact = new ArrayObject;
$contact['subject'] = '[Contact Form] ';
$contact['message'] = 'Type your message here';
$form = new Contact\ContactForm;
$form->bind($contact); // form now has default values for
// 'subject' and 'message'
$data = array(
'name' => 'John Doe',
'email' => 'j.doe#example.tld',
'subject' => '[Contact Form] \'sup?',
);
$form->setData($data);
if ($form->isValid()) {
// $contact now looks like:
// array(
// 'name' => 'John Doe',
// 'email' => 'j.doe#example.tld',
// 'subject' => '[Contact Form] \'sup?',
// 'message' => 'Type your message here',
// )
// only as an ArrayObject
}
Related
I am using the MultiForm module to submit a long form with SilverStripe. The logic for this form is in 'CampaignBriefForm.php' whereas the gridfield CMS field is being added in 'CampaignBriefPage.php'. I have a Data Object for a CampaignBriefLead which is what the form creates.
Campaign Brief Page
private static $has_many = array(
'CampaignBriefLeads' => 'CampaignBriefLead'
);
public function CampaignBriefForm() {
return new CampaignBriefForm($this, 'CampaignBriefForm');
}
Campaign Brief Lead (DO)
private static $has_one = array( "Page" => "CampaignBriefPage" );
As you can see the Campaign Brief page has the correct relationship with the Data Object and also you can see the the form itself (done in a sepearate file) is correctly returning (as it's being saved in the DB). For some reason however, the gridfield will not show me what is in the database for that Data Object. The grid field code is as follows.
$fields = parent::getCMSFields();
$contactConfig = GridFieldConfig_RelationEditor::create();
$contactConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(
array(
'CompanyName' => 'Company Name',
'StartDate' => 'Start Date',
'Duration' => 'Duration',
'WebsiteURL' => 'Website',
'Budget' => 'Budget'
));
$contactGrid = new GridField(
'CampaignBrief',
'Campaign Enquiries',
$this->CampaignBriefLeads(),
$contactConfig
);
$fields->addFieldToTab("Root.Enquiries", $contactGrid);
To me this all looks correct and should work but for some reason it is not working.
Note
The link existing option on the gridfield allows me to link one of the entries from the DO with the gridfield weirdly?? So it saves one entry but I have to do it manually, this tells me it can see the DB but won't pull for some reason.
For reviewing reasons, here is the code for the multiform where the campaign brief lead is actually saved to the DB after the form is submitted.
public function finish($data, $form) {
parent::finish($data, $form);
$steps = DataObject::get(
'MultiFormStep',
"SessionID = {$this->session->ID}"
);
$enquiry = new CampaignBriefLead();
foreach($steps as $step) {
$data = $step->loadData();
foreach($data as $key => $value) {
if($key == 'url' || $key == 'MultiFormSessionID' || $key == 'action_finish') {
continue;
}
if(isset($data[$key])) {
$enquiry->$key = $data[$key];
error_log($data[$key]);
}
}
}
$enquiry->write();
$this->controller->redirect('/campaign-brief/');
}
If you need anything more let me know. Thanks.
I would take a guess that the CampaignBriefLead PageID is not being set on your form submission.
Check the CampaignBriefLead table in your database and check the PageID column. If it is blank, null or 0 for each row then it is not being set.
One way to fix this problem for any new submission is to set the PageID for the $enquiry:
public function finish($data, $form) {
// ...
$enquiry = new CampaignBriefLead();
if ($campaignBriefPage = CampaignBriefPage::get()->first()) {
$enquiry->PageID = $campaignBriefPage->ID;
}
// ...
}
For the existing entries you will need to update the entries to have the correct PageID.
I am trying to build a simple test application using Zend Framework 1.12.7. I have the following code to create a form in a file in the forms folder called Album.php:
class Application_Form_Album extends Zend_Form
{
public function init()
{
$this->setName('album');
$id = new Zend_Form_Element_Hidden('id');
$id->addFilter('Int');
$artist = new Zend_Form_Element_Text('artist');
$artist->setLabel('Artist')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpy');
$title = new Zend_Form_Element_Text('title');
$title->setLabel('Title')
->setRequired(true)
->addFilter('StripTags')
->addFilter('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElements(array($id,$artist,$title,$submit));
}
}
I then added the following code to my IndexController addAction():
public function addAction()
{
$form = new Application_Form_Album();//Create the Form Object
$form->submit->setLabel('Add');//Set label of Submit button
$this->view->form = $form;//Assign view for rendering
if($this->getRequest()->isPost()){//If TRUE then form submitted
$formData = $this->getRequest()->getPost();
if($form->isValid($formData)){//If VALUD then add record
$artist = $form->getValue('artist');
$title = $form->getValue('title');
$albums = new Application_Model_DbTable_Albums();
$albums->addAlbum($artist, $title);
$this->_helper->redirector('index');//return to homepage
}else{
$form->populate($formData);//INVALID so repopulate the form
}
}
}
Then in my view add.phtml I added this code:
<?php
$this->title = "Add new album";
$this->headTitle($this->title);
echo $this->form;
I am getting no errors and the title "Add new album" shows when I am on this URL
http://localhost/zf-tutorial/public/index/add
does anyone know why my form is not displaying???
Ok I've figured it out, and it might be useful for anyone who is just starting to use Zend Like myself. Anyways as of Zend Framework v1.12 the correct way to create a form is by using the addElement method for each element of the form. So the correct way to render my form included in my original post is like this:
public function init()
{
$this->setName('album');
$this->setMethod('post');
$this->addElement('hidden', 'id', array(
'filters'=>array('Int')
));
$this->addElement('text', 'artist', array(
'label' => 'Artist',
'required'=> true,
'filters'=>array('StringTrim','StripTags'),
'validators'=>array('NotEmpty')
));
$this->addElement('text', 'title', array(
'label' => 'Title',
'required'=> true,
'filters'=>array('StripTags'),
'validators'=>array('NotEmpty')
));
$this->addElement('submit', 'submit', array(
'ignore'=>true,
'label'=>'Create'
));
}
Your form creation and handling differs from mine in only two ways:
I typically set the form in the view at the bottom, after all the checks and processing, just before exiting the method.
I do not believe that it is necessary for you have the else clause in which you populate the form with the $formData. The call to isValid($formData) "essentially" does that.
Still hard to see why either of these would cause your form not to render, but worth a try.
I have created a module in SocialEngine(*which is built on Zend framework v1.9) that contains an admin form with a few options.
The problem I have with it is that it seems to no get the values of the fields from database after I refresh the page and it shows me the default values.
It shows the correct values immediately after I save(*but I am not sure if the page is refreshed after saving), but not after I refresh.
controller /application/modules/Mymodule/controllers/AdminSomesettingsController.php :
class Mymodule_AdminSomesettingsController extends Core_Controller_Action_Admin
{
public function indexAction()
{
$this->view->form = $form = new Mymodule_Form_Admin_Someform();
$settings = Engine_Api::_()->getApi('settings', 'core');
if(!$form->isValid($settings->mymodule))
{ return $form->populate($settings->mymodule); }
if( !$this->getRequest()->isPost() ) { return; }
if( !$form->isValid($this->getRequest()->getPost()) ) { return; }
$db = Engine_Api::_()->getDbTable('settings','core')->getAdapter();
$db->beginTransaction();
try {
$values = $form->getValues();
$settings->mymodule = $values;
$db->commit();
} catch( Exception $e ) {
$db->rollback();
throw $e;
}
$form->saveValues();
$form->addNotice('Your changes have been saved.');
}
}
form /application/modules/Mymodule/Form/Admin/Someform.php :
class Mymodule_Form_Admin_Someform extends Engine_Form
{
public function init()
{
$this
->setTitle('My Settings')
->setDescription('Settings');
$this->addElement('Radio', 'some_setting', array(
'label' => 'Some Setting',
'description' => '',
'multiOptions' => array(
0 => 'Option One',
1 => 'Option Two',
2 => 'Option Three',
),
'value' => 1,
'escape' => false,
));
// Add submit button
$this->addElement('Button', 'submit', array(
'label' => 'Save Changes',
'type' => 'submit',
'ignore' => true
));
}
public function saveValues()
{
}
}
I have checked with other plugins and it seems to me that $form->populate($settings->mymodule); repopulates the form after refresh, but it does not work for me.
Any idea how I could make it show the values from the database(*when these values exist) instead of the default values?
I myself am new to socialengine and zend.My understanding of socialengine says, make a function saveValues() inside ur form class, then call it from controller action as $form->saveValues(),passing parameter as needed.This is the convention that socialengine seems to follow, and inside the saveValues() of form class,u can save valus as needed.Ur form shud be populated only if validation fails
(!$form->isValid($formData ))
{ return $form->populate($formData); }
Instead of default adapter,U should try this-
$db =Engine_Api::_()->getDbTable('settings','core')->getAdapter(),
$db->beginTransaction();
If u want to set the value of a particular field try - $form->populate(array('formField'=>'urValue')); in ur case maybe -
$val=$settings->mymodule,
$form->populate('formField'=>$val);
You can add the code in controller $form->some_setting->setValue('1');
I have a form for my entity called Book and I have a type to display a form in my view. In this type I have some fields that are mapped to properties in my entity.
Now I want to add another field which is not mapped in my entity and supply some initial data for that field during form creation.
My Type looks like this
// BookBundle\Type\Book
public function buildForm(FormBuilderInterface $builder, array $options = null)
{
$builder->add('title');
$builder->add('another_field', null, array(
'mapped' => false
));
}
The form is created like this
$book = $repository->find(1);
$form = $this->createForm(new BookType(), $book);
How can I supply some initial data now during form creation? Or how do I have to change that creation of the form to add initial data to the another_field field?
I also have a form that has fields that mostly match a previously defined entity, but one of the form fields has mapped set to false.
To get around this in the controller, you can give it some initial data pretty easily like this:
$product = new Product(); // or load with Doctrine/Propel
$initialData = "John Doe, this field is not actually mapped to Product";
$form = $this->createForm(new ProductType(), $product);
$form->get('nonMappedField')->setData($initialData);
simple as that. Then when you're processing the form data to get ready to save it, you can access the non-mapped data with:
$form->get('nonMappedField')->getData();
One suggestion might be to add a constructor argument (or setter) on your BookType that includes the "another_field" data, and in the add arguments, set the 'data' parameter:
class BookType
{
private $anotherFieldValue;
public function __construct($anotherFieldValue)
{
$this->anotherFieldValue = $anotherFieldValue;
}
public function buildForm(FormBuilderInterface $builder, array $options = null)
{
$builder->add('another_field', 'hidden', array(
'property_path' => false,
'data' => $this->anotherFieldValue
));
}
}
Then construct:
$this->createForm(new BookType('blahblah'), $book);
You can change the request parameters like this to support the form with additional data:
$type = new BookType();
$data = $this->getRequest()->request->get($type->getName());
$data = array_merge($data, array(
'additional_field' => 'value'
));
$this->getRequest()->request->set($type->getName(), $data);
This way your form will fill in the correct values for your field at rendering. If you want to supply many fields this may be an option.
How can I pass the model in array format.
I want to pass models in this format from controller to view:-
Users[user_contact]=Contact
Users[user_contact][contat_city]=City
Users[user_contact][contact_state]=state
This is what I am doing
public function actionCreate() {
$user = new Users;
$presContact = new Contacts;
$presCity = new Cities;
$presState = new States;
$contactArr = array();
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if (isset($_POST['Users'])) {
$transaction = CActiveRecord::getDBConnection()->beginTransaction();
$contactArr = CommonFunctions::saveContact($_POST['Users']['user_pres_contact'],'user_pres_contact',$errorArr);
$presContact = $contactArr['contact'];
$presCity = $contactArr['city'];
$presState = $contactArr['state'];
$user->attributes = $_POST['Users'];
$user->user_pres_contact_id = $presContact->contact_id;
if($user->save()){
$transaction->commit();
$this->redirect(array('view', 'id' => $user->user_id));
} else {
$transaction->rollback();
}
}
$this->render('createUser', array(
'Users' => $user,
'Users[\'user_pres_contact\']'=>$presContact,
'Users[\'user_pres_contact\'][\'contact_city\']'=>$presCity,
'Users[\'user_pres_contact\'][\'contact_state\']'=>$presState,
));
}
I am able to access only $users but
I m not able to access $Users['user_pres_contact'] in the view
That's because you are assigning them as strings...
The correct way of doing things would be (btw, what you are asking for can't done literally, it is impossible to assign 2 values to one key):
$user = array(
'user_press_contact' => array(
'contact' => $presContact,
'city' => $presCity,
'state' => $presState,
),
);
$this->render('createUser', array(
'Users' => $user,
));
It will give you $Users['user_press_contact']['contact'] for the name in the view, etc.
You can use
$user->getAttributes() //it returns an array of data.
Hope that's usefull
It is possible to solve this using model relations? You can define a relation from the User model to the City model (e.g. naming it relation_to_city), then you can just assign the user model in the controller
$this->render('view', 'user'=>$user);
and access the city (from the view)
$user->relation_to_city