I'm a Zend Framework newbie and I'm trying to retrieve some values to update the database. I have the following code in the controller. Populating the form works just fine, as does updating the db with some hard-coded values. My problem lies in trying to retrieve an updated value from the form, see the $first_name variable. It gives me a fatal error.
My question: How do I retrieve an updated value from the form?
public function editAction() {
$view = $this->view;
//get the application
$application_id = $this->getRequest()->getParam('id');
$application = $this->getApplication($application_id);
//get the applicant
$applicant_id = $application->applicant_id;
$applicant = $this->getApplicant($applicant_id);
if ($this->getRequest()->isPost()) {
if ($this->getRequest()->getPost('Save')) {
$applicants_table = new Applicants();
$first_name = $form->getValue('applicant_first_name');
$update_data = array ('first_name' => 'NewFirstName',
'last_name' => 'NewLastName');
$where = array('applicant_id = ?' => 16);
$applicants_table->update($update_data, $where);
}
$url = 'applications/list';
$this->_redirect($url);
} else { //populate the form
//applicant data
$applicant_data = array();
$applicant_array = $applicant->toArray();
foreach ($applicant_array as $field => $value) {
$applicant_data['applicant_'.$field] = $value;
}
$form = new FormEdit();
$form->populate($applicant_data);
$this->view->form = $form;
}
}
First off, your example has a problem here:
$first_name = $form->getValue('applicant_first_name');
...your $form isn't created yet, thus the fatal error; you are calling getValue() on a non-object.
Once you get that squared away, you can populate the form with posted data by calling the isValid method on the $form with request data. Here's a quick example:
// setup $application_data
$form = new FormEdit();
$form->populate($applicant_data);
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
$first_name = $form->getValue('applicant_first_name');
// save data to the database ...
}
}
Related
i am a newbie having difficulty in this. i did not getting a value of ID In A box for Update Inside A Box.
update code
<?php
require_once (__DIR__ . '/../../config.php');
require_once($CFG->dirroot.'/local/message/classes/form/edit.php');
global $DB;
$PAGE->set_url(new moodle_url('/local/message/update.php'));
$PAGE->set_context(\context_system::instance());
$PAGE->set_title('Edit');
//here display form
$mform=new edit();
$table='local_message';
$id=required_param('id', PARAM_INT);
$info=$DB->get_records($table,array('id' => $id));
if ($mform->is_cancelled()) {
print_r("1");
redirect($CFG->wwwroot.'/local/message/manage.php', 'You Cancelled The Form');
} else if ($fromform = $mform->get_data()) {
print_r("2");
$record = new stdClass();
$record->id=$id;
$record->messagetext = $fromform->messagetext;
$record->messagetype = $fromform->messagetype;
$DB->update_record($table, $record);
redirect($CFG->wwwroot . '/local/message/manage.php', 'you created a message with a title ' . $fromform->messagetext);
}
echo $OUTPUT->header();
$mform->display();
echo $OUTPUT->footer();
Form/moodle/Code
<?php
require_once("$CFG->libdir/formslib.php");
class edit extends moodleform {
public function definition() {
global $CFG;
$mform = $this->_form; // Don't forget the underscore!
$mform->addElement('text', 'id');
$mform->setType('id', PARAM_INT);
$mform->addElement('text', 'messagetext', get_string('message_text', 'local_message')); // Add elements to your form
$mform->setType('messagetext', PARAM_NOTAGS); //Set type of element
$mform->setDefault('messagetext', get_string('enter_message', 'local_message')); //Default value
$choices = array();
$choices['0'] = \core\output\notification::NOTIFY_WARNING;
$choices['1'] = \core\output\notification::NOTIFY_SUCCESS;
$choices['2'] = \core\output\notification::NOTIFY_ERROR;
$[enter image description here][1]choices['3'] = \core\output\notification::NOTIFY_INFO;
$mform->addElement('select', 'messagetype', get_string('message_type', 'local_message'), $choices);
$mform->setDefault('messagetype', '3');
$this->add_action_buttons();
}
//Custom validation should be added here
function validation($data, $files) {
return array();
}
}
You need to pass the id to the form before displaying the form
$mform->set_data($info);
$mform->display();
Also, you should use the single get_record function not the multiple get_records function
$info = $DB->get_record($table, array('id' => $id));
Also, you might want to use the hidden element for the id
$mform->addElement('hidden', 'id');
Does any one know if it is possible to have datetimepicker from date_time_selector function in moodle? I want the date/time selected from date_time_selector in a variable to store it into the database.
This is the code what I am working on right now in my edit.php , what am I missing
require_once('../../config.php');
require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->dirroot.'/blocks/myblcok/myblock_form.php');
global $DB;
require_login($SITE);
$block = new myblock();
$mform = new myblock_form();
if ($data = $mform->get_data()) {
$startdate = $data->startdate;
}
$dataobject = new stdClass;
$dataobject->name = get_string('entryname', 'myblock');
$dataobject->description = get_string('entrydescription',
'myblock');
$dataobject->course = $COURSE->id;
$dataobject->userid = $USER->id;
$dataobject->starttime = $data->date;
$DB->insert_record('event', $dataobject);
redirect($CFG->wwwroot.'/course/view.php?id='.$courseid, '', 0);
I'm presuming this is being used in a form? The return value from data_time_selector is a timestamp which can be stored in a database. eg:
In edit_form.php file
require_once($CFG->dirroot.'/lib/formslib.php');
class edit_form extends moodleform {
function definition () {
$mform =& $this->_form;
$mform->addElement('date_time_selector', 'mytime', get_string('mytime', 'mycomponent'));
}
}
In edit.php
require_once('edit_form.php');
$mform = new edit_form();
if ($data = $mform->get_data()) {
// This is a timestamp.
$mytime = $data->mytime;
}
EDIT:
Ah... you need to pass a data object to insert a record eg:
$newrecord = new StdClass();
$newrecord->mytime = $mytime;
$DB->insert_record('table', $newrecord);
Or if the form field names are the same as your field names, just use the $data object eg:
$DB->insert_record('table', $data);
Have a look through the database API :
http://docs.moodle.org/dev/Data_manipulation_API#Inserting_Records
I have a form in my project which is very simple.
It is a textbox with a label, that's it.
But when I try to get the data from those textboxes, it returns NULL.
get($id) and get($id)->getData(), both return NULL.
It's a form without any class attached to it, just to keep it simple.
The purpose of this form is to adjust a number.
What am I doing wrong? Or is there a better way to solve this?
public function makenAction()
{
$em = $this->getDoctrine()->getManager();
$orderregels = $this->getRequest()->getSession()->get('orderregelz')->getOrderregels();
$overzicht = $this->createFormBuilder();
foreach($orderregels as $value)
{
/*
* getting some values from database
*/
$overzicht->add($temp->getOrderregelD(),"text",array('label'=>$tmpcompleet));
}
$overzicht->add('Verzenden','submit');
$request = $this->getRequest();
if ($request->getMethod() == 'POST')
{
$data = array();
foreach ($orderregels as $value)
{
$data[] = $overzicht->get($value);
}
}
this is the way you get data from the request
GET
$value = $request->query->get("input_name");
POST
$value = $request->request->get("input_name");
Otherwise you can simply do:
$formData = $overzicht->getData();
and then access the values like:
$foo = $formData['foo'];
To directly access all the values of a specified form, do:
$formData = $this->getRequest()->request->get('form');
After all, you should to bind the request to form.
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
var_dump($editForm->getData()); die;
}
doc
I have a code here that, has to search and post back information selected in a table above the add button, the search works but im having a problem with the post back to the table function. These are the lines it shows to have errors.
C:\xampp\htdocs\portal-gep-2\application\models\ServiceProviders.php(68): Zend_Db_Table_Abstract->fetchRow(Object(Zend_Db_Table_Select))
public function getName($id)
{
$select = $this->select();
$select->where('service_provider_id = ?', $id);
$result = $this->fetchRow($select); //this line
return $result['service_provider_name'];
}
#6 C:\xampp\htdocs\portal-gep-2\application\modules\admin\controllers\AjaxController.php(1104): Model_ServiceProviders->getName(NULL)
public function postserviceproviderAction()
{
$form = new Form_IndustrialTable();
$this->view->form = $form;
if(!$form->isValid($_POST))
{
$values=$form->getValues();
}
$sp = $this->getRequest()->getPost('serviceprovider', null);
$mdlserviceprovider = new Model_ServiceProviders();
$serviceprovider = $mdlserviceprovider ->getName($id); //this line
$rtn_array= array( 'sp' => $sp,
'serviceprovider ' => $serviceprovider);
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
echo Zend_Json::encode($rtn_array);
}
You don't put any initial value to $id so it's null which causes error.
You want SQL query to look something like SELECT * FROM service_providers WHERE service_provider_id = 50, but for this you have to provide id which you want to find (50 in this example). You need to add some value to variable $id before using in $select->where('service_provider_id = ?', $id);, but your code you never put any value to variable $id.
If I'm guessing your idea then you need to change line:
$serviceprovider = $mdlserviceprovider ->getName($id);
to:
$serviceprovider = $mdlserviceprovider ->getName($sp);
Also this part of your code probably unnecessary as it does nothing:
$form = new Form_IndustrialTable();
$this->view->form = $form;
if(!$form->isValid($_POST))
{
$values=$form->getValues(); //you never use $values
}
I am new to Zend framework and I am trying to update data in database and grid, but instead of updating particular row all rows are getting updated. please help me with this.
Here is my controller code.
public function editAction()
{
$form = new Application_Form_user();
$this->view->form = $form;
if($this->getRequest()->isPost())
{
$formData= $this->getRequest()->getPost();
if($form->isvalid($formData))
{
$client= new Application_Model_DbTable_Client();
$firstname = $formData['firstname'];
$lastname = $formData['lastname'];
$email = $formData['email'];
$client->updateClient('Id',$firstname,$lastname,$email);
$this->_helper->redirector('index');
}
else
{
$form->populate($formData);
}
}
else
{
$id=$this->getRequest()->getparam('id');
if($id>0)
{
$client= new Application_Model_DbTable_Client();
$clients = $client->getClient('Id');
$form->populate($clients[0]);
}
}
}
And here is my model code.
public function updateClient($id,$firstname,$lastname,$email)
{
$data=array('firstname'=>$firstname,
'lastname'=>$lastname,
'email'=>$email);
$this->update($data,"Id=$id");
}
$client->updateClient('Id',$firstname,$lastname,$email)
You're passing the literal string 'Id' as the $id parameter to updateClient. Inside updateClient, you build a where condition with "Id=$id". Your where condition becomes where Id=Id, which is true for every record, so every record is updated.
You need to pass a variable containing the actual ID of the record you want to update.
try to change your code, so it looks like this:
if($form->isvalid($formData))
{
$client= new Application_Model_DbTable_Client();
$firstname = $formData['firstname'];
$lastname = $formData['lastname'];
$email = $formData['email'];
$id=$this->getRequest()->getparam('id'); // define your id
$client->updateClient($id,$firstname,$lastname,$email); // here, change Id into proper $id
$this->_helper->redirector('index');
}