AshishJoshi-asj / zfdatagrid

Automatically exported from code.google.com/p/zfdatagrid
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Add customized form and process to the grid #182

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
What is the expected output? What do you see instead?

Is there a way to add a fully customized form to the grid (not using a form
built from a Model) ?

In my case, working with Propel and not Model, and above all with dynamic
table in a DB (all tables share some identical columns but have specific
ones too).

It looks I have two ways :

1/ Adding 2 extra columns with icons whose links allow to go to a specific
page in where I display my own form and perform my process if valid or not.

2/ Creating a Zend_Form and just add it to the grid so that I can benefits
from edit/add/delete element (icon/link and page managing the form) and be
able to add a callback for the form validation to make my own process.

What version of the product are you using? On what operating system?
v6.alpha, under Linux Fedora

Please provide any additional information below.

Original issue reported on code.google.com by tribal...@gmail.com on 23 Feb 2010 at 10:55

GoogleCodeExporter commented 9 years ago
I suspend my question, having seen that Bvb_Grid_Form extends Zend_Form.
I will come back to say whether this issue has interest.

Original comment by tribal...@gmail.com on 23 Feb 2010 at 1:58

GoogleCodeExporter commented 9 years ago

Original comment by pao.fre...@gmail.com on 23 Feb 2010 at 3:58

GoogleCodeExporter commented 9 years ago
I have created my own Form, by instanciating a Bvb_Grid_Form and adding my form 
fields.

Now... I do not find how to populate them in edition or adding mode. Is there a 
way ?

Otherwise, I would added some fields which are not in the grid, can I ?
And these new fields depends on the line which is edited (I have to populate 
these
ones only when the PK value is known)

Original comment by tribal...@gmail.com on 23 Feb 2010 at 4:23

GoogleCodeExporter commented 9 years ago
Hi, 

I complete my last comment questions, to make them explicit :
Is there a way to associate a form element (eg. Zend_Form_Element of name
'countryformfield'), while building the form, to a field (eg. field 'idCountry' 
in
the SQL query) with a kind of $form->associate('countryformfield', 
':idCountry') and
when a form is displayed then the value of the element 'countryformfield' will 
be
replace by the 'idCountry' field of the query result.

And... is there a way to modify the form before displaying, according to fields 
of
query result.
Example : 
1/ edition form page is called
2/ query result is stored
3/ form is build
4/ a method of the form (callback ?) receive the query result so that 
developper can
add form elements according to the value of one of these fields
5/ form is populate with values
6/ form is displayed

Original comment by tribal...@gmail.com on 24 Feb 2010 at 8:41

GoogleCodeExporter commented 9 years ago
Hi,

You can use this method to know what is being done:

$grid->willShow(); //returns array

But this method can only be called after $grid->deploy()

So it won't be any helpful.

YOu can also get the for instance 

$grid->getForm();

There are a series of callbacks you can use:

$form->setCallback(Before|After)(Update|Insert|Delete);

But I will think in something else, maybe events....

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 25 Feb 2010 at 12:52

GoogleCodeExporter commented 9 years ago
Ok, but what way I must use so ? Two questions :

1/ I have to detect, when I build the form, if the URL contains these params :
editcibleDouble/1/commcibleDouble/mode:edit;[<MY_KEY>:<My_KEY_VALUE>]
and so call the form setValue() method so that the form display values to edit ?

2/ Is there a callback which overide Bvb_Grid behaviour ? I work with a system 
of
revisions, so when a line of the DB is edited I don't want it is updated but 
replaced
by a new one with a revision ID higher. Idem for a deletion. Aniway, I want my
callback to be called to do the process I want, I do not want the component 
write in
my DB.

Original comment by tribal...@gmail.com on 26 Feb 2010 at 9:29

GoogleCodeExporter commented 9 years ago
Hi Tribal,

If I understood correctly your issue 2. above, the solution would be to make a 
model
(Zend_Db_Table), add it to the grid, and in model are functions to override - 
update,
insert, delete, etc.

Like:

function update() {
  fetch highest rev, replace current
  parent::update()
  some cleanup
}

