Conheça  A Ferramenta LowCode mais moderna e veloz para desenvolvimento PHP: Adianti Creator
Fomulario com Imagem Será que alguem poderia me dar um exemplo de um formulario com imagem que o caminho desta imagem venha da base de dados....
LJ
Fomulario com Imagem  
Fechado
Será que alguem poderia me dar um exemplo de um formulario com imagem que o caminho desta imagem venha da base de dados.

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


LJ

Fiz um formulário simples porem esta dando o seguinte erro:

Erro: Argument 1 passed to TForm::addField must implement interface
IWidget, instance of TImage given, called in
C:siterenomearlibadiantiwidgetwebformTForm.class.php
on line 196

 
  1. <?php
  2. /**
  3. * ProductForm Registration
  4. *
  5. * @version 1.0
  6. * @package samples
  7. * @subpackage library
  8. * @author Pablo DallOglio
  9. * @copyright Copyright (c) 2006-2011 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10. * @license http://www.adianti.com.br/framework-license
  11. */
  12. class ProductForm extends TPage
  13. {
  14. private $form; // form
  15. /**
  16. * Class constructor
  17. * Creates the page and the registration form
  18. */
  19. function __construct()
  20. {
  21. parent::__construct();
  22. // creates the form
  23. $this->form = new TForm('form_Product');
  24. // creates a table
  25. $table = new TTable;
  26. $table_buttons = new TTable;
  27. // add the table inside the form
  28. $this->form->add($table);
  29. // create the form fields
  30. $id = new TEntry('id');
  31. $name = new TEntry('name');
  32. $image = new TImage('image');
  33. $name->setSize(200);
  34. $row=$table->addRow();
  35. $row->addCell(new TLabel('Id:'));
  36. $cell=$row->addCell($id);
  37. $cell->colspan=2;
  38. // add a row for the field nome
  39. $row=$table->addRow();
  40. $row->addCell(new TLabel('Nome:'));
  41. $cell=$row->addCell($name);
  42. $cell->colspan=2;
  43. // add a row for the field imagem
  44. $row=$table->addRow();
  45. $row->addCell(new TLabel('Imagem:'));
  46. $cell=$row->addCell($image);
  47. $cell->colspan=2;
  48. // create an action button (go to list)
  49. $goto_button=new TButton('list');
  50. // define the button action
  51. $goto_button->setAction(new TAction(array('ProductDataGridView', 'onReload')), 'Listagem');
  52. $goto_button->setImage('ico_datagrid.gif');
  53. // add a row for the form action
  54. $row=$table_buttons->addRow();
  55. $row->addCell($goto_button);
  56. // add a row for the form action
  57. $row=$table->addRow();
  58. $cell=$row->addCell($table_buttons);
  59. $cell->colspan=3;
  60. // define wich are the form fields
  61. $this->form->setFields(array($id,$name,$image,$goto_button));
  62. // add the form to the page
  63. parent::add($this->form);
  64. }
  65. /**
  66. * method onEdit()
  67. * Executed whenever the user clicks at the edit button da datagrid
  68. */
  69. function onEdit($param)
  70. {
  71. try
  72. {
  73. if (isset($param['key']))
  74. {
  75. // get the parameter $key
  76. $key=$param['key'];
  77. // open a transaction with database 'fotos'
  78. TTransaction::open('fotos');
  79. // instantiates object Book
  80. $object = new Product($key);
  81. // fill the form with the active record data
  82. $this->form->setData($object);
  83. TTransaction::close();
  84. }
  85. else
  86. {
  87. $this->form->clear();
  88. }
  89. }
  90. catch (Exception $e) // in case of exception
  91. {
  92. // shows the exception error message
  93. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  94. // undo all pending operations
  95. TTransaction::rollback();
  96. }
  97. }
  98. }
  99. ?>

AS

opa, pelo que entendi você quer fazer um formulario com upload de imagem?, é isso?
MS

O $image não deve estar no array do addFields(), por isso está ocorrendo este erro.
LJ

