Metodo onDelete parâmetro $key = Undefined index: key Boa noite a todos, tenho um formulário de cadastro e implementei um botão de excluir, porém na action delete não está chegando o parâmetro $key, gostaria de saber onde errei, utilizando o var_dump verifiquei que na linha 202 o parâmetro desaparece, Obrigado ...
MC
Metodo onDelete parâmetro $key = Undefined index: key  
Boa noite a todos, tenho um formulário de cadastro e implementei um botão de excluir, porém na action delete não está chegando o parâmetro $key, gostaria de saber onde errei, utilizando o var_dump verifiquei que na linha 202 o parâmetro desaparece, Obrigado

 
  1. <?php
 
  1. <?php
  2. /**
  3. * Imagem_Marca do gadoForm
  4. * @author <your name here>
  5. */
  6. class Imagem_MarcagadoForm extends TPage
  7. {
  8. protected $form; // form
  9. /**
  10. * Form constructor
  11. * @param $param Request
  12. */
  13. public function __construct( $param )
  14. {
  15. parent::__construct();
  16. // creates the form
  17. $this->form = new TQuickForm('form_Imagem_Marcagado');
  18. $this->form->class = 'tform'; // change CSS class
  19. $this->form = new BootstrapFormWrapper($this->form);
  20. $this->form->style = 'display: table;width:100%'; // change style
  21. // define the form title
  22. $this->form->setFormTitle('Imagem_Marcagado');
  23. // create the form fields
  24. $id = new TEntry('id');
  25. $nome = new TSlim('nome');
  26. $iniciais = new TEntry('iniciais');
  27. $propriedade_id = new TEntry('propriedade_id');
  28. $nome->setSize( '100%', 100);
  29. $nome->setDataProperties(['label'=>'Upload marcador']);
  30. // add the fields
  31. $this->form->addQuickField('Id', $id, '100%' );
  32. $this->form->addQuickField('Nome', $nome, '100%' );
  33. $this->form->addQuickField('Iniciais', $iniciais, '100%' , new TRequiredValidator);
  34. $this->form->addQuickField('Propriedade', $propriedade_id, '50%' );
  35. if (!empty($id))
  36. {
  37. $id->setEditable(FALSE);
  38. }
  39. if (!empty($propriedade_id))
  40. {
  41. $propriedade_id->setEditable(FALSE);
  42. }
  43. // create the form actions
  44. $btn = $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
  45. $btn2 = $this->form->addQuickAction(_t('Delete'), new TAction(array($this, 'onDelete')), 'fa:trash-o red fa-lg');
  46. $btn->class = 'btn btn-sm btn-primary';
  47. $this->form->addQuickAction('Voltar', new TAction(['PropriedadeForm', 'onEdit'] ,['key'=>TSession::getValue('propriedade')]), 'fa:table blue');
  48. // vertical box container
  49. $container = new TVBox;
  50. $container->style = 'width: 90%';
  51. // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  52. $container->add(TPanelGroup::pack('Title', $this->form));
  53. parent::add($container);
  54. }
  55. /**
  56. * Save form data
  57. * @param $param Request
  58. */
  59. public function onSave( $param )
  60. {
  61. try
  62. {
  63. TTransaction::open('permission'); // open a transaction
  64. /**
  65. // Enable Debug logger for SQL operations inside the transaction
  66. TTransaction::setLogger(new TLoggerSTD); // standard output
  67. TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  68. **/
  69. $this->form->validate(); // validate form data
  70. $object = new Imagem_Marcagado;
  71. $data = $this->form->getData(); // get form data as array
  72. $object->fromArray( (array) $data); // load the object with data
  73. // Get posted data
  74. $imagem = Slim::getImages();
  75. $propriedade = TSession::getValue('propriedade');
  76. // No image found under the supplied input name
  77. if ($imagem)
  78. {
  79. $imagem = $imagem[0];
  80. // save output data if set
  81. if (isset($imagem['output']['data']))
  82. {
  83. // Save the file
  84. $name = $imagem['output']['name'];
  85. // We'll use the output crop data
  86. $data = $imagem['output']['data'];
  87. $output = Slim::saveFile($data, $name, 'images/marcagado/', false);
  88. $object->nome = $output['path'];
  89. }
  90. }
  91. $object->propriedade_id = $propriedade;
  92. if (empty($object->nome))
  93. {
  94. throw new Exception('Campo nome é obrigatório');
  95. }
  96. $object->store(); // save the object
  97. // get the generated id
  98. //$data->id = $object->id;
  99. $this->form->setData($data); // fill form data
  100. TTransaction::close(); // close the transaction
  101. //new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  102. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'), new TAction(['PropriedadeForm', 'onEdit'] ,['key'=>TSession::getValue('propriedade')]) );
  103. }
  104. catch (Exception $e) // in case of exception
  105. {
  106. new TMessage('error', $e->getMessage()); // shows the exception error message
  107. $this->form->setData( $this->form->getData() ); // keep form data
  108. TTransaction::rollback(); // undo all pending operations
  109. }
  110. }
  111. /**
  112. * Clear form data
  113. * @param $param Request
  114. */
  115. public function onClear( $param )
  116. {
  117. $this->form->clear(TRUE);
  118. }
  119. /**
  120. * Load object to form data
  121. * @param $param Request
  122. */
  123. public function onEdit( $param )
  124. {
  125. try
  126. {
  127. if (isset($param['key']))
  128. {
  129. $key = $param['key']; // get the parameter $key
  130. TTransaction::open('permission'); // open a transaction
  131. $object = new Imagem_Marcagado($key); // instantiates the Active Record
  132. $this->form->setData($object); // fill the form
  133. TTransaction::close(); // close the transaction
  134. }
  135. else
  136. {
  137. $this->form->clear(TRUE);
  138. }
  139. }
  140. catch (Exception $e) // in case of exception
  141. {
  142. new TMessage('error', $e->getMessage()); // shows the exception error message
  143. TTransaction::rollback(); // undo all pending operations
  144. }
  145. }
  146. public function onDelete($param)
  147. {
  148. // define the delete action
  149. $action = new TAction(array($this, 'Delete'));
  150. $action->setParameters($param); // pass the key parameter ahead
  151. // shows a dialog to the user
  152. new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  153. }
  154. /**
  155. * Delete a record
  156. */
  157. public function Delete($param)
  158. {
  159. try
  160. {
  161. //$key = $param['key']; // get the parameter $key
  162. TTransaction::open('permission'); // open a transaction with database
  163. $object = new Imagem_Marcagado($key, FALSE); // instantiates the Active Record
  164. $object->delete(); // deletes the object from the database
  165. TTransaction::close(); // close the transaction
  166. new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted')); // success message
  167. }
  168. catch (Exception $e) // in case of exception
  169. {
  170. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  171. TTransaction::rollback(); // undo all pending operations
  172. }
  173. }
  174. }
  175. ?>

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 (3)


