Zend form elements coming from a database - zend-framework

Currently I'm stuck writing a edit function for an order page using zend framework
Here is the situation:
One order consist of multiple items
Eg Order Number 1
Consists of Items
OrderListId OrderId title Sent dateSent trackingNumber
1 1 Book A 0 12/12/12 44444
2 1 Book B 1 10/01/12 51223
3 1 Book C 0 01/02/12 33353
and so on...
Please notes: the table are stored in a database
I need to have a page where the user can edit title, Sent, dateSent and trackingNumber fields.
How can I use zend form to populate these fields for edit. Since I want to have one page they can edit all these orderlist items. Rather then the user have to click back and forward to edit each order list item individually.
What's the best approach to this situation?
Thanks so much in advance!

I suggest using ZF Data Grid.

I had a problem similar to this awhile back and I came up with a solution where I just appended an update link to the end of the table row and populated a form for update while still displaying current information.
The following code samples were built before I knew what I was doing with ZF so they may be a little messy. I ended up with what amounts to single view that handles all of my CRUD operations.
**All css selectors removed for clarity
The display view:
<?php if (isset($this->leadtracks)): ?>
<form method="post" action="/admin/track/deletetrack">
<table>
<tr>
<th colspan="5"><?php $this->tableHeader() ?></th>
</tr>
<tr>
<th>Select</th>
<th>Shift</th>
<th>Days Off</th>
<th>Slots</th>
<th> </th>
</tr>
<tr>
<?php
//begin foreach loop using alternate syntax
foreach ($this->leadtracks as $lt):
?>
<td><input type="checkbox" name="trackid[]"
value="<?php
//trackid as value for checkbox
echo $lt->trackid
?>" style="width: 2px" /></td>
<td><?php
//find and display shift name
$lt->getShift();
?></td>
<td><?php $lt->getDays(); ?></td>
<td><?php echo $this->escape($lt['qty'])?></td>
<td><a href="<?php
//link to update action passing trackid and bidlocationid values
echo $this->url(array('module' => 'admin',
'controller' => 'track',
'action' => 'updatetrack',
'trackid' => $lt['trackid'],
'bidlocationid' => $lt['bidlocationid']))
?>">Update</a></td>
</tr>
<?php endforeach ?>
<tr>
<th colspan="5">
<input type="submit" name="submit" value="Delete Selected" />
</th>
</tr>
</table>
<?php endif; ?>
<!-- further code removed for brevity -->
</form>
The updateAction(), only one action for demonstration:
public function updatetrackAction() {
//get form set new label and assign to view
$form = new Admin_Form_Track();
$form->submit->setLabel('Update Track');
$form->addTrack->setLegend('Update A Track');
$this->view->form = $form;
try {
//check is post and is valid and get values from form
if ($this->getRequest()->isPost()) {
if ($form->isValid($this->getRequest()->getPost())) {
$data = $form->getValues();
$trackId = $data['trackid'];
//save update data to database
$update = new Model();
$update->update($trackId, $data);
//generate meesage
$this->_helper->flashMessenger->addMessage(Zend_Registry::get('config')
->messages->trackupdate->successful);
$this->getHelper('Redirector')->gotoSimple('addtrack');
} else {
//if form is not vaild populate form for resubmission
$form->populate($this->getRequest()->getPost());
}
} else {
//if form is not posted populate form with data from database
$trackId = $this->_getParam('trackid', 0);
if ($trackId > 0) {
$z = new Model();
$y = $z->getTrack($trackId)->toArray();
$form->populate($y);
}
}
} catch (Zend_Exception $e) {
$this->_helper->flashMessenger->addMessage($e->getMessage()->getTrace());
$this->_redirect($this->getRequest()->getRequestUri());
}
}
The addAction() and the deleteAction() are very similar and use the same view script called through the action() helper. The result is a page in which add, update and delete actions are performed from what the user perceives as one page(check box for multiple deletes and a link for update, every refresh displays current data). Although I do move the form from side to side as a sort of visual clue, though it really doesn't matter as all actions are available on each page.
the update view:
<div>
<h3><?php echo ucwords($this->escape(strtoupper($this->station) . ' - ' . $this->bidloc)); //page header ?></h3>
<?php echo $this->form //edit/add form ?>
</div>
<div>
<?php echo $this->action('displaytrack', 'track', 'admin') //display view ?>
</div>
I'm sure this can be done with the partial() view helper, without using the action() helper and I'm sure it can be done inline, I just haven't taken the time to figure out how to do it.
good Luck.

Related

How to submit form with Datatables