Alexandre, o que eu preciso é apenas ver a foto, dentro de um célula da tabela, terão outros campos no formulário, onde vou descrever o que vejo na foto, mas removi os outros campos pois a única coisa que não esta funcionando é ver a foto.
LJ

Mailson, removendo o $image do addFields(), realmente não dá o erro, mas mesmo assim, não aparece a foto.
no meu BD no campo image tem o caminho e nome da foto:, tipo : app/images/tf2/LJ13TF2VL0001.jpg
MS

Eu também estou com a mesma situação que você, carregando no construtor a foto aparece mas no onEdit() ela não é exibida. Vou continuar tentando se eu encontrar uma solução eu posto aqui.
Abraço.
MS

Consegui resolver o problema, o que eu precisei fazer foi adicionar a imagem a um div e então adicionar o div a table e não a imagem direto. Funcionou perfeitamente para mim.

No método construtor fica assim
$this->divFoto = new TElement('div'); $this->divFoto->id = 'divFoto'; $this->divFoto->style = "width:100px;height:100px"; $this->imgFoto = new TElement('img'); $this->imgFoto->src = ""; $this->imgFoto->style = "width:100px;height:100px"; $this->divFoto->add($this->imgFoto);


No método "onEdit()", após o "setData()":
$this->imgFoto->src = "app/images/{$empresa}/{$nomeImg}.png";
MS

Lembrando que apenas o $this->divFoto deve ser adicionado a table que será exibida no formulário.
LJ

Mailson, muito obrigado, funcionou !!!

a unica coisa que tive que mudar e não sei porque foi no metodo edit:
No método "onEdit()", após o "setData()":

$this->imgFoto->src = "app/images/{$empresa}/{$nomeImg}.png";

alterei para :

$object = new Product($key) // fill the form with the active record data $this->form->setData($object);
$this->imgFoto->src = "app/images/".$object->corrida."/".$object->name.".jpg";
FS

Amigos gostaria de saber como posso implementar no meu código essa solução que foi dita pelo Mailson da Silva.
Não consigo fazer a imagem aparecer na página.

 
  1. <?php
  2. class ExameFisicoVisaoForm extends TPage
  3. {
  4. private $form;
  5. function __construct()
  6. {
  7. parent::__construct();
  8. $this->notebook = new TNotebook;
  9. $this->notebook->setSize(950, 420);
  10. $this->form = new TForm('Examefisico');
  11. $table = new TTable;
  12. $this->form->add($this->notebook);
  13. $id = new TEntry('id');
  14. $imagem = new TImage('imagem');
  15. $table->addRowSet('Imagem', $imagem);
  16. $salvar_button = new TButton('salvar');
  17. $salvar_button->setAction(new TAction(array($this, 'onSave')), 'Salvar');
  18. $salvar_button->setImage('ico_save.png');
  19. $novo_button=new TButton('novo');
  20. $novo_button->setAction(new TAction(array($this, 'onEdit')), 'Novo');
  21. $novo_button->setImage('ico_new.png');
  22. $listar_button=new TButton('listar');
  23. $listar_button->setAction(new TAction(array('ExameFisicoVisaoGrade', 'onReload')), 'Listagem');
  24. $listar_button->setImage('ico_datagrid.png');
  25. $this->form->setFields(array($id, $salvar_button, $novo_button, $listar_button));
  26. $this->notebook->appendPage(('Exame Fisico'), $table);
  27. $table_buttons = new TTable;
  28. $row_buttons = $table_buttons->addRow();
  29. $row_buttons->addCell($salvar_button);
  30. $row_buttons->addCell($novo_button);
  31. $row_buttons->addCell($listar_button);
  32. $container = new TTable;
  33. $container->addRow()->addCell(new TXMLBreadCrumb('menu.xml', 'ExameFisicoVisaoForm'));
  34. $container->addRow()->addCell($this->form);
  35. $container->addRow()->addCell($table_buttons);
  36. parent::add($container);
  37. }
  38. function onSave()
  39. {
  40. try
  41. {
  42. TTransaction::open('conexao');
  43. $object = $this->form->getData('Examefisico');
  44. $this->form->validate();
  45. $object->store();
  46. $this->form->setData($object);
  47. TTransaction::close();
  48. new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
  49. }
  50. catch (Exception $e)
  51. {
  52. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  53. $this->form->setData( $this->form->getData() );
  54. TTransaction::rollback();
  55. }
  56. }
  57. function onEdit($param)
  58. {
  59. try
  60. {
  61. if (isset($param['key']))
  62. {
  63. $key=$param['key'];
  64. TTransaction::open('conexao');
  65. $object = new Examefisico($key);
  66. $this->form->setData($object);
  67. TTransaction::close();
  68. }
  69. else
  70. {
  71. $this->form->clear();
  72. }
  73. }
  74. catch (Exception $e)
  75. {
  76. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  77. TTransaction::rollback();
  78. }
  79. }
  80. }
  81. ?>
