MA
Gravação de TFieldList
- <?php
- /**
- * Evento_aplicacaoForm Registration
- * @author <your name here>
- */
- class Evento_aplicacaoForm extends TPage
- {
- protected $form; // form
- protected $fieldlist;
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct()
- {
- parent::__construct();
- // creates the form
- $this->form = new TForm('form_Evento_aplicacao');
- $this->form->class = 'tform'; // CSS class
- $this->form->style = 'width: 100%';
- // add a table inside form
- $table = new TTable;
- $table-> width = '100%';
- $this->form->add($table);
- // add a row for the form title
- $row = $table->addRow();
- $row->class = 'tformtitle'; // CSS class
- $row->addCell( new TLabel('Formulário - Evento Específico') )->colspan = 6;
- // create the form fields
- $id = new TEntry('id');
- $empreendimento_id = new TDBCombo('empreendimento_id','sqlserver','Empreendimento','id','sigla');
- $aplicacao_id = new TCombo('aplicacao_id');
- $evento = new TEntry('evento');
- $grupo_id = new TDBCombo('grupo_id', 'sqlserver','Grupo','id','descricao');
- $indicador = new TCombo('indicador');
- // set change action for empreendimento
- $change_action = new TAction(array($this, 'onChange'));
- $empreendimento_id->setChangeAction($change_action);
- // define the sizes
- $id->setSize(100);
- $id->setEditable(false);
- $evento->setSize(500);
- $grupo_id->setSize(200);
- $aplicacao_id->setSize(200);
- $empreendimento_id->setSize(200);
- $indicador->setSize(100);
- $items = array();
- $items['1'] ='Sim';
- $items['0'] ='Não';
- $indicador->addItems($items);
- // validations
- $evento->addValidation('Evento', new TRequiredValidator);
- $grupo_id->addValidation('Grupo', new TRequiredValidator);
- $aplicacao_id->addValidation('Aplicação', new TRequiredValidator);
- $empreendimento_id->addValidation('Empreendimento', new TRequiredValidator);
- $indicador->addValidation('Indicador?', new TRequiredValidator);
- $row = $table->addRow();
- $row->addCell(new TLabel('ID:'));
- $row->addCell($id);
- $row->addCell($label_empreendimento_id = new TLabel('Empreendimento:'));
- $row->addCell($empreendimento_id);
- $row->addCell($label_aplicacao_id = new TLabel('Aplicação:'));
- $row->addCell($aplicacao_id);
- $row = $table->addRow();
- $row->addCell($label_evento = new TLabel('Evento:'));
- $row->addCell($evento)->colspan = 3;
- $row->addCell($label_grupo_id = new TLabel('Grupo:'));
- $row->addCell($grupo_id);
- $row = $table->addRow();
- $row->addCell($label_indicador = new TLabel('Indicador?'));
- $row->addCell($indicador);
- $label_evento->setFontColor('#FF0000');
- $label_grupo_id->setFontColor('#FF0000');
- $label_aplicacao_id->setFontColor('#FF0000');
- $label_empreendimento_id->setFontColor('#FF0000');
- $label_indicador->setFontColor('#FF0000');
- //--------dados do frame---------
- //##################################
- $frame_solucao = new TFrame(NULL, 350);
- $frame_solucao->setLegend('Soluções para o evento');
- $frame_solucao->style .= ';margin: 4px';
- // $solucao = new TMultiField('solucao');
- // $solucao->setClass('Solucao');
- $this->fieldlist = new TFieldList;
- $this->fieldlist->name = 'Solucao';
- $this->fieldlist->width = '100%';
- // $this->form->add($this->fieldlist);
- $this->fieldlist->generateAria();
- $solucao_id = new TEntry('solucao_id');
- $solucao_id->setSize(50);
- //$solucao_id->setEditable(false);
- $solucao_descricao = new TEntry('descricao');
- $solucao_descricao->setSize(600);
- $this->fieldlist->addHeader();
- $this->fieldlist->enableSorting();
- // $solucao->setHeight(200);
- $this->fieldlist->addField('<b>ID</br>',$solucao_id , ['width' => '50%'] );
- $this->fieldlist->addField('<b>Descrição</br>', $solucao_descricao, ['width' => '200%']);
- // $solucao->addField('id', 'ID', $solucao_id, 50);
- // $solucao->addField('descricao',('Solução'), $solucao_descricao, 600);
- // $solucao->setOrientation('horizontal');
- // $this->form->addContent( [new TLabel('Solucao')], [$this->fieldlist]);
- $frame_solucao->add($this->fieldlist);
- $row=$table->addRow();
- $cell = $row->addCell($frame_solucao);
- $cell->colspan = 6;
- //----------------------------------------------------------------------------------
- $this->form->setFields(array($id,$evento,$grupo_id,$aplicacao_id,$empreendimento_id,$indicador));
- //$this->form->addContent( [$this->fieldlist] );
- // create the form actions
- $save_button = TButton::create('save', array($this, 'onSave'), _t('Save'), 'ico_save.png');
- $new_button = TButton::create('new', array($this, 'onEdit'), _t('New'), 'far:edit blue fa-lg');
- $list_button = TButton::create('list', array('Evento_aplicacaoList', 'onReload'), (_t('Back to the listing')), 'ico_datagrid.png');
- $this->form->addField($save_button);
- $this->form->addField($new_button);
- $this->form->addField($list_button);
- $buttons_box = new THBox;
- $buttons_box->add($save_button);
- $buttons_box->add($new_button);
- $buttons_box->add($list_button);
- // add a row for the form action
- $row = $table->addRow();
- $row->class = 'tformaction'; // CSS class
- $row->addCell($buttons_box)->colspan = 6;
- $container = new TTable;
- $container->style = 'width: 80%';
- $container->addRow()->addCell(new TXMLBreadCrumb('menu.xml', 'Evento_aplicacaoList'));
- $container->addRow()->addCell($this->form);
- // add the form to the page_
- parent::add($container);
- }
- /**
- * method onSave()
- * Executed whenever the user clicks at the save button
- */
- function onSave ($param)
- {
- try
- {
- var_dump($param);
- TTransaction::open('sqlserver'); // open a transaction
- // get the form data into an active record Evento_aplicacao
- $object = $this->form->getData('Evento_aplicacao');
- $this->form->validate(); // form validation
- if( $object->solucao ) {
- foreach( $object->solucao as $solucao ){
- $object->addSolucao( $solucao );
- //print_r($solucao);
- }
- }
- if( !empty($param['evento_aplicacao_id']) && is_array($param['evento_aplicacao_id']) )
- {
- foreach( $param['evento_aplicacao_id'] as $row => $descricao)
- {
- $solucao = new Solucao;
- $solucao->id = $param['solucao_id'][$row];
- $solucao->descricao = $param['descricao'][$row];
- $solucao->store();
- }
- }
- /* $data = new stdClass;
- $data->id = $cliente->id;
- TForm::sendData('form_cliente', $data);*/
- $object->store(); // stores the object
- //busca os dados do evento para exibir
- $object = new Evento_aplicacao($object->id);
- $object->solucao = $object->getSolucaos();
- //------------uso do send data-------------------------------
- $obj = new StdClass;
- $obj->empreendimento_id = $object->empreendimento_id; // will fire change action
- $obj->aplicacao_id = $object->aplicacao_id; // will update the value after the change action
- // send some data to form dynamically
- TForm::sendData('form_Evento_aplicacao', $obj);
- //--------------------------------------------
- $this->form->setData($object); // keep form data
- // close the transaction
- // shows the success message
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
- TTransaction::close();
- } catch (Exception $e){ // in case of exception
- new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
- //------------uso do send data-------------------------------
- $obj = new StdClass;
- $obj->empreendimento_id = $object->empreendimento_id; // will fire change action
- $obj->aplicacao_id = $object->aplicacao_id; // will update the value after the change action
- // send some data to form dynamically
- TForm::sendData('form_Evento_aplicacao', $obj);
- //--------------------------------------------
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * method onEdit()
- * Executed whenever the user clicks at the edit button da datagrid
- */
- function onEdit($param)
- {
- try
- {
- if (isset($param['key']))
- {
- $key=$param['key']; // get the parameter $key
- TTransaction::open('sqlserver'); // open a transaction
- $object = new Evento_aplicacao($key); // instantiates the Active Record
- //-------------------------------------------
- $obj = new StdClass;
- $obj->empreendimento_id = $object->empreendimento_id; // will fire change action
- $obj->aplicacao_id = $object->aplicacao_id; // will update the value after the change action
- $object2 = Solucao::where('evento_aplicacao_id', '=', $key)
- ->orderBy('id')
- ->load();
- $object->solucao = $object->getSolucaos();
- foreach ($object2 as $evento_aplicacao)
- {
- $this->fieldlist->addDetail($evento_aplicacao);
- }
- $this->fieldlist->addCloneAction();
- // send some data to form dynamically
- TForm::sendData('form_Evento_aplicacao', $obj);
- //--------------------------------------------
- $this->form->setData($object); // fill the form
- TTransaction::close(); // close the transaction
- }
- else
- {
- $this->fieldlist->addDetail( new stdClass );
- $this->form->clear();
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- //função para carregar as aplicações
- public static function onChange($param)
- {
- TTransaction::open('sqlserver');
- $criteria = new TCriteria;
- $criteria->add(new TFilter('empreendimento_id', '=', $param['empreendimento_id']));
- $repository = new TRepository('Aplicacao');
- $aplicacaos = $repository->load($criteria);
- $options = array();
- foreach ($aplicacaos as $aplicacao){
- $options[$aplicacao->id] = $aplicacao->sigla;
- }
- //TForm::sendData('form_Evento_aplicacao', $options);
- TCombo::reload('form_Evento_aplicacao', 'aplicacao_id', $options);
- }
- }
- ?>
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á 9 meses) - Clique para ver alterações
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:
Consegui solucionar o problema com essa e outras modificações. Muito obrigado.