Problemas com setChangeAction Estou com um problema na execução do setChangeAction no TCombo. Quanto executo esta me retornando Fatal error: Using $this when not in object context in C:xampphtdocscompraslibadiantiwidgetbaseTElement.php on line 180. Estou tentando utilizar uma rotina que foi apresentada no próprio forum para esses casos. ...
CA
Problemas com setChangeAction  
Estou com um problema na execução do setChangeAction no TCombo. Quanto executo esta me retornando Fatal error: Using $this when not in object context in C:xampphtdocscompraslibadiantiwidgetbaseTElement.php on line 180. Estou tentando utilizar uma rotina que foi apresentada no próprio forum para esses casos.

 
  1. <?php
  2. class CadastrarFornecedor extends TPage
  3. {
  4. protected $form; // form
  5. function __construct($param)
  6. {
  7. parent::__construct();
  8. // creates the form
  9. $this->form = new TForm('form_cadastrar_fornecedor');
  10. $this->form->class = 'tform';
  11. $this->form->style = 'max-width: 700px; margin:auto; margin-top:40px;';
  12. // add the notebook inside the form
  13. $table = new TTable;
  14. $table->width = '100%';
  15. $this->form->add($table);
  16. // create the form fields
  17. $tipo = new TCombo('tipo');
  18. $nome = new TEntry('nome');
  19. $fantasia = new TEntry('fantasia');
  20. $cpf = new TEntry('cpf_cnpj');
  21. $tipo->setChangeAction(new TAction(array($this, 'onAtualizarCampos')));
  22. $nome->setMaxLength('120');
  23. $fantasia->setMaxLength('100');
  24. $cpf->setMaxLength('14');
  25. // add the combo tipo
  26. $itemstipo = array();
  27. $itemstipo['1'] = 'PESSOA FÍSICA';
  28. $itemstipo['2'] = 'PESSOA JURÍDICA';
  29. $tipo->addItems($itemstipo);
  30. $row=$table->addRow();
  31. $row->addCell( new TLabel('Cadastro de Fornecedores') )->colspan = 2;
  32. $row->class='tformtitle';
  33. $cpf->placeholder = 'Apenas números';
  34. // create the field labels
  35. $lab_tipo = new TLabel('Tipo:');
  36. $lab_nome = new TLabel('Nome:');
  37. $lab_fantasia = new TLabel('Fantasia:');
  38. $lab_cpf = new TLabel('CPF:');
  39. $row=$table->addRow();
  40. $row->addCell( $lab_tipo )->style = 'width:20%';
  41. $row->addCell( $tipo );
  42. $row=$table->addRow();
  43. $row->addCell( $lab_nome );
  44. $row->addCell( $nome );
  45. $row=$table->addRow();
  46. $row->addCell( $lab_fantasia );
  47. $row->addCell( $fantasia );
  48. $row=$table->addRow();
  49. $row->addCell( $lab_cpf );
  50. $row->addCell( $cpf );
  51. // create an action button (save)
  52. $save_button=new TButton('save');
  53. // define the button action
  54. $save_button->setAction(new TAction(array($this, 'onSave')), 'Gravar Fornecedor');
  55. $save_button->class = 'btn btn-success';
  56. $save_button->style = 'font-size:18px;width:90%;padding:10px';
  57. $row=$table->addRow();
  58. $row->class = 'tformaction';
  59. $cell = $row->addCell( $save_button );
  60. $cell->colspan = 2;
  61. $cell->style = 'text-align:center';
  62. // create an action button (new)
  63. $new_button=new TButton('new');
  64. // define the button action
  65. $new_button->setAction(new TAction(array('LoginForm', 'onLogout')), 'Retornar');
  66. $new_button->class = 'btn btn-primary';
  67. $new_button->style = 'font-size:18px;width:90%;padding:10px';
  68. $row=$table->addRow();
  69. $row->class = 'tformaction';
  70. $cell = $row->addCell( $new_button );
  71. $cell->colspan = 2;
  72. $cell->style = 'text-align:center';
  73. $this->form->setFields(array($tipo, $nome, $fantasia, $cpf, $save_button, $new_button));
  74. // add the form to the page
  75. parent::add($this->form);
  76. }
  77. /**
  78. * Authenticate the User
  79. */
  80. public function onSave()
  81. {
  82. try
  83. {
  84. }
  85. catch (Exception $e)
  86. {
  87. }
  88. }
  89. public static function onAtualizarCampos()
  90. {
  91. $script = new TElement('script');
  92. $script->type = 'text/javascript';
  93. $javascript = " // personaliza os campos de acordo com o tipo de pessoa
  94. $('select[name=\"tipo\"]').change(function(event)
  95. {
  96. var tipoPessoa
  97. $('select[name=\"tipo\"] > option:selected').each(function(){tipoPessoa = $(this).text();});
  98. if(tipoPessoa.toLowerCase() == 'pessoa física')
  99. {
  100. $('label:contains(CNPJ:)').parent().css('left',82);
  101. $('label:contains(CNPJ:)').text('CPF:');
  102. $('input[name=\"cpf_cnpj\"]').val('');
  103. $('input[name=\"cpf_cnpj\"]').attr({onkeypress:'return entryMask(this,event,\"999.999.999-99\")'});
  104. $('input[name=\"fantasia\"]').attr({class:'tfield',readonly:false,value:''});
  105. }
  106. if(tipoPessoa.toLowerCase() == 'pessoa jurídica')
  107. {
  108. $('label:contains(CPF:)').parent().css('left',75);
  109. $('label:contains(CPF:)').text('CNPJ:');
  110. $('input[name=\"cpf_cnpj\"]').val('');
  111. $('input[name=\"cpf_cnpj\"]').attr({onkeypress:'return entryMask(this,event,\"99.999.999/9999-99\")'});
  112. $('input[name=\"fantasia\"]').attr({class:'tfield_disabled',readonly:'1',value:''});
  113. }
  114. }); ";
  115. $script->add($javascript);
  116. parent::add($script);
  117. }
  118. }</code>

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)


IF

O erro está ocorrendo em parent::add numa function static.

A saída seria vc usar o TScript::create()
MG

Você pode usar a dica do Ivan ou adicionar o elemento diretamente no construtor usando 'on' sem necessidade de utilizar um "TAction":
;;;;; $(document).on('change','select[name=\"tipo\"]' , function(event){ .....>/code>