LJ

este é meu codigo que esta funcionando:

 
  1. <?php
  2. /**
  3. * ProductForm Registration
  4. *
  5. */
  6. class ProductForm extends TPage
  7. {
  8. private $form; // form
  9. /**
  10. * Class constructor
  11. * Creates the page and the registration form
  12. */
  13. function __construct()
  14. {
  15. parent::__construct();
  16. //loads easyzoom
  17. TPage::include_css('app/lib/jquery/easyzoom/css/example.css');
  18. TPage::include_css('app/lib/jquery/easyzoom/css/pygments.css');
  19. TPage::include_css('app/lib/jquery/easyzoom/css/easyzoom.css');
  20. TPage::include_js('app/lib/jquery/easyzoom/js/easyzoom.js');
  21. // creates the form
  22. $this->form = new TForm('form_Product');
  23. // creates a table
  24. $table = new TTable;
  25. $table_buttons = new TTable;
  26. $table_numbers = new TTable;
  27. // add the table inside the form
  28. $this->form->add($table);
  29. // create the form fields
  30. $id = new TEntry('id');
  31. $name = new TEntry('name');
  32. $n1 = new TEntry('n1');
  33. $n2 = new TEntry('n2');
  34. $n3 = new TEntry('n3');
  35. $n4 = new TEntry('n4');
  36. $outros = new TCombo('outros');
  37. $items = array();
  38. $items['ni'] = 'Nao identificado';
  39. $items['cadeirante'] = 'Cadeirante';
  40. $items['bike'] = 'Bike';
  41. $items['po'] = 'Podium';
  42. $outros->addItems($items);
  43. $name->setSize(150);
  44. $name->setProperty("tabindex","-1");
  45. $id->setProperty("tabindex","-1");
  46. $n1->setSize(40);
  47. $n2->setSize(40);
  48. $n3->setSize(40);
  49. $n4->setSize(40);
  50. $id->setSize(40);
  51. $n1->setProperty("tabindex","1");
  52. $n2->setProperty("tabindex","2");
  53. $n3->setProperty("tabindex","3");
  54. $n4->setProperty("tabindex","4");
  55. $outros->setProperty("tabindex","5");
  56. $id->setEditable(FALSE);
  57. $name->setEditable(FALSE);
  58. $n1->setMask('99999'); // define numeric input
  59. $n2->setMask('99999'); // define numeric input
  60. $n3->setMask('99999'); // define numeric input
  61. $n4->setMask('99999');
  62. $table->border=1;
  63. $table->bgcolor='#f2f2f2';
  64. $easydiv = new TElement('div');
  65. $easydiv->id = "easyzoom easyzoom--overlay";
  66. $easydiv->class = "easyzoom easyzoom--overlay";
  67. $this->imgFoto = new TElement('img');
  68. $this->imgFoto->src = "";
  69. $this->imgFoto->style = "width:300px;height:450px";
  70. $this->linka = new TElement('a');
  71. $this->linka->href = "";
  72. $this->linka->add($this->imgFoto);
  73. $easydiv->add($this->linka);
  74. $script =new TElement('script');
  75. $script->type = 'text/javascript';
  76. $script->add('
  77. // Instantiate EasyZoom plugin
  78. var $easyzoom = $(".easyzoom").easyZoom();
  79. // Get the instance API
  80. var api = $easyzoom.data("easyZoom");
  81. ');
  82. // add the script to the table
  83. //$table->addRow()->addCell($script);
  84. $easydiv->add($script);
  85. //$this->form->add($script);
  86. // tabelas com numeros
  87. $row=$table_numbers->addRow();
  88. $row->addCell(new TLabel('Id:'));
  89. $cell=$row->addCell($id);
  90. $row=$table_numbers->addRow();
  91. $row->addCell(new TLabel('Nome:'));
  92. $cell=$row->addCell($name);
  93. $row=$table_numbers->addRow();
  94. $cell=$row->addCell('Numeros:');
  95. $cell->colspan=4;
  96. $row=$table_numbers->addRow();
  97. $cell=$row->addCell($n1);
  98. $cell->colspan=4;
  99. $row=$table_numbers->addRow();
  100. $cell=$row->addCell($n2);
  101. $cell->colspan=4;
  102. $row=$table_numbers->addRow();
  103. $cell=$row->addCell($n3);
  104. $cell->colspan=4;
  105. $row=$table_numbers->addRow();
  106. $cell=$row->addCell($n4);
  107. $cell->colspan=4;
  108. $row=$table_numbers->addRow();
  109. $cell=$row->addCell($outros);
  110. $cell->colspan=4;
  111. $row=$table_numbers->addRow();
  112. $cell=$row->addCell($table_buttons);
  113. $cell->colspan=4;
  114. // create an action button (save)
  115. $save_button=new TButton('save');
  116. // define the button action
  117. $save_button->setAction(new TAction(array($this, 'onSave')), 'Salvar e Mantem');
  118. $save_button->setImage('ico_save.png');
  119. $save_button->setProperty("tabindex","6");
  120. // create an action button (proximo)
  121. $proximo_button=new TButton('proximo');
  122. // define the button action
  123. $proximo_button->setAction(new TAction(array($this, 'onProximo')), 'Salva e Proximo');
  124. $proximo_button->setImage('ico_save.png');
  125. $proximo_button->setProperty("tabindex","7");
  126. // create an action button (go to list)
  127. $goto_button=new TButton('list');
  128. // define the button action
  129. $goto_button->setAction(new TAction(array('ProductDataGridView', 'onReload')), 'Listagem');
  130. $goto_button->setImage('ico_datagrid.png');
  131. $goto_button->setProperty("tabindex","-1");
  132. // create an action button (go to list)
  133. $anterior_button=new TButton('anterior');
  134. // define the button action
  135. $anterior_button->setAction(new TAction(array($this, 'onAnterior')), 'Anterior');
  136. $anterior_button->setImage('ico_anterior.png');
  137. $anterior_button->setProperty("tabindex","-1");
  138. // create an action button (go to list)
  139. $limpar_button=new TButton('limpar');
  140. // define the button action
  141. $limpar_button->setAction(new TAction(array($this, 'onLimpar')), 'Limpar');
  142. $limpar_button->setImage('ico_delete.png');
  143. $limpar_button->setProperty("tabindex","-1");
  144. // create an action button (go to list)
  145. $deletar_button=new TButton('excluir');
  146. // define the button action
  147. $deletar_button->setAction(new TAction(array($this, 'onDelete')), 'EXCLUIR');
  148. $deletar_button->setImage('ico_delete.png');
  149. $deletar_button->setProperty("tabindex","-1");
  150. // add a row for the form action
  151. $row=$table_buttons->addRow();
  152. $row->addCell($save_button);
  153. $row->addCell($proximo_button);
  154. $row->addCell($limpar_button);
  155. $row->addCell($anterior_button);
  156. $row->addCell($deletar_button);
  157. $row->addCell($goto_button);
  158. // add a row for the form action
  159. //$row=$table->addRow();
  160. //$cell=$row->addCell($table_buttons);
  161. //$cell->colspan=3;
  162. $row=$table->addRow();
  163. //$row->addCell($this->divFoto);
  164. $row->addCell($easydiv);
  165. $row->addCell($table_numbers);
  166. // define wich are the form fields
  167. $this->form->setFields(array($id,$name,$n1,$n2,$n3,$n4,$outros,$save_button,$anterior_button,$proximo_button,$goto_button,$limpar_button,$deletar_button));
  168. // add the form to the page
  169. parent::add($this->form);
  170. }
  171. /**
  172. * method onSave()
  173. * Executed whenever the user clicks at the save button
  174. */
  175. function onSave()
  176. {
  177. try
  178. {
  179. // open a transaction with database 'library'
  180. TTransaction::open('fotos');
  181. // get the form data into an active record Book
  182. $object = $this->form->getData('Product');
  183. // stores the object
  184. $object->store();
  185. // fill the form with the active record data
  186. // mantem n1,n2,n3,n4
  187. //$this->form->setData($object);
  188. //avança para proximo
  189. $objectproximo = new Product($object->proximo);
  190. if ($objectproximo->n1 == "" and $objectproximo->outros == "") {
  191. $objectproximo->n1 = $object->n1;
  192. $objectproximo->n2 = $object->n2;
  193. $objectproximo->n3 = $object->n3;
  194. $objectproximo->n4 = $object->n4;
  195. }
  196. $this->form->setData($objectproximo);
  197. $this->imgFoto->src = "app/images/".$objectproximo->corrida."/".$objectproximo->name.".jpg";
  198. $this->linka->href = $this->imgFoto->src;
  199. // close the transaction
  200. TTransaction::close();
  201. // shows the success message
  202. //new TMessage('info', 'Registro Salvo');
  203. // reload the listing
  204. }
  205. catch (Exception $e) // in case of exception
  206. {
  207. // shows the exception error message
  208. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  209. // undo all pending operations
  210. TTransaction::rollback();
  211. }
  212. }
  213. /**
  214. * method onEdit()
  215. * Executed whenever the user clicks at the edit button da datagrid
  216. */
  217. function onEdit($param)
  218. {
  219. try
  220. {
  221. if (isset($param['key']))
  222. {
  223. // get the parameter $key
  224. $key=$param['key'];
  225. // open a transaction with database 'fotos'
  226. TTransaction::open('fotos');
  227. // instantiates object Book
  228. $object = new Product($key);
  229. // fill the form with the active record data
  230. $this->form->setData($object);
  231. $this->imgFoto->src = "app/images/".$object->corrida."/".$object->name.".jpg";
  232. $this->linka->href = $this->imgFoto->src;
  233. // close the transaction
  234. TTransaction::close();
  235. }
  236. else
  237. {
  238. $this->form->clear();
  239. }
  240. }
  241. catch (Exception $e) // in case of exception
  242. {
  243. // shows the exception error message
  244. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  245. // undo all pending operations
  246. TTransaction::rollback();
  247. }
  248. }
  249. function onAnterior()
  250. {
  251. try
  252. {
  253. // open a transaction with database 'library'
  254. TTransaction::open('fotos');
  255. // get the form data into an active record Book
  256. $object = $this->form->getData('Product');
  257. // stores the object
  258. //$object->store();
  259. // fill the form with the active record data
  260. //$this->form->setData($object);
  261. //avança para proximo
  262. $objectanterior = new Product($object->anterior);
  263. $this->form->setData($objectanterior);
  264. $this->imgFoto->src = "app/images/".$objectanterior->corrida."/".$objectanterior->name.".jpg";
  265. $this->linka->href = $this->imgFoto->src;
  266. // close the transaction
  267. TTransaction::close();
  268. // shows the success message
  269. //new TMessage('info', 'Registro Salvo');
  270. // reload the listing
  271. }
  272. catch (Exception $e) // in case of exception
  273. {
  274. // shows the exception error message
  275. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  276. // undo all pending operations
  277. TTransaction::rollback();
  278. }
  279. }
  280. function onProximo()
  281. {
  282. try
  283. {
  284. // open a transaction with database 'library'
  285. TTransaction::open('fotos');
  286. // get the form data into an active record Book
  287. $object = $this->form->getData('Product');
  288. // stores the object
  289. $object->store();
  290. // fill the form with the active record data
  291. //$this->form->setData($object);
  292. //avança para proximo
  293. $objectproximo = new Product($object->proximo);
  294. $this->form->setData($objectproximo);
  295. $this->imgFoto->src = "app/images/".$objectproximo->corrida."/".$objectproximo->name.".jpg";
  296. $this->linka->href = $this->imgFoto->src; //avança para proximo
  297. // close the transaction
  298. TTransaction::close();
  299. // shows the success message
  300. //new TMessage('info', 'Registro Salvo');
  301. // reload the listing
  302. }
  303. catch (Exception $e) // in case of exception
  304. {
  305. // shows the exception error message
  306. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  307. // undo all pending operations
  308. TTransaction::rollback();
  309. }
  310. }
  311. /**
  312. * method onEdit()
  313. * Executed whenever the user clicks at the edit button da datagrid
  314. */
  315. function onLimpar()
  316. {
  317. try
  318. {
  319. // instantiates object Book
  320. $object = $this->form->getData('Product');
  321. $object->n1="";
  322. $object->n2="";
  323. $object->n3="";
  324. $object->n4="";
  325. $object->outros="";
  326. // fill the form with the active record data
  327. $this->form->setData($object);
  328. $this->imgFoto->src = "app/images/".$object->corrida."/".$object->name.".jpg";
  329. $this->linka->href = $this->imgFoto->src;
  330. }
  331. catch (Exception $e) // in case of exception
  332. {
  333. // shows the exception error message
  334. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  335. // undo all pending operations
  336. TTransaction::rollback();
  337. }
  338. }
  339. /**
  340. * method onDelete()
  341. * executed whenever the user clicks at the delete button
  342. * Ask if the user really wants to delete the record
  343. */
  344. function onDelete()
  345. {
  346. // get the parameter $key
  347. $object = $this->form->getData('Product');
  348. $key=$object->id;
  349. // define two actions
  350. $action = new TAction(array($this, 'Delete'));
  351. // define the action parameters
  352. $action->setParameter('key', $key);
  353. // shows a dialog to the user
  354. new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
  355. }
  356. /**
  357. * method Delete()
  358. * Delete a record
  359. */
  360. function Delete($param)
  361. {
  362. try
  363. {
  364. // get the parameter $key
  365. $key=$param['key'];
  366. // open a transaction with database 'changeman'
  367. TTransaction::open('fotos');
  368. // instantiates object Document
  369. $object = new Product($key);
  370. $objectproximo = new Product($object->proximo);
  371. //exclui a imagem
  372. unlink("app/images/".$object->corrida."/".$object->name.".jpg");
  373. // deletes the object from the database
  374. $object->delete();
  375. // reload the listing
  376. //$this->onReload();
  377. // shows the success message
  378. new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted'));
  379. $this->form->setData($objectproximo);
  380. $this->imgFoto->src = "app/images/".$objectproximo->corrida."/".$objectproximo->name.".jpg";
  381. $this->linka->href = $this->imgFoto->src; //avança para proximo
  382. // close the transaction
  383. TTransaction::close();
  384. }
  385. catch (Exception $e) // in case of exception
  386. {
  387. // shows the exception error message
  388. new TMessage('error', '<b>Error</b> ' . $e->getMessage());
  389. // undo all pending operations
  390. TTransaction::rollback();
  391. }
  392. }
  393. }
  394. ?>
FS

Show, deu certo aqui brother!