Jump to content


Setting Default Value for Hidden Field


  • Please log in to reply
3 replies to this topic

#1 Mugs321

Mugs321

    Member

  • Members
  • PipPip
  • 16 posts
  • LocationCanada

Posted 30 June 2018 - 03:34 PM

Hi all,

 

I've got an N2N relationship and would like to hide the relation field and automatically (via code) set its value.  I tried the following, but I get this error:

 

Unknown column 'Game' in 'field list'

 

Code:

//Main table
$crud->setTable('item');
$crud->setSubject('Item', 'Items');

//Build relationship between games and items (make sure to hide reference fieldname, will add automatically before add & update)
$crud->setRelationNtoN('Game', 'game_uses_item', 'game', 'ItemID', 'GameID', 'game_shortcode', null);
$crud->unsetFields(['Game']);

//On add and update, default to current game and move files from import folder to common_items
function doAddOrUpdate($stateParameters, $inputFilePath, $gameID) {
	//Default to current game
	$stateParameters->data['Game'] = $gameID;

	//Move files to common_items
	return doImportMove($stateParameters, $inputFilePath);
}

//Once data saved to db, need to move files to correct location
$crud->callbackBeforeInsert(function ($stateParameters) use ($inputFilePath, $gameID) {
	return doAddOrUpdate($stateParameters, $inputFilePath, $gameID);
});

//Once data saved to db, need to move files to correct location
$crud->callbackBeforeUpdate(function ($stateParameters) use ($inputFilePath, $gameID) {
	return doAddOrUpdate($stateParameters, $inputFilePath, $gameID);
});

It looks like "Game" field is being removed from the entire dataset.  Is there any way to do this?

 

Cheers,

Dave

 



#2 web-johnny

web-johnny

    grocery CRUD Author

  • Administrators
  • 1,093 posts
  • LocationLondon

Posted 10 July 2018 - 04:17 PM

Hello Dave,

 

Yes, basically the unsetField is using to completely remove the field that this is not what you are trying to do. What you are trying to do (if I understood correctly) is:

- Show the relation n to n data on the datagrid

- Hide the field for relation n to n as the data will be updated on the callbacks

 

So you will need to do 2 changes at your code:

 

Replace:
 

$crud->unsetFields(['Game']);

with:

$crud->fieldTypeFormFields('Game', 'hidden');

this will specify that only for the formFields (add/edit/view/clone) this field will be hidden.

 

second change that you will need to do is to update your data by using callback AFTER insert and update. So you will have:

 

//Once data saved to db, need to move files to correct location
$crud->callbackAfterInsert(function ($stateParameters) use ($inputFilePath, $gameID) {
    return doAddOrUpdate($stateParameters, $inputFilePath, $gameID);
});

//Once data saved to db, need to move files to correct location
$crud->callbackAfterUpdate(function ($stateParameters) use ($inputFilePath, $gameID) {
    return doAddOrUpdate($stateParameters, $inputFilePath, $gameID);
});

So your final code will look something like this:

//Main table
$crud->setTable('item');
$crud->setSubject('Item', 'Items');

//Build relationship between games and items (make sure to hide reference fieldname, will add automatically before add & update)
$crud->setRelationNtoN('Game', 'game_uses_item', 'game', 'ItemID', 'GameID', 'game_shortcode', null);
$crud->fieldTypeFormFields('Game', 'hidden');

//On add and update, default to current game and move files from import folder to common_items
function doAddOrUpdate($stateParameters, $inputFilePath, $gameID) {
	//Default to current game
	$stateParameters->data['Game'] = $gameID;

	//Move files to common_items
	return doImportMove($stateParameters, $inputFilePath);
}

//Once data saved to db, need to move files to correct location
$crud->callbackAfterInsert(function ($stateParameters) use ($inputFilePath, $gameID) {
	return doAddOrUpdate($stateParameters, $inputFilePath, $gameID);
});

//Once data saved to db, need to move files to correct location
$crud->callbackAfterUpdate(function ($stateParameters) use ($inputFilePath, $gameID) {
	return doAddOrUpdate($stateParameters, $inputFilePath, $gameID);
});

Regards

Johnny


Posted Image

#3 okyrahmanto

okyrahmanto

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 01 September 2018 - 06:48 AM

Hi Jhonny

 

i was trying for using default value for hidden fieldtype

 

i was use it easily with grocery crud comunity

 

public function kontrak_barang() {
        //
        $id_kontrak = $this->uri->segment(3);
        $crud = $this->_getGroceryCrudEnterprise();
        $crud->setTable('tb_kontrak_barang');
        $crud->setSkin('bootstrap-v4');
        $crud->fieldType('id_kontrak','hidden',$id_kontrak); // this where i struggling beacuse the result always 0
        $crud->setRelation('id_barang','tb_barang','nama_barang');
        $crud->where(['tb_kontrak_barang.id_kontrak = ?' => $id_kontrak]);
        $output = $crud->render();
        $this->_exe_output($output);
    }

 

 

but it doesn't work with enterprise version

can you tell me how to do it right?

sorry for bad english  :)



#4 rdroguett

rdroguett

    Member

  • Members
  • PipPip
  • 18 posts

Posted 22 January 2019 - 04:15 AM

i have a some error alway is 0






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users