NR

A linha onde a variável $key é atribuída está comentada:
 
  1. <?php
  2. // linha 200
  3. //$key = $param['key']; // get the parameter $key
  4. ?>
MC

Obrigado pela ajuda, descomentei a linha 200 e como havia falado anteriormente o valor não chega no parâmetro, ainda continua apresentando erro e o registro não é apagado.

Notice: Undefined index: key in C:wampwwwpatrulharural2appcontrolpatrulhaincludesImagem_MarcagadoForm.class.php on line 200

 
  1. <?php
  2. public function Delete($param)
  3. {
  4. try
  5. {
  6. $key = $param['key']; // get the parameter $key
  7. TTransaction::open('permission'); // open a transaction with database
  8. $object = new Imagem_Marcagado($key, FALSE); // instantiates the Active Record
  9. $object->delete(); // deletes the object from the database
  10. TTransaction::close(); // close the transaction
  11. new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted')); // success message
  12. //new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'), new TAction(['PropriedadeForm', 'onEdit'] ,['key'=>TSession::getValue('propriedade')]) );
  13. }
  14. catch (Exception $e) // in case of exception
  15. {
  16. new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
  17. TTransaction::rollback(); // undo all pending operations
  18. }
  19. }
  20. ?>


Se substituo a linha 202 e mudo o parâmetro $key para $param['id'] conforme abaixo, o registro é apagado.

$object = new Imagem_Marcagado($param['id'], FALSE)

Obrigado
NR

Agora que vi que você está chamando a função Delete através de um botão no formulário. Acontece que a chave "key" é usada pelas ações da datagrid. Ao usar um botão em um formulário essa chave não é utilizada e a variável $param vai conter os dados dos campos do formulário.