Preenchimento automático de campos da mesma tabela Bom dia, estou tentando puxar informações da mesma tabela como preenchimento automático. Faz 3 dias que estou nesse problema e tem algo que esta faltando. Preciso que, ao digitar o CPF do usuário, o campo Cliente seja preenchido automático. Tabela: ativos (system_user_name = name cliente) (system_user_id = cpf) ...
LH
Preenchimento automático de campos da mesma tabela  
Bom dia, estou tentando puxar informações da mesma tabela como preenchimento automático. Faz 3 dias que estou nesse problema e tem algo que esta faltando.
Preciso que, ao digitar o CPF do usuário, o campo Cliente seja preenchido automático.

Tabela: ativos (system_user_name = name cliente) (system_user_id = cpf)

 
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_erros', 1);
  4. error_reporting(E_ALL);
  5. class AtivaPromocao extends TPage
  6. {
  7. private $form;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. //parent::setFormTitle( "Cadastro de Profissionais" );
  12. //parent::setSize( 0.600, 0.800 );
  13. $redstar = '<font color="red"><b>*</b></font>';
  14. $this->form = new BootstrapFormBuilder( "form_ativa" );
  15. $this->form->setFormTitle( "Ativar Voucher" );
  16. $this->form->class = "tform";
  17. $id = new THidden( "id" );
  18. $empresa_id = new TDBUniqueSearch( 'empresa_id', 'database', 'EmpresaRecord', 'id', 'nomefantasia', 'nomefantasia' );
  19. $voucher_id = new TDBCombo( 'voucher_id', 'database', 'VoucherRecord', 'id', 'nomeproduto', 'nomeproduto');
  20. $state_id = new TDBCombo('system_user_id', 'database', 'SystemUser', 'id', 'cpf', 'cpf');
  21. $state_id ->enableSearch();
  22. $filter = new TCriteria();
  23. $filter ->add(new TFilter('id', '<', '0'));
  24. $city_id = new TDBCombo('system_user_name', 'database', 'SystemUser', 'id', 'name', 'name', $filter);
  25. $city_id ->enableSearch();
  26. //$change_action = new TAction(array($this, 'onChange'));
  27. //$system_user_id->setChangeAction($change_action);
  28. //$system_user_name->setEditable(FALSE);
  29. $state_id->setSize('50%');
  30. $this->form->addFields( [ $id ] );
  31. $this->form->addFields( [new TLabel("CPF: {$redstar}")], [$state_id] );
  32. $this->form->addFields( [new TLabel("Cliente: {$redstar}")], [$city_id] );
  33. $this->form->addFields([]);
  34. $this->form->addFields( [new TLabel("Empresa: {$redstar}")], [$empresa_id] );
  35. $this->form->addFields( [new TLabel("Produto: {$redstar}")], [$voucher_id] );
  36. $state_id->setChangeAction( new TAction( array($this, 'onStateChange' )) );
  37. $this->form->addAction( 'Salvar', new TAction( [ $this, "onSave" ] ), 'fa:floppy-o' )->class = 'btn btn-sm btn-success';
  38. $this->form->addAction('Voltar', new TAction(array('VoucherList', 'onReload')), 'fa:arrow-left')->class = 'btn btn-sm btn-primary';
  39. $container = new TVBox();
  40. $container->style = "width: 100%";
  41. $container->add( $this->form );
  42. parent::add( $container );
  43. }
  44. public function onSave($param)
  45. {
  46. try
  47. {
  48. TTransaction::open('database');
  49. $this->form->validate();
  50. $object = new Test;
  51. $data = $this->form->getData();
  52. $object->fromArray((array) $data);
  53. $object->store();
  54. $data->id = $object->id;
  55. $this->form->setData($data);
  56. $this->fireEvents($object);
  57. TTransaction::close();
  58. new TMessage('info', TAdiantiCoreTranslator::translate('AtivaRecord'));
  59. } catch (Exception $e)
  60. {
  61. new TMessage('error', $e->getMessage());
  62. $this->form->setData($this->form->getData());
  63. TTransaction::rolback();
  64. }
  65. }
  66. public function fireEvents($param)
  67. {
  68. $obj = new StdClass;
  69. $obj->state_id = $object->state_id;
  70. $obj->city_id = $object->city_id;
  71. TForm::sendData('form_ativa', $obj);
  72. }
  73. public static function onStateChange($param)
  74. {
  75. try
  76. {
  77. TTransaction::open('database');
  78. if (!empty($param['system_user_id']))
  79. {
  80. $criteria = TCriteria::create( ['name' => $param['system_user_id'] ] );
  81. TDBCombo::reloadFromModel('form_ativa', 'system_user_name', 'database', 'SystemUSer', 'id', 'name', 'name', $criteria, TRUE);
  82. }
  83. else
  84. {
  85. TCombo::clearField('form_ativa', 'system_user_name');
  86. }
  87. TTransaction::close();
  88. }
  89. catch (Exception $e)
  90. {
  91. new TMessage('error', $e->getMessage());
  92. }
  93. //var_dump($param);
  94. }
  95. /*public static function onChange($param)
  96. {
  97. TTransaction::open("database");
  98. $criteria = new TCriteria();
  99. $criteria->add(new TFilter('cpf', '=', $param['system_user_id']));
  100. $repository = new TRepository("SystemUser");
  101. $users = $repository->load($criteria);
  102. $options = array();
  103. foreach($users as $user){
  104. $options[$user->id] = $user->name;
  105. }
  106. //var_dump($param);
  107. TCombo::reload('form_ativa', 'system_user_name', $options);
  108. }
  109. public function onSave()
  110. {
  111. try {
  112. $this->form->validate();
  113. TTransaction::open( "database" );
  114. $object = $this->form->getData("AtivaRecord");
  115. $object->store();
  116. TTransaction::close();
  117. $action = new TAction( [ "VoucherList", "onReload" ] );
  118. new TMessage( "info", "Registro salvo com sucesso!", $action );
  119. } catch ( Exception $ex ) {
  120. TTransaction::rollback();
  121. new TMessage( "error", "Ocorreu um erro ao tentar salvar o registro!<br><br><br><br>" . $ex->getMessage() );
  122. }
  123. }*/
  124. public function onEdit( $param )
  125. {
  126. try {
  127. if( isset( $param[ "key" ] ) ) {
  128. TTransaction::open( "database" );
  129. $object = new AtivaRecord($param["key"]);
  130. $this->form->setData($object);
  131. TTransaction::close();
  132. }
  133. } catch ( Exception $ex ) {
  134. TTransaction::rollback();
  135. new TMessage( "error", "Ocorreu um erro ao tentar carregar o registro para edição!<br><br>" . $ex->getMessage() );
  136. }
  137. }
  138. }

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)


