Edit form

A place for users and developers of the Xataface to discuss and receive support.

Edit form

Postby Babs » Thu Jan 14, 2010 12:08 pm

Hi,

Is it possible to work with the edit form in delegate classes functions ?
I would like to add a advmultiselect widget in the edit form when a checkbox is clicked, but I don't know if it is possible de modify this form in the delegate class.

Thank in advance

PS: I managed to add elements in actions/edit.php but I want to try without modify Xataface files
Babs
 
Posts: 11
Joined: Wed Oct 14, 2009 4:13 am

Re: Edit form

Postby shannah » Thu Jan 14, 2010 12:14 pm

There are a number of blocks and slots that are available on the edit form. including:

after_{fieldname}_widget
before_{fieldname}_widget


To see the full list, you can turn debug mode on in the conf.ini file (debug=1).

Then you can add whatever you want after or before any of the fields.

You can also play games with showing and hiding fields by just adding a little bit of javascript to your pages and registering onchange or onclick events to your widgets.

There should be plenty of flexibility without needing to change any xataface files.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Edit form

Postby Babs » Thu Jan 14, 2010 12:55 pm

Thank you for your answer.

OK I didn't know there were blocks in forms.
But which method do I use to add widgets ?

I thought about something like:

Code: Select all
$edit_form =& // the edit_form object
$edit_form->addElement('myelement', '', ...)


Is it correct ?
Babs
 
Posts: 11
Joined: Wed Oct 14, 2009 4:13 am

Re: Edit form

Postby shannah » Thu Jan 14, 2010 2:13 pm

The blocks won't give you access directly to the HTML_QuickForm object. Rather it allows you to place html output to any place you like.

I would break this problem down into 2 parts.

1. Add the extra fields to the form.
2. Add interactivity to hide and show fields based on user interaction.

To add fields to the form that don't correspond to a field in the database you can just add a transient field via the fields.ini file.
e.g.

Code: Select all
[myfield]
    transient=1
    widget:type=advselect
    ...


The transient=1 tells xataface that, despite the fact that this field definition doesn't correspond with a field in the database, you would still like it to appear in your forms.


Then use blocks to add your javascript interaction.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Edit form

Postby Babs » Fri Jan 15, 2010 8:31 am

OK Steve thank you.

But I don't manage to add the "onclick" attribute to my checkbox field (knowing that this is a transient field, with a relationship).
I tried:

Code: Select all
widget:atts:onclick = "MyFunction();"

but nothing happens.
Babs
 
Posts: 11
Joined: Wed Oct 14, 2009 4:13 am

Re: Edit form

Postby shannah » Mon Jan 25, 2010 11:18 am

With checkbox groups the widget:atts directive doesn't work unfortunately. However there's nothing stopping you from inserting your own javascript to add handlers using javascript code to the checkboxes that you want. Using jquery it is quite easy to add events and handlers to just about any part of the page.
shannah
 
Posts: 4457
Joined: Wed Dec 31, 1969 5:00 pm

Re: Edit form

Postby Babs » Tue Feb 16, 2010 1:04 am

Maybe someone will be interested in the solution I used:

First, in order to use the PEAR Quickform tool:

table delegate class:
Code: Select all
private $formulaire;
       
function __construct()
{
    require_once('HTML/Quickform.php');
    $this->formulaire = new HTML_Quickform();
}


Then, the code used to add a block in the form:

Code: Select all
function block__after_poste_widget()
{
   
   $app =& Dataface_Application::getInstance(); 
   /*
   var_dump($app->currentRecord);
   exit();
   */
   $this->formulaire->addElement('advmultiselect', 'test', 'Pièces concernées');
   $this->formulaire->display();
   
   echo <<<EOF
   <script>
      $("input[name='poste[id=47]']").click(function()
                                    {
                                       ajoutListePieces(47);
                                    });
      
      
      function ajoutListePieces(poste)
      {
         $.getJSON(DATAFACE_SITE_HREF, { "-action":"pieces", "poste":escape(poste) }, function(data) {controleListePieces(data);});
      }
         
      function controleListePieces(data)
      {
         for ( var i=0; i<data.length; i++)
         {
            $("#__pieces").addOption(data[i][0], data[i][1]);
         }
      }
   </script>
EOF;
   
}


The javascript/ajax functions are used to call a list of options, via a xataface action.

Code: Select all
class actions_pieces
{
   function handle(&$params)
   {
      $poste = htmlentities($_GET['poste']);   
      $sql = "select id, codearticle from article LIMIT 0, 10";
      
      $res = mysql_query($sql, df_db());
      if ( !$res ) trigger_error(mysql_error(df_db()), E_USER_ERROR);
      $tableau = array();

      while ($r = mysql_fetch_array($res))
      {
         $tableau[] = array($r['id'], $r['codearticle']);
      }
   
      import('Services/JSON.php');
      $json =& new Services_JSON(); // A JSON encoder to allow us to easily
      // convert PHP arrays to javascript arrays.
      echo $json->encode($tableau);
   }
}


The code is not finished yet, but I hope it will help someone.
Babs
 
Posts: 11
Joined: Wed Oct 14, 2009 4:13 am


Return to Xataface Users

Who is online

Users browsing this forum: No registered users and 23 guests

Powered by Dataface
© 2005-2007 Steve Hannah All rights reserved