Lançado Adianti Framework 8.1!
Clique aqui para saber mais
Erro de Exceção: TCombo could not be converted to string Olá pessoal. Estou tentando implementar o código abaixo que verifiquei em um post (https://www.adianti.com.br/forum/pt/view_661?adicionando-validacoes-em-formularios-dinamicos), porém, ao tentar executar a minha classe de cadastro de cliente dá esse erro: Object of class AdiantiWidgetFormTCombo could not be converted to string O meu campo é esse: $CLI01Tipo = new TComb...
HB
Erro de Exceção: TCombo could not be converted to string  
Olá pessoal.
Estou tentando implementar o código abaixo que verifiquei em um post (https://www.adianti.com.br/forum/pt/view_661?adicionando-validacoes-em-formulari), porém, ao tentar executar a minha classe de cadastro de cliente dá esse erro:

Object of class AdiantiWidgetFormTCombo could not be converted to string

O meu campo é esse: $CLI01Tipo = new TCombo('CLI01Tipo');
e eu preciso que ele seja um combo e na sequência do código fiz assim:

$CLI01Tipo->addItems( [ '1' => 'Física', '2' => 'Jurídica'] );
$CLI01Tipo->setValue('1');

O tipo de dado da minha tabela no banco é varchar e mesmo assim está dando esse erro.

Vejam abaixo o trecho do código que está no fórum que estou tentando implementar (no meu código original troco as minhas variáveis).
Importante:
Nessa parte if($object->tipopessoa_id == 1) eu já testei assim if($object->tipopessoa_id == 1) e também assim if($object->tipopessoa_id == '1') com aspas simples, mais não funciona.

  1. <?php
  2.                 // Valida o CPF para pessoas físicas
  3.                 if($object->tipopessoa_id == 1)
  4.                 {                
  5.                     $validator = new TRequiredValidator;
  6.                     $validator->validate('CPF',$object->cpf_cnpj);  
  7.                     
  8.                     if($object->cpf_cnpj != '000.000.000-00')
  9.                     {
  10.                         $validator = new TCPFValidator;
  11.                         $validator->validate('CPF',$object->cpf_cnpj);
  12.                         
  13.                         $id $this->form->getField('id');
  14.                     
  15.                         $validator = new TUniqueValidator;
  16.                         $validator->validate('CPF',$object->cpf_cnpj,array('class'=>'Cliente','field'=>'cpf_cnpj','pk'=>$id)); 
  17.                     }  
  18.                 }
  19.                 
  20.                 // Valida o CNPJ para pessoas jurídicas
  21.                 if($object->tipopessoa_id == 2)
  22.                 {
  23.                     $validator = new TRequiredValidator;
  24.                     $validator->validate('CNPJ',$object->cpf_cnpj); 
  25.                     
  26.                     if($object->cpf_cnpj != '00.000.000/0000-00')
  27.                     {
  28.                     
  29.                         $validator = new TCNPJValidator;
  30.                         $validator->validate('CNPJ',$object->cpf_cnpj);
  31.                         
  32.                         $id $this->form->getField('id');
  33.                         
  34.                         $validator = new TUniqueValidator;
  35.                         $validator->validate('CNPJ',$object->cpf_cnpj,array('class'=>'Cliente','field'=>'cpf_cnpj','pk'=>$id));     
  36.                     }
  37.                 } 
  38.     ?>


Alguém pode me ajudar a entender e a resolver essa situação de erro envolvendo um combo?

Obrigado,
José Humberto Júnior

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


NR

Na mensagem de erro deve estar aparecendo em qual linha é o problema. Poste o conteúdo dessa linha.
HB

Boa tarde.
essa é a linha do erro: File: app control Sistema clienteForm.class.php : 112

abaixo o código completo da classe:
  1. <?php
  2. /**
  3.  * clienteForm Form
  4.  * @author  <your name here>
  5.  */
  6. class clienteForm extends TPage
  7. {
  8.     protected $form// form
  9.     
  10.     /**
  11.      * Form constructor
  12.      * @param $param Request
  13.      */
  14.     public function __construct$param )
  15.     {
  16.         parent::__construct();
  17.         
  18.         // creates the form
  19.         $this->form = new TQuickForm('form_cliente');
  20.         $this->form->class 'tform'// change CSS class
  21.         
  22.         $this->form->style 'display: table;width: 70%'// change style
  23.         
  24.         // define the form title
  25.         $this->form->setFormTitle('Cadastro de Clientes');
  26.         
  27.         // create the form fields
  28.         $id            = new TEntry('id');
  29.         $CLI01Tipo     = new TCombo('CLI01Tipo');
  30.         $CLI01Nome     = new TEntry('CLI01Nome');
  31.         $CLI01CPF      = new TEntry('CLI01CPF');
  32.         $CLI01CNPJ     = new TEntry('CLI01CNPJ');
  33.         // $CLI01RG       = new TEntry('CLI01RG');
  34.         // $CLI01InsEstad = new TEntry('CLI01InsEstad');
  35.         $CLI01CEP      = new TEntry('CLI01CEP');
  36.         $CLI01Endereco = new TEntry('CLI01Endereco');
  37.         $CLI01Bairro   = new TEntry('CLI01Bairro');
  38.         $CLI01Cidade   = new TEntry('CLI01Cidade');
  39.         $estado_id     = new  ">TDBSeekButton('estado_id''sistema''form_cliente''estado''EST01Nome''estado_id''estado_nome');
  40.         $estado_nome   = new TEntry('estado_nome');
  41.         // $CLI01Fixo     = new TEntry('CLI01Fixo');
  42.         $CLI01Celular  = new TEntry('CLI01Celular');
  43.         $CLI01DtCadas  = new TDate('CLI01DtCadas');
  44.         $CLI01Ativo    = new TCombo('CLI01Ativo');
  45.         // add the fields
  46.         $this->form->addQuickField('Código'$id,  80 );
  47.         $this->form->addQuickField('Tipo'$CLI01Tipo,  80 ,  new TRequiredValidator);
  48.         $this->form->addQuickField('Nome'$CLI01Nome,  300 , new TRequiredValidator);
  49.         $this->form->addQuickField('CPF'$CLI01CPF,  135 );
  50.         $this->form->addQuickField('CNPJ'$CLI01CNPJ,  135 );
  51.         // $this->form->addQuickField('RG', $CLI01RG,  100 );
  52.         // $this->form->addQuickField('Insc. Estadual', $CLI01InsEstad,  100 );
  53.         $this->form->addQuickField('CEP'$CLI01CEP,  135 );
  54.         $this->form->addQuickField('Endereço'$CLI01Endereco,  300 , new TRequiredValidator);
  55.         $this->form->addQuickField('Bairro'$CLI01Bairro,  300 );
  56.         $this->form->addQuickField('Cidade'$CLI01Cidade,  300 , new TRequiredValidator);
  57.         $this->form->addQuickField('UF'$estado_id,  50 , new TRequiredValidator);
  58.         $this->form->addQuickField(''$estado_nome,  200 );
  59.         // $this->form->addQuickField('Fone Fixo', $CLI01Fixo,  50 );
  60.         $this->form->addQuickField('Fone Celular'$CLI01Celular,  125 );
  61.         $this->form->addQuickField('Cadastro'$CLI01DtCadas,  100 , new TRequiredValidator);
  62.         $this->form->addQuickField('Ativo'$CLI01Ativo,  80 , new TRequiredValidator);
  63.         // minhas modificações
  64.         $estado_nome->setEditable(FALSE);
  65.         //$CLI01CNPJ->addValidation('CNPJ', new TCNPJValidator);
  66.         //$CLI01CPF->addValidation('CPF',   new TCPFValidator);  
  67.         $CLI01Nome->forceUpperCase();
  68.         $CLI01Endereco->forceUpperCase();
  69.         $CLI01Bairro->forceUpperCase();
  70.         $CLI01Cidade->forceUpperCase();
  71.         $CLI01DtCadas->setMask('dd/mm/yyyy');
  72.         $CLI01DtCadas->setDatabaseMask('yyyy-mm-dd');
  73.         $CLI01Celular->setMask('(99)9.9999-9999');
  74.         $CLI01CEP->setMask('99.999-999');
  75.         $CLI01CNPJ->setMask('99.999.999/9999-99');
  76.         $CLI01CPF->setMask('999.999.999-99');
  77.         $CLI01Tipo->addItems( [ '1' => 'Física''2' => 'Jurídica'] );
  78.         $CLI01Tipo->setValue('1');
  79.         $CLI01Ativo->addItems( [ '1' => 'Sim''2' => 'Não'] );
  80.         $CLI01Ativo->setValue('1');
  81.         if (!empty($id))
  82.         {
  83.             $id->setEditable(FALSE);
  84.         }
  85.         
  86.         /** samples
  87.          $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
  88.          $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
  89.          $fieldX->setSize( 100, 40 ); // set size
  90.          **/
  91.          
  92.         // create the form actions
  93.         $this->form->addQuickAction(_t('Save'), new TAction(array($this'onSave')), 'fa:floppy-o');
  94.         $this->form->addQuickAction(_t('New'),  new TAction(array($this'onClear')), 'bs:plus-sign green');
  95.         
  96.         // minhas modificações
  97.         $this->form->addQuickAction('Retorna', new TAction(array('clienteCadastroGrid''onReload')), 'fa:table blue' );
  98.         
  99.         // vertical box container
  100.         $container = new TVBox;
  101.         $container->style 'width: 90%';
  102.         //$container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
  103.         $container->add($this->form);
  104.         
  105.         // Valida o CPF para pessoas físicas
  106.         if($this->form->$CLI01Tipo == '1' )
  107.         {                
  108.             $validator = new TRequiredValidator;
  109.             $validator->validate('CPF'$this->form->$CLI01CPF);  
  110.             
  111.             if($this->form->$CLI01CPF != '000.000.000-00')
  112.             {
  113.                 $validator = new TCPFValidator;
  114.                 $validator->validate('CPF'$this->form->$CLI01CPF);
  115.             }  
  116.         }
  117.                 
  118.         // Valida o CNPJ para pessoas jurídicas
  119.         if($this->form->$CLI01Tipo == '2' )
  120.         {
  121.             $validator = new TRequiredValidator;
  122.             $validator->validate('CNPJ'$this->form->$CLI01CNPJ);
  123.             
  124.             if($this->form->$CLI01CNPJ != '00.000.000/0000-00')
  125.             {
  126.             
  127.                 $validator = new TRequiredValidator;
  128.                 $validator->validate('CNPJ'$this->form->$CLI01CNPJ);  
  129.             }
  130.         }
  131.         
  132.         
  133.         parent::add($container);
  134.     }
  135.     /**
  136.      * Save form data
  137.      * @param $param Request
  138.      */
  139.     public function onSave$param )
  140.     {
  141.         try
  142.         {
  143.             TTransaction::open('sistema'); // open a transaction
  144.             
  145.             /**
  146.             // Enable Debug logger for SQL operations inside the transaction
  147.             TTransaction::setLogger(new TLoggerSTD); // standard output
  148.             TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  149.             **/
  150.             
  151.             $this->form->validate(); // validate form data
  152.             
  153.             $object = new cliente;  // create an empty object
  154.             $data $this->form->getData(); // get form data as array
  155.             $object->fromArray( (array) $data); // load the object with data
  156.             $object->store(); // save the object
  157.             
  158.             // get the generated id
  159.             $data->id $object->id;
  160.             
  161.             $this->form->setData($data); // fill form data
  162.             TTransaction::close(); // close the transaction
  163.             
  164.             new TMessage('info'TAdiantiCoreTranslator::translate('Record saved'));
  165.         }
  166.         catch (Exception $e// in case of exception
  167.         {
  168.             new TMessage('error'$e->getMessage()); // shows the exception error message
  169.             $this->form->setData$this->form->getData() ); // keep form data
  170.             TTransaction::rollback(); // undo all pending operations
  171.         }
  172.     }
  173.     
  174.     /**
  175.      * Clear form data
  176.      * @param $param Request
  177.      */
  178.     public function onClear$param )
  179.     {
  180.         $this->form->clear(TRUE);
  181.     }
  182.     
  183.     /**
  184.      * Load object to form data
  185.      * @param $param Request
  186.      */
  187.     public function onEdit$param )
  188.     {
  189.         try
  190.         {
  191.             if (isset($param['key']))
  192.             {
  193.                 $key $param['key'];  // get the parameter $key
  194.                 TTransaction::open('sistema'); // open a transaction
  195.                 $object = new cliente($key); // instantiates the Active Record
  196.                 $this->form->setData($object); // fill the form
  197.                 TTransaction::close(); // close the transaction
  198.             }
  199.             else
  200.             {
  201.                 // $this->form->clear(TRUE);
  202.             }
  203.         }
  204.         catch (Exception $e// in case of exception
  205.         {
  206.             new TMessage('error'$e->getMessage()); // shows the exception error message
  207.             TTransaction::rollback(); // undo all pending operations
  208.         }
  209.     }
  210. }
  211. ?>
