How to get a handle to a CRUD form

80 views
Skip to first unread message

The Black Pig

unread,
Nov 1, 2012, 9:05:58 AM11/1/12
to agile-too...@googlegroups.com
Hi all,

simple question I hope.

I want to add some client side behaviour (show/hide or add fields depending on field values)  to the form that is presented by the Add or Edit buttons from a CRUD grid.

How do I get a handle to the form so that I can manipulate it?

TIA
The Black Pig

Gowrav Vishwakarma

unread,
Nov 1, 2012, 9:21:30 AM11/1/12
to agile-too...@googlegroups.com
$crud = $this->add('CRUD');

so form in crud can be used with
$crud->form

and grid in crud can be handeled using
$crud->grid


The Black Pig

--
 
 



--
Regards

Gowrav Vishwakarma
Man of Affairs, Digital Vish, Xavoc International
http://www.digitlavish.com : +91 9783807100
http://www.xavoc.com

--------------------------------------------------------------------
Never Never Never Never Never Never Never Never Never Never Never Never Never Never Never  GIVE UP

Imants Horsts

unread,
Nov 1, 2012, 10:45:26 AM11/1/12
to agile-too...@googlegroups.com
And don't forget to use IF statements, because same script will be executed for form and for grid. Like this:


$crud = $this->add('CRUD');

if($f=$crud->form){
    $f->addField();
    // do something else with form
}

if($g=$crud->grid){
    $g->addPaginator();
    // do something else with grid

The Black Pig

unread,
Nov 1, 2012, 11:07:50 AM11/1/12
to agile-too...@googlegroups.com
Thanks,

That's what I thought but I when I add a field, it's not appearing.

This is my page class:

<?php
class page_livestock extends Page {
    function init(){
        parent::init();
$m = $this->add('Model_Livestock_Types');

$tabs = $this->add('Tabs');
foreach ($m as $r) {  //Loop thru livestock types and create a tab with CRUD for each
   $name = $r['name'];
   $id = $r['id'];
   $tm = $this->setModel("Livestock_$name")->addCondition('type_id', $id);
   $tf = "setTab$name";
   $tm['type_id'] = $id;
   $tab = $tabs->addTab("$name");
   if (method_exists(__CLASS__, $tf)) {
$this->$tf($tab, $tm);  //some livestock need special form handling or CRUD fields, manage them with their own method
   } else {
$this->setTab($tab, $tm); //if not use a default method
   }
}
    }
    
    private function setTabPigs(&$t, $tm) {
$fields['grid'] = array('name','tag_no','sex','dob','dod');
$fields['edit'] = array('name','tag_no','sex','dob','dod','comments');
$c = $this->add('CRUD')->setModel($tm,$fields['edit'],$fields['grid']);
$t->add($c);
if ($f = $c->form) {
   $f->addField('text','sire');
}
    }
    
    private function setTabSheep(&$t, $tm) {
$fields['grid'] = array('name','race','tag_no','sex','dob','dod');
$fields['edit'] = array('name','race','tag_no','sex','dob','dod','comments');
$t->add('CRUD')->setModel($tm,$fields['edit'],$fields['grid']);
    }
    
    private function setTab(&$t, $tm) {
$t->add('CRUD')->setModel($tm);
    }
}


Imants Horsts

unread,
Nov 1, 2012, 12:17:01 PM11/1/12
to agile-too...@googlegroups.com
1)

    $tm = $this->setModel("Livestock_$name")->addCondition('type_id', $id);
should be
    $tm = $this->add("Model_Livestock_$name")->addCondition('type_id', $id);
Also I'm not sure you need condition here at all. You probably have this condition already in Model_Lifestock_Pigs / Sheeps etc.
So you probably don't need that here one more time.

2)
I guess this is useless because you already have condition set:
    $tm['type_id'] = $id;

3)
You probably need to define $fields variable (as empty array or null) before using it.
It'll not change much, but... I guess you can get PHP warning otherwise.
At least I would do like that :)

4)
These lines are wrong:

    $c = $this->add('CRUD')->setModel($tm,$fields['edit'],$fields['grid']);
    $t->add($c);
sould be
    $c = $t->add('CRUD');
    $c->setModel($tm,$fields['edit'],$fields['grid']);
because setModel will return Model and not CRUD object
also because you add CRUD to $this (page) and not tab.

5)
Try out your code with changes explained above and let's see what happens.
I didn't test this - just writing directly here what I think and know.

Imants Horsts

unread,
Nov 1, 2012, 12:18:37 PM11/1/12
to agile-too...@googlegroups.com
Why you need these lines at all?

    if ($f = $c->form) {
        $f->addField('text','sire');
    }
Can't you simply add "sire" field in $fields['edit'] ?





On Thursday, November 1, 2012 5:07:50 PM UTC+2, The Black Pig wrote:

The Black Pig

unread,
Nov 1, 2012, 12:38:19 PM11/1/12
to agile-too...@googlegroups.com
Hi Imants,

I was just in the middle of posting a reply.

to address your first points:

1)    $tm = $this->setModel("Livestock_$name")->addCondition('type_id', $id);
should be    $tm = $this->add("Model_Livestock_$name")->addCondition('type_id', $id);

Also I'm not sure you need condition here at all. You probably have this condition already in Model_Lifestock_Pigs / Sheeps etc.

I don't have the addCondition in the child classes as I couldn't work out to soft code it (i.e. pass the type_id as a variable into the model - I don't want to hard code the values)


2)
I guess this is useless because you already have condition set:
    $tm['type_id'] = $id;

Yes - this was a hangover when I was trying soft code the addCondition in the model.


3)
You probably need to define $fields variable (as empty array or null) before using it.
It'll not change much, but... I guess you can get PHP warning otherwise.
At least I would do like that :)

Yep - just being lazy ;)


4)
These lines are wrong:
    $c = $this->add('CRUD')->setModel($tm,$fields['edit'],$fields['grid']);
    $t->add($c);
sould be
    $c = $t->add('CRUD');
    $c->setModel($tm,$fields['edit'],$fields['grid']);
because setModel will return Model and not CRUD object
also because you add CRUD to $this (page) and not tab.

Yes - this was the problem.

I had already changed the lines to read:

$c = $t->add('CRUD')->setModel($tm,$fields['edit'],$fields['grid']);

because using  $c = $this->add('CRUD')->setModel($tm,$fields['edit'],$fields['grid']); added it the page - so the Pigs CRUD was appearing on every tab.

and I was beginning to lose my mind because, as you said, $c = $t->add('CRUD')->setModel($tm,$fields['edit'],$fields['grid']); returns the Model and not CRUD.

So I have amended the CRUD declaration as perr your suggestion and it works great, I have a handle to the form.

6) Why you need these lines at all?

    if ($f = $c->form) {
        $f->addField('text','sire');
    }
Can't you simply add "sire" field in $fields['edit'] ?

I want to add client side behaviour to the form depending on other field values. e.g There is a field called sire_locale which is radio with values L or F - if L then the sire lives on the farm and there will be a Livestock record that we can select from a dropdown (hence my earlier post about self-referencing models). If F is selected then the sire is from another farm and we don't have records for it - so we have a text input to make notes about the sire.  I only want to show the correct input for the sire (either sire_id or sire_foreign) - not both.

Anyway - thanks for your help again.

Imants Horsts

unread,
Nov 1, 2012, 6:04:24 PM11/1/12
to agile-too...@googlegroups.com
You're welcome!
Reply all
Reply to author
Forward
0 new messages