I'm using this approach and it works nicely. 

Hope it helps

Original comment by vlatko.b...@gmail.com on 26 Feb 2010 at 11:23

GoogleCodeExporter commented 9 years ago
Hi !

I understand and it is a good idea indeed.

But... my tables are PHP generated. To understand, know that I load CSV files 
to put
their content in tables.
These tables share some "system" fields (I know them, they do not come from 
CSV) but
they contains too data from CSV, each CSV column being a field in my table.

I'm would not want to create a "Model" for each table.

Original comment by tribal...@gmail.com on 26 Feb 2010 at 12:07

GoogleCodeExporter commented 9 years ago
Hi,

tribalmco: If you have time, you can create an adapter that implements the
Bvb_Grid_Source_Interface that will deal with all display and CRUD operations.

Latest revision already has form generation based on simple queries.

Revision 701

The system will try to guess the table to store data, but you can override by 
using
$form->setTable('my_crud_table');

Let me know if this helps...

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 27 Feb 2010 at 5:09

GoogleCodeExporter commented 9 years ago
a Propel source implementation could be an idea in fact.

In my main case, I have to display in datagrid rows whose data come from more
databases and from tables whose a number of columns are dynamics.

The best possible implementation for me is to be able to just add my own form 
(that
can be not created by Bvb_Grid because of its complexity) and my own callbacks 
to
perform validation and addition/edition/deletion processes after submitting the 
form
, Bvb_Grid doing nothing at this level (no process for creating and populating 
the
form and no process of the form submission).
That could be a user defined callback set in the Bvb_Grid (by specifying the 
field it
want to retrieve as parameter) which would return the form (already initialised 
and
populated) to the Bvb_Grid to display it in its page.

A kind of :
$grid->setFormByCallback($callbackToManageTheForm, 
$valueOfTheFieldNeededToInitializeIt);

And user must defined a method like :

function getEditionForm($valueOfWaitedField)
{
    $form = new Zend_Form();
    // stuff...

    if (true == $*->isPost()) {
        // stuff...
        // if all is good so redirection to the Bvb_Grid page (or perhaps Bvb_Grid
could be passed as parameter so that it can be called to process the 
redirection with
the its params management (order, filter, ...))
    }
    // populate by getting data thanks to the value of the waited field
    return $form;
}

To resume, I think it could be practical to be able to :
- defined a own form
- get the value of the asked field to be able to retrieve data to populate the 
form

It could be nice, too, to be able to have a callback for deletion, Bvb_Grid 
doing
nothing and letting the callback make its process.

This "opening" would offers the possibility to benefit from page/URL management
without having to create our own pages (controller).

I hope it is comprehensible.

Original comment by tribal...@gmail.com on 1 Mar 2010 at 3:58

GoogleCodeExporter commented 9 years ago
Hi,

This is what I can do for you at this moment.

When editing a record you can get the details in your controller by doing this:

{{{
$grid->getSource()->fetchDetail($grid->getPkFromUrl())
}}}

Which returns an array.

About form.

{{{
$form = new Bvb_Grid_Form();
$form->setAdd(1)->setEdit(1)->setDelete(1)->setAddButton(1);
$grid->setForm($form);
}}}

You should have something like this.
A "new" option has been added. 

{{{
$form->setTable(false);
}}}
if set to false, the system won't do the delete, insert, and update processes. 

This allows you to use the callbacks to do what you want with your data.

You can still use the Bvb_Grid_Form();

Remove all form elements and then add yours

{{{
$form = new Bvb_Grid_Form();
$form->setTable(false);
$form->setAdd(1)->setEdit(1)->setDelete(1)->setAddButton(1);
$form->addElement('text','element_name',array('label'=>'Something'));
$grid->setForm($form);
}}}

After adding the form you can remove all fields that where auto added.

{{{
foreach ($grid->getFields() as $field)
   {
     $grid->getForm()->removeElement($field);
   }
}}}

And the form will render with only the fields you added manually.

In time for 0.6 I think this is the best I can do for you.

Unless your are seeing a better solution...

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 2 Mar 2010 at 2:36