IM

Luis,

$state_id->setExitAction(new TAction([$this,'aoSair']));

Crei essa action
e depois

public static function aoSair($param = null) { try { if (!empty($param['state_id'])) { $object = new stdClass(); $object->Nome= "nome que vc quer"; // faça seu critério de busca TForm::sendData(self::$formName, $object); } } catch (Exception $e) { new TMessage('error', $e->getMessage()); } }
IM

Luis,
$state_id->setExitAction(new TAction([$this,'aoSair']));

Crei essa action e depois
 
  1. <?php>
  2. public static function aoSair($param = null)
  3. { try
  4. { if (!empty($param['state_id']))
  5. { $object = new stdClass(); $object->Nome= "nome que vc quer"; // faça seu critério de busca
  6. TForm::sendData(self::$formName, $object);
  7. }
  8. } catch (Exception $e)
  9. { new TMessage('error', $e->getMessage()); }
  10. }
  11. ?>
LH

Ele da esse erro!


Exceção
Método setExitAction() não encontrado

File: appcontrolcadastrosAtivaPromocao.class.php : 23
AdiantiWidgetFormTField->__call(setExitAction,AdiantiControlTAction)
File: libadianticoreAdiantiCoreApplication.php : 62
AtivaPromocao->__construct(AtivaPromocao)
File: engine.php : 23
AdiantiCoreAdiantiCoreApplication::run(1)
File: engine.php : 64
TApplication::run(1)
IM

Troca combo por tentry
IM

Para Dbcombo o metodo eh outro
setChangeAction

E não setExitaction
LH

Quando eu dou um var_dump no $param ele não puxa o city_id.
LH

Resolvi, era só o filtro errado. Devia está puxando o ID. kkkk Vlwww