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. if($object->cpf_cnpj != '000.000.000-00')
  8. {
  9. $validator = new TCPFValidator;
  10. $validator->validate('CPF',$object->cpf_cnpj);
  11. $id = $this->form->getField('id');
  12. $validator = new TUniqueValidator;
  13. $validator->validate('CPF',$object->cpf_cnpj,array('class'=>'Cliente','field'=>'cpf_cnpj','pk'=>$id));
  14. }
  15. }
  16. // Valida o CNPJ para pessoas jurídicas
  17. if($object->tipopessoa_id == 2)
  18. {
  19. $validator = new TRequiredValidator;
  20. $validator->validate('CNPJ',$object->cpf_cnpj);
  21. if($object->cpf_cnpj != '00.000.000/0000-00')
  22. {
  23. $validator = new TCNPJValidator;
  24. $validator->validate('CNPJ',$object->cpf_cnpj);
  25. $id = $this->form->getField('id');
  26. $validator = new TUniqueValidator;
  27. $validator->validate('CNPJ',$object->cpf_cnpj,array('class'=>'Cliente','field'=>'cpf_cnpj','pk'=>$id));
  28. }
  29. }
  30. ?>


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

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.