GoogleCodeExporter commented 9 years ago
Hi,

Sorry about the 

{{{
}}}

I though I was in the wiki :)

Original comment by pao.fre...@gmail.com on 2 Mar 2010 at 2:37

GoogleCodeExporter commented 9 years ago
Hi,

could the function name ->setTable(false) be changed else to reflect its 
functionality.

Maybe $form->setPerformCRUD(false), or something more mnemonic?

Original comment by vlatko.b...@gmail.com on 2 Mar 2010 at 6:14

GoogleCodeExporter commented 9 years ago
Hi,

Great, your anwser match totally my need !

In fact a new name could be more convenient. setPerformCrud() looks good, or
setIsPerformCrudAllowed() (notion of boolean in the name, explicit for a 'ini'
configuration : isPerformCrudAllowed = <1|0> ; question of standards)

For an next version, be able to set a boolean for each type of processes could 
be a
'plus' as well : <methodName>For<Edition|Addition|Deletion>()

Original comment by tribal...@gmail.com on 2 Mar 2010 at 9:10

GoogleCodeExporter commented 9 years ago
Another annotation : When I tried the getSource() when displaying the Grid (and 
not
the form), it retrieves me data of the first row.

So my question is : is there a way to know in what mode the grid is ? (deletion,
edition, addition, ... ?) so that to be able to prepare the form with good 
processes.

Original comment by tribal...@gmail.com on 2 Mar 2010 at 9:12

GoogleCodeExporter commented 9 years ago
Hi,

First the method name:

The method setTable() is intended for inserting data in other table different 
from
the one we fetch results. 

A new method was added

$form->setIsPerformCrudAllowed(0|1);

This will disable auto processing.

About comment 15.

Use this:
$grid->getFormSettings();

Will return an array like this:

mode:
    add|edit|delete
id:
    The id where the action will be performed// Same as $grid->getPkFromUrl();
row:
    If editing, the actual record that is beeing edited
action:
    The form action

Let me know if this works...

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 2 Mar 2010 at 11:33

GoogleCodeExporter commented 9 years ago
Tribal,

Can you explain this better:

"
For an next version, be able to set a boolean for each type of processes could 
be a
'plus' as well : <methodName>For<Edition|Addition|Deletion>()
"

Best Regards,
Bento Vilas Boas

Original comment by pao.fre...@gmail.com on 2 Mar 2010 at 11:35

GoogleCodeExporter commented 9 years ago
Hi again,

I have not tested the last update yet. I will do it.

For explanation, like $form->setIsPerformCrudAllowed(0|1), it could be useful 
(why
not !) to be able to disable just a part of the CRUD processes, that is to say 
just
disable the edition process, and not the addition and deletion ones.

So :
$form->setIsPerformCrudAllowedForAddition(0|1)
$form->setIsPerformCrudAllowedForEdition(0|1)
$form->setIsPerformCrudAllowedForDeletion(0|1)

We can imagine a case where addition and edition would be managed by the 
component,
but for deletion the row would stay in database, it could be just mark as 
'deleted'
(flag) via a user callback, so no taken action (ie. deletion) by the component.

Original comment by tribal...@gmail.com on 2 Mar 2010 at 2:48

GoogleCodeExporter commented 9 years ago
Just to extend the possibility :
$form->setIsPerformCrudAllowed(1)->setIsPerformCrudAllowedForDeletion(0) would
allowed component to process all form actions exept for deletion one.

Original comment by tribal...@gmail.com on 2 Mar 2010 at 2:51

GoogleCodeExporter commented 9 years ago

Original comment by bento.vi...@gmail.com on 3 Mar 2010 at 4:56

GoogleCodeExporter commented 9 years ago
I use :

$form = new Bvb_Grid_Form();
$form->setAdd(1)->setEdit(1)->setDelete(1)->setAddButton(1);
$form->setIsPerformCrudAllowed(0);
$grid->setForm($form);

When I click on edit icon on the grid, the page display to me, instead of the 
form,
this :

