Lançado Adianti Framework 7.6!
Clique aqui para saber mais
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.                 
  82.                 TDBCombo::reloadFromModel('form_ativa''system_user_name''database''SystemUSer''id''name''name'$criteriaTRUE);
  83.             }
  84.             else
  85.             {
  86.                 TCombo::clearField('form_ativa''system_user_name');
  87.             }
  88.             
  89.             TTransaction::close();
  90.         }
  91.         catch (Exception $e)
  92.         {
  93.             new TMessage('error'$e->getMessage());
  94.         }
  95.         //var_dump($param);
  96.     }
  97.     /*public static function onChange($param)
  98.     {
  99.         TTransaction::open("database");
  100.         $criteria = new TCriteria();
  101.         $criteria->add(new TFilter('cpf', '=', $param['system_user_id']));
  102.         $repository = new TRepository("SystemUser");
  103.         $users = $repository->load($criteria);
  104.         $options = array();
  105.         foreach($users as $user){
  106.             $options[$user->id] = $user->name;
  107.         }
  108.             
  109.         //var_dump($param);
  110.         TCombo::reload('form_ativa', 'system_user_name', $options);
  111.         
  112.     }
  113.     public function onSave()
  114.     {
  115.         try {
  116.             $this->form->validate();
  117.             TTransaction::open( "database" );
  118.             $object = $this->form->getData("AtivaRecord");
  119.             $object->store();
  120.             TTransaction::close();
  121.             $action = new TAction( [ "VoucherList", "onReload" ] );
  122.             new TMessage( "info", "Registro salvo com sucesso!", $action );
  123.         } catch ( Exception $ex ) {
  124.             TTransaction::rollback();
  125.             new TMessage( "error", "Ocorreu um erro ao tentar salvar o registro!<br><br><br><br>" . $ex->getMessage() );
  126.         }
  127.     }*/
  128.     public function onEdit$param )
  129.     {
  130.         try {
  131.             if( isset( $param"key" ] ) ) {
  132.                 TTransaction::open"database" );
  133.                 $object = new AtivaRecord($param["key"]);
  134.                 $this->form->setData($object);
  135.                 TTransaction::close();
  136.             }
  137.         } catch ( Exception $ex ) {
  138.             TTransaction::rollback();
  139.             new TMessage"error""Ocorreu um erro ao tentar carregar o registro para edição!<br><br>" $ex->getMessage() );
  140.         }
  141.     }
  142. }

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


Dominando o Adianti 7 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