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.
- <?php
- class CadastrarFornecedor extends TPage
- {
- protected $form; // form
- function __construct($param)
- {
- parent::__construct();
- // creates the form
- $this->form = new TForm('form_cadastrar_fornecedor');
- $this->form->class = 'tform';
- $this->form->style = 'max-width: 700px; margin:auto; margin-top:40px;';
- // add the notebook inside the form
- $table = new TTable;
- $table->width = '100%';
- $this->form->add($table);
- // create the form fields
- $tipo = new TCombo('tipo');
- $nome = new TEntry('nome');
- $fantasia = new TEntry('fantasia');
- $cpf = new TEntry('cpf_cnpj');
- $tipo->setChangeAction(new TAction(array($this, 'onAtualizarCampos')));
- $nome->setMaxLength('120');
- $fantasia->setMaxLength('100');
- $cpf->setMaxLength('14');
- // add the combo tipo
- $itemstipo = array();
- $itemstipo['1'] = 'PESSOA FÍSICA';
- $itemstipo['2'] = 'PESSOA JURÍDICA';
- $tipo->addItems($itemstipo);
- $row=$table->addRow();
- $row->addCell( new TLabel('Cadastro de Fornecedores') )->colspan = 2;
- $row->class='tformtitle';
- $cpf->placeholder = 'Apenas números';
- // create the field labels
- $lab_tipo = new TLabel('Tipo:');
- $lab_nome = new TLabel('Nome:');
- $lab_fantasia = new TLabel('Fantasia:');
- $lab_cpf = new TLabel('CPF:');
- $row=$table->addRow();
- $row->addCell( $lab_tipo )->style = 'width:20%';
- $row->addCell( $tipo );
- $row=$table->addRow();
- $row->addCell( $lab_nome );
- $row->addCell( $nome );
- $row=$table->addRow();
- $row->addCell( $lab_fantasia );
- $row->addCell( $fantasia );
- $row=$table->addRow();
- $row->addCell( $lab_cpf );
- $row->addCell( $cpf );
- // create an action button (save)
- $save_button=new TButton('save');
- // define the button action
- $save_button->setAction(new TAction(array($this, 'onSave')), 'Gravar Fornecedor');
- $save_button->class = 'btn btn-success';
- $save_button->style = 'font-size:18px;width:90%;padding:10px';
- $row=$table->addRow();
- $row->class = 'tformaction';
- $cell = $row->addCell( $save_button );
- $cell->colspan = 2;
- $cell->style = 'text-align:center';
- // create an action button (new)
- $new_button=new TButton('new');
- // define the button action
- $new_button->setAction(new TAction(array('LoginForm', 'onLogout')), 'Retornar');
- $new_button->class = 'btn btn-primary';
- $new_button->style = 'font-size:18px;width:90%;padding:10px';
- $row=$table->addRow();
- $row->class = 'tformaction';
- $cell = $row->addCell( $new_button );
- $cell->colspan = 2;
- $cell->style = 'text-align:center';
- $this->form->setFields(array($tipo, $nome, $fantasia, $cpf, $save_button, $new_button));
- // add the form to the page
- parent::add($this->form);
- }
- /**
- * Authenticate the User
- */
- public function onSave()
- {
- try
- {
- }
- catch (Exception $e)
- {
- }
- }
- public static function onAtualizarCampos()
- {
- $script = new TElement('script');
- $script->type = 'text/javascript';
- $javascript = " // personaliza os campos de acordo com o tipo de pessoa
- $('select[name=\"tipo\"]').change(function(event)
- {
- var tipoPessoa
- $('select[name=\"tipo\"] > option:selected').each(function(){tipoPessoa = $(this).text();});
- if(tipoPessoa.toLowerCase() == 'pessoa física')
- {
- $('label:contains(CNPJ:)').parent().css('left',82);
- $('label:contains(CNPJ:)').text('CPF:');
- $('input[name=\"cpf_cnpj\"]').val('');
- $('input[name=\"cpf_cnpj\"]').attr({onkeypress:'return entryMask(this,event,\"999.999.999-99\")'});
- $('input[name=\"fantasia\"]').attr({class:'tfield',readonly:false,value:''});
- }
- if(tipoPessoa.toLowerCase() == 'pessoa jurídica')
- {
- $('label:contains(CPF:)').parent().css('left',75);
- $('label:contains(CPF:)').text('CNPJ:');
- $('input[name=\"cpf_cnpj\"]').val('');
- $('input[name=\"cpf_cnpj\"]').attr({onkeypress:'return entryMask(this,event,\"99.999.999/9999-99\")'});
- $('input[name=\"fantasia\"]').attr({class:'tfield_disabled',readonly:'1',value:''});
- }
- }); ";
- $script->add($javascript);
- parent::add($script);
- }
- }</code>
Pacotão Dominando o Adianti Framework 7
O material mais completo de treinamento do Framework.
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado! Versão 7.4
Curso em vídeo aulas + Livro completo + Códigos fontes do projeto ERPHouse.
Conteúdo Atualizado! Versão 7.4
Quero me inscrever agora!
O erro está ocorrendo em parent::add numa function static.
A saída seria vc usar o TScript::create()
Você pode usar a dica do Ivan ou adicionar o elemento diretamente no construtor usando 'on' sem necessidade de utilizar um "TAction":