I use Datatables to submit answers of questions from a survey. Each question is in new page, and the next is seen with the "Next" button. I want to have only one submit button and it has to appear in the end of all questions. In my code, it's under the table and is visible all the time. My biggest problem is that this submit button insert in db only the last question, but not all questions.
Please can you help me? :)
My view is:
<html>
<head>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.5/css/jquery.dataTables.min.css" type="text/css" />
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//cdn.datatables.net/1.10.5/js/jquery.dataTables.min.js"></script>
<script>
$(document).ready(function() {
$('#example').dataTable( {
"pagingType": "simple",
"lengthMenu": [[1], [1]],
"bSort": false,
"language": {
"paginate": {
"previous": true
}
}
} );
} );
</script>
</head>
<body>
<?php
echo form_open('index/survey_fill/' .$survey_id ); ?>
<table id="example" >
<thead>
<tr><th>Question</th></tr>
</thead>
<tbody>
<?php
echo validation_errors();
foreach ($survey as $row)
{
?>
<tr>
<td>
<?php echo "$row->question"; ?><br/>
<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => '5'
);
echo form_radio($data);
echo " 5 ";
$data=array(
'name' => 'answer['.$row->question_id.']',
'value' => ' 4'
);
echo form_radio($data);
echo " 4";
</td></tr>
<?php
}
?>
</tbody>
</table>
<?php echo "<input type='hidden' name='question_id' value='$row->question_id' />"; ?>
<input type="submit" id="button" name = "submit" value="Submit" class="btn btn-success">
</form>
</div>
</body>
</html>
My model is:
public function survey_fill()
{
$answers=($this->input->post('answer'));
if (null !==($this->input->post('submit'))) {
$date = new DateTime("now");
foreach($answers as $question_id=>$answer)
{
$data = array(
'user_id'=>$this->session->userdata['user_id'],
'question_id'=>$question_id,
'answer'=>$answer,
'created_at'=>$date->format('Y-m-d H:i:s')
);
$this->db->insert('survey_answers', $data);
}
if($this->db->affected_rows() > 0)
{
return true;
}
return false;
}
}
I'm thinking Datatables is quite a lot of overkill for what you're trying to do. Personally, I'd output this in a set of divs and do a simple forward-back type button with jQuery toggle, which is a LOT less overhead. There's also very low-overhead paging solutions out there that could suffice. Remember, datatables is not only reformatting the table, but it has an encyclopedia of API calls that are loaded, plus the data model it holds. That's a LOT for a show/hide type test.
Regardless, the solution is pretty straightforward. Wrap your container (whatever it is, datatable or divs) with a <form> tag. Put the submit button at the end, and show/hide or enable/disable it via whatever means you want. Datatables has an API call that can tell you the current page, and you could key off that to enable your button. Since the button will be before you close the <form> it will automatically submit.
Alternative, you could wrap your container in the form, build a jQuery .on('click') event that serializes the form and submits it via Ajax if that was desired. The key if you go this route is to preventDefault() so the form doesn't auto-submit as is standard for html forms.

Disable or change validation for embedded form fields in Symfony1 form