object(Zend_Db_Statement_Exception)[276]
  protected 'message' => string 'SQLSTATE[HY000]: General error: 1096 No tables used'
(length=51)
  private 'string' => string '' (length=0)
  protected 'code' => int 0
  protected 'file' => string 'Zend/Db/Statement/Pdo.php' (length=64)
  protected 'line' => int 238

Original comment by tribal...@gmail.com on 3 Mar 2010 at 5:21

GoogleCodeExporter commented 9 years ago
Reason found : form must be added to the grid before invoking this method.

Original comment by tribal...@gmail.com on 3 Mar 2010 at 5:24

GoogleCodeExporter commented 9 years ago
My code is so :
        $form = new Bvb_Grid_Form();
        $form->setAdd(1)->setEdit(1)->setDelete(1)->setAddButton(1);
        $this->_grid->setForm($form);
        $form->setIsPerformCrudAllowed(0);

And... the component keeps on writing the DB (after submitting the edit form)

Original comment by tribal...@gmail.com on 3 Mar 2010 at 5:26

GoogleCodeExporter commented 9 years ago
Hi,

New options as suggested:

$form->setIsPerformCrudAllowed(false);
$form->setIsPerformCrudAllowedForAddition(true);
$form->setIsPerformCrudAllowedForEdition(true);
$form->setIsPerformCrudAllowedForDeletion(false);

All defaults to true;

@Tribal: comment 21 is the correct way to do it.

Let me know if the errors persists...

Best Regards,
Bento Vilas Boas

Original comment by bento.vi...@gmail.com on 3 Mar 2010 at 9:57

GoogleCodeExporter commented 9 years ago
Error persists.
Example : uncomment l.288 of SiteController ( 
$form->setIsPerformCrudAllowed(false)
of crudaction())

Original comment by tribal...@gmail.com on 4 Mar 2010 at 9:01

GoogleCodeExporter commented 9 years ago
@Tribal

Are you using a Model or a Query?

Best Regards,
Bento Vilas Boas

Original comment by bento.vi...@gmail.com on 6 Mar 2010 at 9:37

GoogleCodeExporter commented 9 years ago
Hi bento, 

I'm using a query.

Original comment by tribal...@gmail.com on 8 Mar 2010 at 9:03

GoogleCodeExporter commented 9 years ago
Hi again, I update the errors :
Since a previous update I have not get the error "SQLSTATE[HY000]: General 
error:
1096 No tables used", but the component update my database with edition, 
deletion or
addition actions.
My code :
        $this->_form = new Bvb_Grid_Form();
        $this->_form->setAdd(1)->setEdit(1)->setDelete(1)->setAddButton(1);
        $this->_form->setIsPerformCrudAllowed(false);
        $this->_grid->setForm($this->_form);

Original comment by tribal...@gmail.com on 8 Mar 2010 at 9:31

GoogleCodeExporter commented 9 years ago
Hi Tribal,

Please check the latest revision

Best Regards,
Bento Vilas Boas

Original comment by bento.vi...@gmail.com on 8 Mar 2010 at 1:04

GoogleCodeExporter commented 9 years ago
I have tried and that works, the component does not keep on writing the DB.

But the confirmation message, specifying that record was updated, is displayed.
I have tried to return a boolean from my callback function (beforeUpdate) to 
see if
there is a effect but no.

What is the way to specify to the component whether the update process has 
failed or
not please, if it exists ?

Original comment by tribal...@gmail.com on 8 Mar 2010 at 1:20

GoogleCodeExporter commented 9 years ago
Hi,

The behavior resides inside a try{}catch{} block.

If no exception is thrown, the result is available as successful.

I haven't try it yet, but if your callback throws an exception, it will 
"fail"...

Best Regards,
Bento Vilas Boas 

Original comment by bento.vi...@gmail.com on 8 Mar 2010 at 2:49

GoogleCodeExporter commented 9 years ago
Great !
Thank you.

Original comment by tribal...@gmail.com on 8 Mar 2010 at 3:46

GoogleCodeExporter commented 9 years ago

Original comment by bento.vi...@gmail.com on 8 Mar 2010 at 3:50