HT
Erro ao tentar editar form campos TDBUniqueSearch e TDBSelec
Pessoal, estou tentando editar uma informação de um formulário que busco do banco normalmente. Mas na hora de salvar, como valido alguns campos, apresenta a informação que o cod_cliente não esta preenchido. Primeiro estava com problema em trazer os dados de um TDBSelect (Agregação), que só consegui sobrescrevendo o metódo de edição, agora estou no cod_cliente, que é um TDBUniqueSearch. Ele preenche o campo com a informação, mais quando vai para função salvar, o valor do campo vai zerado.
- <?php
- class SacColetaForm extends TPage
- {
- protected $form; // form
- use Adianti\Base\AdiantiStandardFormTrait; // Standard form methods
- /**
- * Class constructor
- * Creates the page and the registration form
- */
- function __construct($param)
- {
- parent::__construct();
- $this->setDatabase('mysql'); // defines the database
- $this->setActiveRecord('SacColeta'); // defines the active record
- // creates the form
- $this->form = new BootstrapFormBuilder('form_SacColeta');
- $this->form->setFormTitle('Sac Coleta');
- // create the form fields
- $id = new TEntry('id');
- $data_coleta = new TDate('data_coleta');
- $data_saida = new TDate('data_saida');
- $cod_cliente = new TDBUniqueSearch('cod_cliente', 'sqlserver', 'Clien', 'Codigo', 'Razao_Social');
- $motivo = new TEntry('motivo', 'mysql', 'Motivo', 'id', 'descricao');
- $sac_proced_id = new TDBSelect('sac_proced_id', 'mysql', 'SacProcedimento', 'id', 'descricao');
- $sac_protoc_tipo_id = new TDBCombo('sac_protoc_tipo_id', 'mysql', 'SacProtocoloTipo', 'id', 'descricao');
- $observacao = new TEntry('observacao');
- $nota_fiscal = new TEntry('nota_fiscal');
- $controle = new TCombo('controle');
- $controle->addItems(array('E'=> 'Enviado', 'R'=> 'Recebido', 'A' => 'Arquivado'));
- // add the fields
- $this->form->addFields( [ new TLabel('Id') ], [ $id ] );
- $this->form->addFields( [ new TLabel('Tipo Protocolo') ], [ $sac_protoc_tipo_id ] );
- $this->form->addFields( [ new TLabel('Nota Fiscal') ], [ $nota_fiscal ] );
- $this->form->addFields( [ new TLabel('Data Coleta') ], [ $data_coleta ] );
- $this->form->addFields( [ new TLabel('Data Saida') ], [ $data_saida ] );
- $this->form->addFields( [ new TLabel('Cliente') ], [ $cod_cliente ] );
- $this->form->addFields( [ new TLabel('Motivo') ], [ $motivo ] );
- $this->form->addFields( [ new TLabel('Procedimento') ], [ $sac_proced_id ] );
- $this->form->addFields( [ new TLabel('Observacao') ], [ $observacao ] );
- $this->form->addFields( [ new TLabel('Controle') ], [ $controle ] );
- $nota_fiscal->setExitAction(new TAction(array($this, 'onExitNota')));
- // set sizes
- $id->setSize('100%');
- $data_coleta->setSize('100%');
- $data_saida->setSize('100%');
- $cod_cliente->setSize('100%');
- $motivo->setSize('100%');
- $sac_proced_id->setSize('100%', 200);
- $sac_protoc_tipo_id->setSize('100%');
- $observacao->setSize('100%');
- $nota_fiscal->setSize('100%');
- $nota_fiscal->setMask('99999999999');
- // add validation
- $sac_proced_id->addValidation( 'Procedimento', new TRequiredValidator );
- $sac_protoc_tipo_id->addValidation( 'Tipo Protocolo', new TRequiredValidator );
- $cod_cliente->addValidation( 'Cliente', new TRequiredValidator );
- $observacao->addValidation( 'Observação', new TRequiredValidator );
- $id->setEditable(FALSE);
- if (!empty($param['key']))
- {
- $data_coleta->setEditable(FALSE);
- $data_saida->setEditable(FALSE);
- $cod_cliente->setEditable(FALSE);
- $motivo->setEditable(FALSE);
- $sac_proced_id->setEditable(FALSE);
- $sac_protoc_tipo_id->setEditable(FALSE);
- $observacao->setEditable(FALSE);
- $nota_fiscal->setEditable(FALSE);
- TButton::enableField('form_SacColeta','salvar');
- }else{
- TButton::disableField('form_SacColeta','cancelar');
- TButton::disableField('form_SacColeta','fechar');
- }
- // create the form actions
- $btn = $this->form->addAction(_t('Save'), new TAction([$this, 'onSave']), 'fa:floppy-o');
- $btn->class = 'btn btn-sm btn-primary';
- $this->form->addAction('Voltar', new TAction(['SacColetaList', 'onReload']), 'fa:backward blue');
- $this->form->addAction('Fechar', new TAction([$this, 'onQuestionFechar']), 'fa:times green');
- $this->form->addAction('Cancelar', new TAction([$this, 'onQuestionCancel']), 'fa:eraser red');
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 100%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- parent::add($container);
- }
- function onQuestionFechar($param)
- {
- $this->form->setData($this->form->getData());
- if($param['controle'] == '')
- {
- new TMessage('info','O campo controle precisa ser definido!');
- }else{
- $action = new TAction(array($this, 'onFechar'));
- $action->setParameter('id', $param['id']);
- $action->setParameter('controle', $param['controle']);
- new TQuestion('Deseja realmente fechar a coleta?.', $action);
- }
- }
- function onFechar($param)
- {
- try
- {
- TTransaction::open('mysql');
- $coleta = new SacColeta($param['id']);
- $coleta->status = 'F';
- $coleta->controle = $param['controle'];
- $coleta->store();
- TTransaction::close();
- $action = new TAction(array('SacColetaList','onReload'));
- new TMessage('info','Coleta fechada com sucesso!', $action);
- }catch(Exception $e)
- {
- new TMessage('erro', $e->getMessage());
- }
- }
- function onQuestionCancel($param)
- {
- $action = new TAction(array($this, 'onCancel'));
- $action->setParameter('id', $param['id']);
- new TQuestion('Deseja realmente cancelar a coleta?.', $action);
- }
- function onCancel($param)
- {
- try
- {
- TTransaction::open('mysql');
- $coleta = new SacColeta($param['id']);
- $coleta->status = 'C';
- $coleta->store();
- TTransaction::close();
- $action = new TAction(array('SacColetaList','onReload'));
- new TMessage('info','Coleta cancelada com sucesso!', $action);
- }catch(Exception $e)
- {
- new TMessage('erro', $e->getMessage());
- }
- }
- /**
- * method onExitNota()
- * Action to input nota
- */
- static function onExitNota($param)
- {
- try
- {
- TTransaction::open('sqlserver');
- $nfscb = new Nfscb($param['nota_fiscal']);
- if(!empty($nfscb))
- {
- $object = new StdClass;
- $object->cod_cliente = $nfscb->Cod_Cliente;
- TForm::sendData('form_SacColeta', $object);
- }
- TTransaction::close();
- }catch(Exception $e){
- new TMessage('erro', $e->getMessage());
- }
- }
- /**
- * method onSave()
- * Executed whenever the user clicks at the save button
- */
- public function onSave($param)
- {
- try
- {
- $object = $this->form->getData();
- print_r($object);
- $this->form->validate();
- $object = $this->form->getData();
- $this->form->setData($object);
- TTransaction::open('mysql');
- $sacColeta = new SacColeta;
- TTransaction::close();
- TTransaction::open('sqlserver');
- $cliente = new Clien($object->cod_cliente);
- if(!empty($object->nota_fiscal))
- {
- $nfscb = new Nfscb($object->nota_fiscal);
- $sacColeta->nota_fiscal = $object->nota_fiscal;
- $sacColeta->vendedor = $nfscb->vendedor->Nome_Guerra;
- $sacColeta->operador = $nfscb->vendedor->Nome_Guerra;
- if($nfscb->Cod_Cliente != $object->cod_cliente)
- {
- throw new Exception('Nota Fiscal não esta relacionada a esse cliente!', 1);
- }
- }
- $sacColeta->data_emissao = date('Y-m-d');
- $sacColeta->data_coleta = $object->data_coleta;
- $sacColeta->data_saida = $object->data_saida;
- $sacColeta->cod_cliente = $object->cod_cliente;
- $sacColeta->razao = $cliente->Razao_Social;
- $sacColeta->cnpj = $cliente->Cgc_Cpf;
- $sacColeta->endereco = $cliente->Endereco;
- $sacColeta->numero = $cliente->Numero;
- $sacColeta->bairro = $cliente->bairro->Descricao;
- $sacColeta->cidade = $cliente->cidade->Descricao;
- $sacColeta->estado = $cliente->Cod_Estado;
- $sacColeta->sac_protoc_tipo_id = $object->sac_protoc_tipo_id;
- $sacColeta->motivo = $object->motivo;
- $sacColeta->observacao = $object->observacao;
- TTransaction::close();
- TTransaction::open('mysql');
- $sacColeta->protocolo = date('dmy'). str_pad(rand(1,100),3,0,STR_PAD_LEFT) . $sacColeta->getLastID() + 1;
- foreach ($object->sac_proced_id as $key => $value)
- {
- $sac_proc = new SacProcedimento($value);
- $sacColeta->addSacColetaProcedimento($sac_proc);
- }
- $sacColeta->store();
- // close the transaction
- TTransaction::close();
- $pdf = new SacProtocoloDownload;
- $pdf->protocoloPdfColeta($sacColeta->id);
- $action = new TAction(array('SacColetaList','onReload'));
- new TMessage('info', AdiantiCoreTranslator::translate('Record saved'), $action);
- }
- catch (Exception $e) // in case of exception
- {
- // get the form data
- $object = $this->form->getData();
- // fill the form with the active record data
- $this->form->setData($object);
- // shows the exception error message
- new TMessage('error', $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method onEdit()
- * Executed whenever the user clicks at the edit button da datagrid
- * @param $param An array containing the GET ($_GET) parameters
- */
- public function onEdit($param)
- {
- try
- {
- if (empty($this->database))
- {
- throw new Exception(AdiantiCoreTranslator::translate('^1 was not defined. You must call ^2 in ^3', AdiantiCoreTranslator::translate('Database'), 'setDatabase()', AdiantiCoreTranslator::translate('Constructor')));
- }
- if (empty($this->activeRecord))
- {
- throw new Exception(AdiantiCoreTranslator::translate('^1 was not defined. You must call ^2 in ^3', 'Active Record', 'setActiveRecord()', AdiantiCoreTranslator::translate('Constructor')));
- }
- if (isset($param['key']))
- {
- // get the parameter $key
- $key=$param['key'];
- // open a transaction with database
- TTransaction::open($this->database);
- $class = $this->activeRecord;
- // instantiates object
- $object = new $class($key);
- // AQUI TIVE QUE FAZER ASSIM PARA PREENCHER O CAMPO TDBSelect.
- $object->sac_proced_id = $object->getSacColetaProcedimentos();
- // fill the form with the active record data
- $this->form->setData($object);
- // close the transaction
- TTransaction::close();
- return $object;
- }
- else
- {
- $this->form->clear();
- }
- }
- catch (Exception $e) // in case of exception
- {
- // shows the exception error message
- new TMessage('error', $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- }
- ?>
O var_dump de $param na função onSave retorna esse campo?
Sim, mas retorna vazio
O sistema carrega o cliente no campo, mas na hora de salvar, informa o campo vazio.