I am trying to cancel validation in embedded forms based on a value from main form.
By default, embedded forms fields have validator option set to 'required'=>true. So it gets validated like that. If user leave any field blank, the form does not pass validation and blank fields get marked in template (different style).
What I am trying to do is to change option:"required" to false for all fields in embedded form.
I tried to do that in post validator callback method, but it seems that it is not possible that way.
The main form code:
class TestForma extends sfForm
{
public function configure()
{
$this->setWidgets(array(
'validate_items' => new sfWidgetFormChoice(array(
'choices' => array('no' => 'No', 'yes' => 'Yes'),
'multiple' => false,'expanded'=>true,'default' => 'no')),
));
$this->setValidators(array('validate_items' => new sfValidatorPass()));
$this->widgetSchema->setNameFormat('testforma[%s]');
$subForm = new sfForm();
for ($i = 0; $i < 2; $i++)
{
$form = new ItemForma();
$subForm->embedForm($i, $form);
}
$this->embedForm('items', $subForm);
$this->validatorSchema->setPostValidator(
new sfValidatorCallback(array('callback' => array($this, 'postValidate')))
);
}
Post-validator code:
public function postValidate($validator,$values)
{
$validatorSchema = $this->getValidatorSchema();
if($values['validate_items']=='no')
{
$itemsValidatorSchema = $validatorSchema['items'];
$itemsFieldsValidatorSchemes = $itemsValidatorSchema->getFields();
foreach($itemsFieldsValidatorSchemes as $itemValidatorScheme)
{
$itemValidatorScheme['color']->setOption('required',false);
$itemValidatorScheme['shape']->setOption('required',false);
}
}
return $values;
}
Embedded form class:
class ItemForma extends sfForm
{
public function configure()
{
$this->setWidgets(array(
'color' => new sfWidgetFormInputText(),
'shape' => new sfWidgetFormInput(),
));
$this->setValidators(array(
'color' => new sfValidatorString(array('required'=>true)),
'shape' => new sfValidatorEmail(array('required'=>true)),
));
$this->widgetSchema->setNameFormat('items[%s]');
}
}
Template code:
<form action="<?php echo url_for('weather/formiranje')?>" method="post">
<?php
foreach($form->getErrorSchema()->getErrors() as $e)
{
echo $e->__toString();
}
?>
<table>
<tfoot>
<tr>
<td colspan="2">
<input type="submit" value="OK" />
</td>
</tr>
</tfoot>
<tbody>
<tr><th>Main form</th></tr>
<tr><td><?php echo $form['validate_items']->renderLabel() ?>
<span class="<?php echo $form['validate_items']->hasError() ? 'rowError' : ''?>">
<?php echo $form['validate_items'] ?></span>
</td></tr>
<tr><td> </td></tr>
<tr><th>Embedded forms</th></tr>
<?php
foreach($form['items'] as $item)
{
?>
<tr>
<td><span class="<?php echo $item['color']->hasError() ? 'rowError' : ''?>">
<?php echo $item['color']->renderLabel() ?>
<?php echo $item['color'] ?></span>
</td>
</tr>
<tr>
<td><span class="<?php echo $item['shape']->hasError() ? 'rowError' : ''?>">
<?php echo $item['shape']->renderLabel() ?>
<?php echo $item['shape'] ?></span>
</td></tr>
<?php
}
echo $form['_csrf_token'];
?>
</tbody>
</table>
</form>
The way you organised it won't work because the post validator is run after all the field validators, so they've already been checked and marked as failed. (because the fields were required).
You could try the same approach you have here but with setting a preValidator instead of a postValidator. I think it should work then.
If it still won't work as expected what I would do is to change the default settings on the embedded form's fields to 'required' = false and use the postValidator. In the validator you could check whether or not you need to validate the embedded fields. If you need to validate them you can check if their values are set and if not you can throw errors for those fields. (I hope this is explained clearly)
Another thing you could try is to re-run the validation for the chosen fields. So something like that in your postValidator:
$itemValidatorScheme['color']->setOption('required',false);
$itemValidatorScheme['color']->clean($values['name_of_the_field']);

Is it possible to profile Zend_Table queries?

I'm looking for a way to profile queries which are performed internally by Zend_Table.
For example, after you finish Quickstart course, how to profile all the queries that are performed ?
I've tried to enable profiler from application.ini like this:
resources.db.profiler.class = "Zend_Db_Profiler_Firebug"
resources.db.profiler.enabled = true
And placed next rows in a Guestbook controller:
...
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$profiler = $db->getProfiler();
echo $profiler->getTotalElapsedSecs();
Which gives me 0
I've also tried to enable profiler in a Bootstrap file like this:
protected function _initProfiler() {
$this->bootstrap("db");
$profiler = new Zend_Db_Profiler_Firebug("All DB Queries");
$profiler->setEnabled(true);
Zend_Registry::get("db")->setProfiler($profiler);
}
Whick doesn't give me any result (I've installed and tested Firebug and FirePHP, using Zend_Log_Writer_Firebug())
I will appreciate any help. Thanks !
I've not tried the Firebug profiler. But I do use a html table to output profiler info at the bottom of each page that does a query.
enable profiler in application.ini
resources.db.params.profiler = true
render the profile results in the layout:
<?php
$this->addScriptPath(APPLICATION_PATH . '/views/scripts');
echo $this->render('profiler.phtml')
?>
profiler.phtml the view to render
<?php
// get the default db adapter
$adapter = Zend_Db_Table::getDefaultAdapter();
$profiler = $adapter->getProfiler();
if ($profiler->getEnabled() && $profiler->getTotalNumQueries() > 0) :
?>
<div style='text-align:center'>
<h2>Database Profiling Report</h2>
<p>Total queries executed: <?php echo $profiler->getTotalNumQueries() ?></p>
<p>Total elapsed time: <?php echo $profiler->getTotalElapsedSecs() ?></p>
</div>
<table class='spreadsheet' cellpadding='0' cellspacing='0' style='margin:10px auto'>
<thead>
<tr>
<th>#</th>
<th>Query</th>
<th>Time</th>
</tr>
</thead>
<tbody>
<?php foreach ($profiler->getQueryProfiles() as $queryNumber => $query) : ?>
<tr>
<td>(<?php echo $queryNumber + 1 ?>)</td>
<td><?php echo $query->getQuery(); ?></td>
<td><?php echo $query->getElapsedSecs(); ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php endif ?>
I don't imagine that using the firebug profiler is much more difficult.
The problem was in the parameters instantiation. When I've entered
resources.db.params.profiler.class = "Zend_Db_Profiler_Firebug"
resources.db.params.profiler.enabled = true
instead of previous configuration, everything went fine.
Note .params section of the parameter lines