</your>
WP

acho que o correto seria

  1. <?php
  2. //if($this->form->$CLI01Tipo == '1' )
  3. if($this->form->CLI01Tipo == '1' )
  4. ?>
HB

Olá Willian,
Fazendo desta forma que você me indicou, da um erro de propriedade indefinida justamente na linha 112 e 125, respectivamente

Notice: Undefined property: AdiantiWidgetWrapperTQuickForm::$CLI01Tipo in C:xampphtdocsSistemaappcontrolSistemaclienteForm.class.php on line 112
Notice: Undefined property: AdiantiWidgetWrapperTQuickForm::$CLI01Tipo in C:xampphtdocsSistemaappcontrolSistemaclienteForm.class.php on line 125

O trecho do código modificado ficou assim:

  1. <?php
  2.         // Valida o CPF para pessoas físicas
  3.         if($this->form->CLI01Tipo == '1' )
  4.         {                
  5.             $validator = new TRequiredValidator;
  6.             $validator->validate('CPF'$this->form->$CLI01CPF);  
  7.             
  8.             if($CLI01CPF != '000.000.000-00')
  9.             {
  10.                 $validator = new TCPFValidator;
  11.                 $validator->validate('CPF'$this->form->$CLI01CPF);
  12.             }  
  13.         }
  14.                 
  15.         // Valida o CNPJ para pessoas jurídicas
  16.         if($this->form->CLI01Tipo == '2' 
  17.         {
  18.             $validator = new TRequiredValidator;
  19.             $validator->validate('CNPJ'$this->form->$CLI01CNPJ);
  20.             
  21.             if($this->form->$CLI01CNPJ != '00.000.000/0000-00')
  22.             {
  23.             
  24.                 $validator = new TRequiredValidator;
  25.                 $validator->validate('CNPJ'$this->form->$CLI01CNPJ);  
  26.             }
  27.         }
  28. ?>
WP

vamos por parte (eu não tinha lido todo seu codigo)

1- vejamos esta parte primeiro
  1. <?php  
  2.    $CLI01Tipo->addItems( [ '1' => 'Física''2' => 'Jurídica'] );
  3.    $CLI01Tipo->setValue('1'); //você esta indicando o Valor 1 para iniciar, sendo assim não tem sentido algum fazer um IF  >>>> if($this->form->CLI01Tipo == '1' ) sendo que o valor é sempre 1 ?!?!?!?!
  4. ?>


2- lembre que você esta fazendo uso do __construct (aqui ainda não tem acesso ao valores imputados no formulario) creio que esta na área errado seu código, deveria estar no onSave

se colocar no onsave deve ficar algo como


  1. <?php
  2.     public function onSave$param )
  3.     {
  4.         try
  5.         {
  6.             TTransaction::open('sistema'); // open a transaction
  7.             
  8.             /**
  9.             // Enable Debug logger for SQL operations inside the transaction
  10.             TTransaction::setLogger(new TLoggerSTD); // standard output
  11.             TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
  12.             **/
  13.             $data $this->form->getData(); // get form data as array
  14.             // Valida o CPF para pessoas físicas
  15.             if($data->CLI01Tipo == '1' )
  16.             {                
  17.                 $validator = new TRequiredValidator;
  18.                 $validator->validate('CPF'$data->CLI01CPF);  
  19.                 
  20.                 if($CLI01CPF != '000.000.000-00')
  21.                 {
  22.                     $validator = new TCPFValidator;
  23.                     $validator->validate('CPF',$data->CLI01CPF);
  24.                 }  
  25.             }
  26.                     
  27.             // Valida o CNPJ para pessoas jurídicas
  28.             if($data->CLI01Tipo == '2' 
  29.             {
  30.                 $validator = new TRequiredValidator;
  31.                 $validator->validate('CNPJ'$data->CLI01CNPJ);
  32.                 
  33.                 if($data->CLI01CNPJ != '00.000.000/0000-00')
  34.                 {
  35.                 
  36.                     $validator = new TRequiredValidator;
  37.                     $validator->validate('CNPJ'$data->CLI01CNPJ);  
  38.                 }
  39.             }
  40.             
  41.             $this->form->validate(); // validate form data
  42.             
  43.             $object = new cliente;  // create an empty object
  44.            
  45.             $object->fromArray( (array) $data); // load the object with data
  46.             $object->store(); // save the object
  47.             
  48.             // get the generated id
  49.             $data->id $object->id;
  50.             
  51.             $this->form->setData($data); // fill form data
  52.             TTransaction::close(); // close the transaction
  53.             
  54.             new TMessage('info'TAdiantiCoreTranslator::translate('Record saved'));
  55.         }
  56.         catch (Exception $e// in case of exception
  57.         {
  58.             new TMessage('error'$e->getMessage()); // shows the exception error message
  59.             $this->form->setData$this->form->getData() ); // keep form data
  60.             TTransaction::rollback(); // undo all pending operations
  61.         }
  62.     }
  63. ?>

WP

logo faça as correçoes pertinentes, não fiz todas as alterações
HB

Willian, incluí a validação no onSave e deu certinho.
Mais uma vez muito obrigado.