{parts/gtag_head.html} {parts/gtag_body.html}
Mês do Programador

Vamos comemorar juntos o seu mês.
Aproveite condições especiais.

Cursos, combos e assinaturas com desconto exclusivo para comemorar o Dia do Programador. Tempo limitado.

Promoção válida de 14/09 à 25/09

De 30% Até50% OFF
Gravação de TFieldList ...
MA
Gravação de TFieldList  


 
  1. <?php
  2. /**
  3. * Evento_aplicacaoForm Registration
  4. * @author <your name here>
  5. */
  6. class Evento_aplicacaoForm extends TPage
  7. {
  8. protected $form; // form
  9. protected $fieldlist;
  10. /**
  11. * Class constructor
  12. * Creates the page and the registration form
  13. */
  14. function __construct()
  15. {
  16. parent::__construct();
  17. // creates the form
  18. $this->form = new TForm('form_Evento_aplicacao');
  19. $this->form->class = 'tform'; // CSS class
  20. $this->form->style = 'width: 100%';
  21. // add a table inside form
  22. $table = new TTable;
  23. $table-> width = '100%';
  24. $this->form->add($table);
  25. // add a row for the form title
  26. $row = $table->addRow();
  27. $row->class = 'tformtitle'; // CSS class
  28. $row->addCell( new TLabel('Formulário - Evento Específico') )->colspan = 6;
  29. // create the form fields
  30. $id = new TEntry('id');
  31. $empreendimento_id = new TDBCombo('empreendimento_id','sqlserver','Empreendimento','id','sigla');
  32. $aplicacao_id = new TCombo('aplicacao_id');
  33. $evento = new TEntry('evento');
  34. $grupo_id = new TDBCombo('grupo_id', 'sqlserver','Grupo','id','descricao');
  35. $indicador = new TCombo('indicador');
  36. // set change action for empreendimento
  37. $change_action = new TAction(array($this, 'onChange'));
  38. $empreendimento_id->setChangeAction($change_action);
  39. // define the sizes
  40. $id->setSize(100);
  41. $id->setEditable(false);
  42. $evento->setSize(500);
  43. $grupo_id->setSize(200);
  44. $aplicacao_id->setSize(200);
  45. $empreendimento_id->setSize(200);
  46. $indicador->setSize(100);
  47. $items = array();
  48. $items['1'] ='Sim';
  49. $items['0'] ='Não';
  50. $indicador->addItems($items);
  51. // validations
  52. $evento->addValidation('Evento', new TRequiredValidator);
  53. $grupo_id->addValidation('Grupo', new TRequiredValidator);
  54. $aplicacao_id->addValidation('Aplicação', new TRequiredValidator);
  55. $empreendimento_id->addValidation('Empreendimento', new TRequiredValidator);
  56. $indicador->addValidation('Indicador?', new TRequiredValidator);
  57. $row = $table->addRow();
  58. $row->addCell(new TLabel('ID:'));
  59. $row->addCell($id);
  60. $row->addCell($label_empreendimento_id = new TLabel('Empreendimento:'));
  61. $row->addCell($empreendimento_id);
  62. $row->addCell($label_aplicacao_id = new TLabel('Aplicação:'));
  63. $row->addCell($aplicacao_id);
  64. $row = $table->addRow();
  65. $row->addCell($label_evento = new TLabel('Evento:'));
  66. $row->addCell($evento)->colspan = 3;
  67. $row->addCell($label_grupo_id = new TLabel('Grupo:'));
  68. $row->addCell($grupo_id);
  69. $row = $table->addRow();
  70. $row->addCell($label_indicador = new TLabel('Indicador?'));
  71. $row->addCell($indicador);
  72. $label_evento->setFontColor('#FF0000');
  73. $label_grupo_id->setFontColor('#FF0000');
  74. $label_aplicacao_id->setFontColor('#FF0000');
  75. $label_empreendimento_id->setFontColor('#FF0000');
  76. $label_indicador->setFontColor('#FF0000');
  77. //--------dados do frame---------
  78. //##################################
  79. $frame_solucao = new TFrame(NULL, 350);
  80. $frame_solucao->setLegend('Soluções para o evento');
  81. $frame_solucao->style .= ';margin: 4px';
  82. // $solucao = new TMultiField('solucao');
  83. // $solucao->setClass('Solucao');
  84. $this->fieldlist = new TFieldList;
  85. $this->fieldlist->name = 'Solucao';
  86. $this->fieldlist->width = '100%';
  87. // $this->form->add($this->fieldlist);
  88. $this->fieldlist->generateAria();
  89. $solucao_id = new TEntry('solucao_id');
  90. $solucao_id->setSize(50);
  91. //$solucao_id->setEditable(false);
  92. $solucao_descricao = new TEntry('descricao');
  93. $solucao_descricao->setSize(600);
  94. $this->fieldlist->addHeader();
  95. $this->fieldlist->enableSorting();
  96. // $solucao->setHeight(200);
  97. $this->fieldlist->addField('<b>ID</br>',$solucao_id , ['width' => '50%'] );
  98. $this->fieldlist->addField('<b>Descrição</br>', $solucao_descricao, ['width' => '200%']);
  99. // $solucao->addField('id', 'ID', $solucao_id, 50);
  100. // $solucao->addField('descricao',('Solução'), $solucao_descricao, 600);
  101. // $solucao->setOrientation('horizontal');
  102. // $this->form->addContent( [new TLabel('Solucao')], [$this->fieldlist]);
  103. $frame_solucao->add($this->fieldlist);
  104. $row=$table->addRow();
  105. $cell = $row->addCell($frame_solucao);
  106. $cell->colspan = 6;
  107. //----------------------------------------------------------------------------------
  108. $this->form->setFields(array($id,$evento,$grupo_id,$aplicacao_id,$empreendimento_id,$indicador));
  109. //$this->form->addContent( [$this->fieldlist] );
  110. // create the form actions
  111. $save_button = TButton::create('save', array($this, 'onSave'), _t('Save'), 'ico_save.png');
  112. $new_button = TButton::create('new', array($this, 'onEdit'), _t('New'), 'far:edit blue fa-lg');
  113. $list_button = TButton::create('list', array('Evento_aplicacaoList', 'onReload'), (_t('Back to the listing')), 'ico_datagrid.png');
  114. $this->form->addField($save_button);
  115. $this->form->addField($new_button);
  116. $this->form->addField($list_button);
  117. $buttons_box = new THBox;
  118. $buttons_box->add($save_button);
  119. $buttons_box->add($new_button);
  120. $buttons_box->add($list_button);
  121. // add a row for the form action
  122. $row = $table->addRow();
  123. $row->class = 'tformaction'; // CSS class
  124. $row->addCell($buttons_box)->colspan = 6;
  125. $container = new TTable;
  126. $container->style = 'width: 80%';
  127. $container->addRow()->addCell(new TXMLBreadCrumb('menu.xml', 'Evento_aplicacaoList'));
  128. $container->addRow()->addCell($this->form);
  129. // add the form to the page_
  130. parent::add($container);
  131. }
  132. /**
  133. * method onSave()
  134. * Executed whenever the user clicks at the save button
  135. */
  136. function onSave ($param)
  137. {
  138. try
  139. {
  140. var_dump($param);
  141. TTransaction::open('sqlserver'); // open a transaction
  142. // get the form data into an active record Evento_aplicacao
  143. $object = $this->form->getData('Evento_aplicacao');
  144. $this->form->validate(); // form validation
  145. if( $object->solucao ) {
  146. foreach( $object->solucao as $solucao ){
  147. $object->addSolucao( $solucao );
  148. //print_r($solucao);
  149. }
  150. }
  151. if( !empty($param['evento_aplicacao_id']) && is_array($param['evento_aplicacao_id']) )
  152. {
  153. foreach( $param['evento_aplicacao_id'] as $row => $descricao)
  154. {
  155. $solucao = new Solucao;
  156. $solucao->id = $param['solucao_id'][$row];
  157. $solucao->descricao = $param['descricao'][$row];
  158. $solucao->store();
  159. }
  160. }
  161. /* $data = new stdClass;
  162. $data->id = $cliente->id;
  163. TForm::sendData('form_cliente', $data);*/
  164. $object->store(); // stores the object
  165. //busca os dados do evento para exibir
  166. $object = new Evento_aplicacao($object->id);
  167. $object->solucao = $object->getSolucaos();
  168. //------------uso do send data-------------------------------
  169. $obj = new StdClass;
  170. $obj->empreendimento_id = $object->empreendimento_id; // will fire change action
  171. $obj->aplicacao_id = $object->aplicacao_id; // will update the value after the change action
  172. // send some data to form dynamically
  173. TForm::sendData('form_Evento_aplicacao', $obj);
  174. //--------------------------------------------
  175. $this->form->setData($object); // keep form data
  176. // close the transaction
  177. // shows the success message
  178. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  179. TTransaction::close();
  180. } catch (Exception $e){ // in case of exception
  181. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  182. //------------uso do send data-------------------------------
  183. $obj = new StdClass;
  184. $obj->empreendimento_id = $object->empreendimento_id; // will fire change action
  185. $obj->aplicacao_id = $object->aplicacao_id; // will update the value after the change action
  186. // send some data to form dynamically
  187. TForm::sendData('form_Evento_aplicacao', $obj);
  188. //--------------------------------------------
  189. $this->form->setData( $this->form->getData() ); // keep form data
  190. TTransaction::rollback(); // undo all pending operations
  191. }
  192. }
  193. /**
  194. * method onEdit()
  195. * Executed whenever the user clicks at the edit button da datagrid
  196. */
  197. function onEdit($param)
  198. {
  199. try
  200. {
  201. if (isset($param['key']))
  202. {
  203. $key=$param['key']; // get the parameter $key
  204. TTransaction::open('sqlserver'); // open a transaction
  205. $object = new Evento_aplicacao($key); // instantiates the Active Record
  206. //-------------------------------------------
  207. $obj = new StdClass;
  208. $obj->empreendimento_id = $object->empreendimento_id; // will fire change action
  209. $obj->aplicacao_id = $object->aplicacao_id; // will update the value after the change action
  210. $object2 = Solucao::where('evento_aplicacao_id', '=', $key)
  211. ->orderBy('id')
  212. ->load();
  213. $object->solucao = $object->getSolucaos();
  214. foreach ($object2 as $evento_aplicacao)
  215. {
  216. $this->fieldlist->addDetail($evento_aplicacao);
  217. }
  218. $this->fieldlist->addCloneAction();
  219. // send some data to form dynamically
  220. TForm::sendData('form_Evento_aplicacao', $obj);
  221. //--------------------------------------------
  222. $this->form->setData($object); // fill the form
  223. TTransaction::close(); // close the transaction
  224. }
  225. else
  226. {
  227. $this->fieldlist->addDetail( new stdClass );
  228. $this->form->clear();
  229. }
  230. }
  231. catch (Exception $e) // in case of exception
  232. {
  233. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  234. TTransaction::rollback(); // undo all pending operations
  235. }
  236. }
  237. //função para carregar as aplicações
  238. public static function onChange($param)
  239. {
  240. TTransaction::open('sqlserver');
  241. $criteria = new TCriteria;
  242. $criteria->add(new TFilter('empreendimento_id', '=', $param['empreendimento_id']));
  243. $repository = new TRepository('Aplicacao');
  244. $aplicacaos = $repository->load($criteria);
  245. $options = array();
  246. foreach ($aplicacaos as $aplicacao){
  247. $options[$aplicacao->id] = $aplicacao->sigla;
  248. }
  249. //TForm::sendData('form_Evento_aplicacao', $options);
  250. TCombo::reload('form_Evento_aplicacao', 'aplicacao_id', $options);
  251. }
  252. }
  253. ?>


Qual é o problema ao gravar esse TFieldList? Os dados que vieram do outro componente foi gravado, mas o TFieldList não grava.
Editado 17/07/2023 (há 3 anos) - Clique para ver alterações

Curso Dominando o Adianti Framework

O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado!


Dominando o Adianti Framework Quero me inscrever agora!

Comentários (2)


PD

Eu não vi declarados no form os campos da FieldList.
Veja nesse exemplo:
https://framework.adianti.me/tutor/index.php?class=FormFieldListView

Sem todos esses passos, não irá passar no POST:
 
  1. <?php
  2. $number = new TEntry('number[]');
  3. $this->fieldlist->addField( '<b>Number</b>', $number, ['width' => '25%', 'sum' => true] );
  4. $this->form->addField($number);
  5. ?>
MA

Consegui solucionar o problema com essa e outras modificações. Muito obrigado.