zend paginator make other links and Action Calling concatinating with the URL

I am NEW to ZF .i used zend paginator in my first project .its working fine that is switching b/w pages with right result but the problem is that i have other links too in that view have a look to my view
<?php include "header.phtml"; ?>
<h1><?php echo $this->escape($this->title);?></h1>
<h2><?php echo $this->escape($this->description);?></h2>
Register
<table border="1" align="center">
<tr>
<th>User Name</th>
<th>First Name</th>
<th>Last Name</th>
<th>Action</th>
</tr>
<?php
foreach($this->paginator as $record){?>
<tr>
<td><?php echo $record->user_name;?></td>
<td><?php echo $record->first_name;?></td>
<td><?php echo $record->last_name;?></td>
<td>
Edit
|
Delete
</td>
</tr>
<?php } ?>
</table>
<?php echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml'); ?>
<?php include "footer.phtml"; ?>
as i said the pagination renders and working fine but when i click on these links
<a id="edit_link" href="edit/id/<?php echo $record->id;?>">Edit</a>
or
<a id="delete_link" href="del/id/<?php echo $record->id;?>">Delete</a>
or
Register
it is not calling the required action instead it make my url like this
(initial link) http://localhost/zend_login/web_root/index.php/task/list
after clicking any of the above link its like this
http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/8
http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/23
http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/register http://localhost/zend_login/web_root/index.php/task/list/page/edit/id/edit/id/del/id/12
note its not happening when the page renders first time but when i click on any pagination link its doing so initialy its going to the reguired action and displaying a view...any help HERE IS THE ACTION
public function listAction(){
$registry = Zend_Registry::getInstance();
$DB = $registry['DB'];
$sql = "SELECT * FROM task ORDER BY task_name ASC";
$result = $DB->fetchAll($sql);
$page=$this->_getParam('page',1);
$paginator = Zend_Paginator::factory($result);
$paginator->setItemCountPerPage(3);
$paginator->setCurrentPageNumber($page);
$this->view->assign('title','Task List');
$this->view->assign('description','Below, are the Task:');
$this->view->paginator=$paginator;
}
Try:
// controller
$this->view->controllerName = $this->getRequest()->getControllerName();
// view script
Edit
|
Delete
or
Edit
|
Delete
Second example uses baseUrl() view helper that's using front controller's baseUrl setting. If you don't set baseUrl in your frontController it's trying to guess. As you're not using bootstrap functionality to set baseUrl you may do the following in index.php (not required):
$frontController = Zend_Controller_Front::getInstance();
$frontController->setBaseUrl('/');
Third possibility using url() view helper:
<a href="<?php echo $this->url(array(
'controller' => $controllerName,
'action' => 'edit',
'id' => $record_->id
)); ?>">Edit</a>
|
<a href="<?php echo $this->url(array(
'controller' => $controllerName,
'action' => 'del',
'id' => $record_->id
));?>">Delete</a>
add this in your action
$request = $this->getRequest();
$this->view->assign('url', $request->getBaseURL());
and replace your links in view with this
Add a Task
Edit
Delete

Symfony forms post

I read about Symfony forms and i have simple question :
i Make 2 templates "test" and test1 . I want to post forms values of
test to test1 and after to show them with echo.
In test i put:
form action="<?php echo url_for('maraton/test') ?>" method="POST" />
<table>
<tr>
<?php echo $form?>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="submit" />
</td>
</tr>
</table>
</form>
my action is :
public function executeTest(sfWebRequest $request)
{
$this->form = new AthletesForm();
}
public function executeTest1(sfWebRequest $request)
{
$this->form = new AthletesForm();
if ($request->isMethod('post'))
{
$values = $this->form->getValues();
}
When creating a form you create it using an action :
public function executeContact($request)
{
$this->form = new sfForm();
}
Then to display that form you use a template
<?php echo $form->renderFormTag('foo/contact') ?> // this route "foo/contact" points to the action that will process the submit
<table>
<?php echo $form ?>
<tr>
<td colspan="2">
<input type="submit" />
</td>
</tr>
</table>
</form>
Then to process the form submit you use either the same action as above or a seperate action :
if ($request->isMethod('post'))
// this if statement would be added if you used the same action to
// create / process the submit of the form
{
$this->form = new sfForm(); // create an empty form object
$this->form->bind($request->getParameter('contact'));
// merges (binds) the post data with the form
if ($this->form->isValid()) // if the data is valid
{
// Handle the form submission
$contact = $this->form->getValues();
$name = $contact['name'];
// Or to get a specific value
$name = $this->form->getValue('name');
// Do stuff
// persist to database / save to file etc
$this->redirect('foo/bar');
}