CJ
Erro nos campos vetoriais ao salvar
Pessoal,
Quando salvo os dados do formulário, na aba Cursos não exibe os dados salvos no BD. O que posso fazer para listá-los depois de salvar.
PS: Ao editar o registro os dados são preenchidos corretamente.
Quando salvo os dados do formulário, na aba Cursos não exibe os dados salvos no BD. O que posso fazer para listá-los depois de salvar.
PS: Ao editar o registro os dados são preenchidos corretamente.
- <?php
- /**
- * FuncionarioForm Form
- * @author <your name here>
- */
- class FuncionarioForm extends TPage
- {
- protected $form; // form
- private $cursos;
- /**
- * Form constructor
- * @param $param Request
- */
- public function __construct( $param )
- {
- parent::__construct();
- // creates the form
- $this->form = new BootstrapFormBuilder('form_Funcionario');
- $this->form->setFormTitle('Funcionário');
- // create the form fields
- $id = new TEntry('id');
- $nome = new TEntry('nome');
- $endereco = new TEntry('endereco');
- $numero = new TEntry('numero');
- $complem = new TEntry('complem');
- $bairro = new TEntry('bairro');
- $cidade = new TEntry('cidade');
- $uf = new TCombo('uf');
- $cep = new TEntry('cep');
- $nacionalidade = new TEntry('nacionalidade');
- $naturalidade = new TEntry('naturalidade');
- $uf_natural = new TCombo('uf_natural');
- $data_nascimento = new TDate('data_nascimento');
- $telefone = new TEntry('telefone');
- $celular = new TEntry('celular');
- $email = new TEntry('email');
- $sexo = new TCombo('sexo');
- $estadocivil_id = new TDBCombo('estadocivil_id', 'sispessoal', 'Estadocivil','id','nome','nome');
- $racacor_id = new TDBCombo('racacor_id', 'sispessoal', 'Racacor','id','nome','nome');
- $pai = new TEntry('pai');
- $mae = new TEntry('mae');
- $grauinstrucao_id = new TDBCombo('grauinstrucao_id', 'sispessoal','Grauinstrucao', 'id','nome','nome');
- $rg = new TEntry('rg');
- $rg_data = new TDate('rg_data');
- $rg_orgao = new TEntry('rg_orgao');
- $cpf = new TEntry('cpf');
- $ct = new TEntry('ct');
- $ct_serie = new TEntry('ct_serie');
- $ct_uf = new TCombo('ct_uf');
- $ct_data = new TDate('ct_data');
- $reg_profissional = new TEntry('reg_profissional');
- $cnh = new TEntry('cnh');
- $titulo = new TEntry('titulo');
- $titulo_zona = new TEntry('titulo_zona');
- $titulo_secao = new TEntry('titulo_secao');
- $titulo_cidade = new TEntry('titulo_cidade');
- $pis_pasep = new TEntry('pis_pasep');
- $pis_pasep_data = new TDate('pis_pasep_data');
- $obs = new TText('obs');
- $pendencias = new TText('pendencias');
- $ativo = new TRadioGroup('ativo');
- $banco_id = new TDBCombo('banco_id','sispessoal','Banco','id','nome','nome');
- $agencia = new TEntry('agencia');
- $conta = new TEntry('conta');
- $operacao = new TEntry('operacao');
- // Tamanho dos campos
- $nome->setSize('100%');
- $endereco->setSize('100%');
- $numero->setSize('100%');
- $complem->setSize('100%');
- $bairro->setSize('100%');
- $cidade->setSize('100%');
- $uf->setSize('100%');
- $cep->setSize('100%');
- $nacionalidade->setSize('100%');
- $naturalidade->setSize('100%');
- $uf_natural->setSize('100%');
- $data_nascimento->setSize('100%');
- $telefone->setSize('100%');
- $celular->setSize('100%');
- $email->setSize('100%');
- $sexo->setSize('100%');
- $estadocivil_id->setSize('100%');
- $racacor_id->setSize('100%');
- $pai->setSize('100%');
- $mae->setSize('100%');
- $grauinstrucao_id->setSize('100%');
- $rg->setSize('100%');
- $rg_data->setSize('100%');
- $rg_orgao->setSize('100%');
- $cpf->setSize('100%');
- $ct->setSize('100%');
- $ct_serie->setSize('100%');
- $ct_uf->setSize('100%');
- $ct_data->setSize('100%');
- $reg_profissional->setSize('100%');
- $cnh->setSize('100%');
- $titulo->setSize('100%');
- $titulo_zona->setSize('100%');
- $titulo_secao->setSize('100%');
- $titulo_cidade->setSize('100%');
- $pis_pasep->setSize('100%');
- $pis_pasep_data->setSize('100%');
- $obs->setSize('100%');
- $pendencias->setSize('100%');
- $ativo->setSize('100%');
- $banco_id->setSize('100%');
- $agencia->setSize('100%');
- $conta->setSize('100%');
- $operacao->setSize('100%');
- $options_ativo = ['S'=>'Sim', 'N'=>'Não'];
- $ativo->addItems($options_ativo);
- $ativo->setLayout('horizontal');
- $options_sexo = ['M'=>'Masculino', 'F'=>'Feminino'];
- $sexo->addItems($options_sexo);
- $options_uf = [
- 'AC'=>'Acre',
- 'AL'=>'Alagoas',
- 'AP'=>'Amapá',
- 'AM'=>'Amazonas',
- 'BA'=>'Bahia',
- 'CE'=>'Ceará',
- 'DF'=>'Distrito Federal',
- 'ES'=>'Espírito Santo',
- 'GO'=>'Goiás',
- 'MA'=>'Maranhão',
- 'MT'=>'Mato Grosso',
- 'MS'=>'Mato Grosso do Sul',
- 'MG'=>'Minas Gerais',
- 'PA'=>'Pará',
- 'PB'=>'Paraíba',
- 'PR'=>'Paraná',
- 'PE'=>'Pernambuco',
- 'PI'=>'Piauí',
- 'RJ'=>'Rio de Janeiro',
- 'RN'=>'Rio Grande do Norte',
- 'RS'=>'Rio Grande do Sul',
- 'RO'=>'Rondônia',
- 'RR'=>'Roraima',
- 'SC'=>'Santa Catarina',
- 'SP'=>'São Paulo',
- 'SE'=>'Sergipe',
- 'TO'=>'Tocantins'
- ];
- $uf->addItems($options_uf);
- $uf_natural->addItems($options_uf);
- $ct_uf->addItems($options_uf);
- // Máscaras dos campos
- $id->setEditable(FALSE);
- $nome->forceUpperCase();
- $endereco->forceUpperCase();
- $numero->forceUpperCase();
- $complem->forceUpperCase();
- $bairro->forceUpperCase();
- $cidade->forceUpperCase();
- $uf->enableSearch();
- $cep->setMask('99999-999');
- $nacionalidade->forceUpperCase();
- $naturalidade->forceUpperCase();
- $uf_natural->enableSearch();
- $data_nascimento->setMask('dd/mm/yyyy', false);
- $data_nascimento->setDatabaseMask('yyyy-mm-dd');
- $telefone->setMask('(99)9999-9999', true);
- $celular->setMask('(99)99999-9999', true);
- $email->forceLowerCase();
- $sexo->enableSearch();
- $estadocivil_id->enableSearch();
- $racacor_id->enableSearch();
- $pai->forceUpperCase();
- $mae->forceUpperCase();
- $grauinstrucao_id->enableSearch();
- $rg->forceUpperCase();
- $rg_data->setMask('dd/mm/yyyy', false);
- $rg_data->setDatabaseMask('yyyy-mm-dd');
- $rg_orgao->forceUpperCase();
- $cpf->setMask('999.999.999-99');
- $ct->forceUpperCase();
- $ct_serie->forceUpperCase();
- $ct_uf->enableSearch();
- $ct_data->setMask('dd/mm/yyyy', false);
- $ct_data->setDatabaseMask('yyyy-mm-dd');
- $reg_profissional->forceUpperCase();
- $cnh->forceUpperCase();
- $titulo->forceUpperCase();
- $titulo_zona->forceUpperCase();
- $titulo_secao->forceUpperCase();
- $titulo_cidade->forceUpperCase();
- $pis_pasep->forceUpperCase();
- $pis_pasep_data->setMask('dd/mm/yyyy', false);
- $pis_pasep_data->setDatabaseMask('yyyy-mm-dd');
- $ativo->setUseButton(true);
- $banco_id->enableSearch();
- $agencia->forceUpperCase();
- $conta->forceUpperCase();
- $operacao->forceUpperCase();
- // Aba Dados Gerais
- $this->form->appendPage('Dados Gerais');
- $this->form->addContent([ new TFormSeparator('Funcionário', '#18953c', '18', '#eeeeee')] );
- $this->form->addFields( [ new TLabel('Id') ], [$id]);
- $this->form->addFields( [ new TLabel('Ativo')], [$ativo], [new TLabel('CPF', 'red')], [$cpf] );
- $this->form->addFields( [ new TLabel('Nome', 'red')], [$nome] );
- $this->form->addFields( [ new TLabel('Data Nasc') ], [ $data_nascimento ], [new TLabel('Sexo') ], [ $sexo ] );
- $this->form->addFields( [ new TLabel('Raça/Cor') ], [ $racacor_id ], [new TLabel('Estado Civil') ], [ $estadocivil_id ] );
- $this->form->addFields( [ new TLabel('Naturalidade') ], [ $naturalidade], [ new TLabel('UF') ], [ $uf_natural ] );
- $this->form->addFields( [ new TLabel('Grau de Instrução', 'red') ], [ $grauinstrucao_id]);
- $this->form->addContent([ new TFormSeparator('Filiação', '#18953c', '18', '#eeeeee')] );
- $this->form->addFields( [ new TLabel('Nome Pai') ], [ $pai ], [ new TLabel('Nome Mãe') ], [ $mae ] );
- $this->form->addContent([ new TFormSeparator('Endereço', '#18953c', '18', '#eeeeee')] );
- $this->form->addFields( [ new TLabel('Endereço') ], [ $endereco ], [new TLabel('Número') ], [ $numero ] );
- $this->form->addFields( [ new TLabel('Complem') ], [ $complem ], [new TLabel('Bairro') ], [ $bairro ] );
- $this->form->addFields( [ new TLabel('Cidade') ], [ $cidade ], [ new TLabel('UF') ], [ $uf ]);
- $this->form->addFields( [ new TLabel('CEP') ], [ $cep ], [ new TLabel('Telefone Fixo') ], [ $telefone ] );
- $this->form->addFields( [ new TLabel('Celular') ], [ $celular ], [ new TLabel('E-mail') ], [ $email ] );
- $this->form->addContent([ new TFormSeparator('Dados Bancários', '#18953c', '18', '#eeeeee')] );
- $this->form->addFields( [ new TLabel('Banco')], [$banco_id], [ new TLabel('Operação') ],[ $operacao ] );
- $this->form->addFields( [ new TLabel('Agencia')], [$agencia], [ new TLabel('Conta')],[ $conta] );
- // Aba Documentação
- $this->form->appendPage('Documentação');
- $this->form->addContent([ new TFormSeparator('Identidade', '#18953c', '18', '#eeeeee')] );
- $this->form->addFields( [ new TLabel('Número') ], [$rg], [ new TLabel('Data Expedição') ], [$rg_data], [ new TLabel('Emissor') ], [$rg_orgao]);
- $this->form->addContent([ new TFormSeparator('Carteira de Trabalho', '#18953c', '18', '#eeeeee')] );
- $this->form->addFields( [ new TLabel('Número') ], [$ct], [ new TLabel('Série') ], [$ct_serie]);
- $this->form->addFields( [ new TLabel('UF') ], [$ct_uf] , [ new TLabel('Data') ], [$ct_data]);
- $this->form->addContent([ new TFormSeparator('Titulo Eleitoral', '#18953c', '18', '#eeeeee')] );
- $this->form->addFields( [ new TLabel('Número') ], [$titulo], [ new TLabel('Zona') ], [$titulo_zona]);
- $this->form->addFields( [ new TLabel('Seção') ], [$titulo_secao] , [ new TLabel('Cidade/UF') ], [$titulo_cidade]);
- $this->form->addContent([ new TFormSeparator('PIS/PASEP', '#18953c', '18', '#eeeeee')] );
- $this->form->addFields( [ new TLabel('Número') ], [$pis_pasep], [ new TLabel('Data Emissão') ], [$pis_pasep_data]);
- $this->form->addContent([ new TFormSeparator('Outros', '#18953c', '18', '#eeeeee')] );
- $this->form->addFields( [ new TLabel('Registro Profissional') ], [$reg_profissional], [ new TLabel('CNH') ], [$cnh]);
- // Aba Cursos
- $this->form->appendPage('Cursos');
- $curso_id = new TDBCombo('curso_id[]', 'sispessoal', 'Curso', 'id', 'nome', 'nome');
- $curso_id->enableSearch();
- $curso_id->setSize('400');
- $this->cursos = new TFieldList;
- $this->cursos->addField( '<b>Curso</b>', $curso_id);
- $this->form->addField($curso_id);
- $this->form->addContent( [ $this->cursos ] );
- $this->form->appendPage('Especializações');
- $this->form->appendPage('Histórico Admissional');
- // Aba Outras informações
- $this->form->appendPage('Outras informações');
- $this->form->addFields( [ new TLabel('Pendências') ], [$pendencias]);
- $this->form->addFields( [ new TLabel('Observações') ], [$obs]);
- // create the form actions
- $btn = $this->form->addAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
- $btn->class = 'btn btn-sm btn-primary';
- $this->form->addAction(_t('New'), new TAction(array($this, 'onClear')), 'bs:plus-sign green');
- $this->form->addAction( 'Listagem de Funcionários', new TAction(array('FuncionarioList', 'onReload')), 'fa:table blue' );
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 100%';
- $container->add( $this->form);
- parent::add($container);
- }
- /**
- * Save form data
- * @param $param Request
- */
- public function onSave( $param )
- {
- try
- {
- TTransaction::open('sispessoal'); // open a transaction
- $this->form->validate();
- $funcionario = new Funcionario;
- $funcionario->fromArray( $param );
- if( !empty($param['curso_id']) AND is_array($param['curso_id']) )
- {
- foreach( $param['curso_id'] as $row => $curso_id)
- {
- if ($curso_id)
- {
- $curso = new Curso;
- $curso->id = $curso_id;
- $funcionario->addCurso($curso);
- }
- }
- }
- $funcionario->store();
- $data = new stdClass;
- $data->id = $funcionario->id;
- $this->cursos = $funcionario->getCursos();
- TForm::sendData('form_Funcionario', $data);
- TTransaction::close(); // close the transaction
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- $this->form->setData( $this->form->getData() ); // keep form data
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * Clear form data
- * @param $param Request
- */
- public function onClear( $param )
- {
- $this->form->clear(TRUE);
- $this->cursos->addHeader();
- $this->cursos->addDetail( new stdClass );
- $this->cursos->addCloneAction();
- }
- /**
- * Load object to form data
- * @param $param Request
- */
- public function onEdit( $param )
- {
- try
- {
- if (isset($param['key']))
- {
- $key = $param['key']; // get the parameter $key
- TTransaction::open('sispessoal'); // open a transaction
- $object = new Funcionario($key); // instantiates the Active Record
- $cursos = $object->getCursos();
- if ($cursos)
- {
- $this->cursos->addHeader();
- foreach ($cursos as $curso)
- {
- $curso_detail = new stdClass;
- $curso_detail->curso_id = $curso->id;
- $this->cursos->addDetail($curso_detail);
- }
- $this->cursos->addCloneAction();
- }
- else
- {
- $this->onClear($param);
- }
- $this->form->setData($object); // fill the form
- TTransaction::close(); // close the transaction
- }
- else
- {
- $this->form->clear(TRUE);
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- }
- ?>
Geralmente nos forms vetoriais a função onSave é static, o que evita esse problema.
Mas se quiser manter dessa maneira, tem que readicionar os dados à tela, como na onEdit:
Nataniel,
Resolvi transformando a função em static.
Obrigado