RC
Preview de Imagens
Boa noite, comunidade.
Utilizei o exemplo do ProductForm (www.adianti.com.br/framework_files/tutor/index.php?class=ProductForm) para fazer o preview da imagem antes de salvar.
Entretanto, a imagem não aparece para visualização.
Alguém consegue identificar qual o problema?
Utilizei o exemplo do ProductForm (www.adianti.com.br/framework_files/tutor/index.php?class=ProductForm) para fazer o preview da imagem antes de salvar.
Entretanto, a imagem não aparece para visualização.
Alguém consegue identificar qual o problema?
- <?php
- /**
- * SocioForm Form
- * @author <your name here>
- */
- class SocioForm extends TPage
- {
- protected $form; // form
- private $frame;
- /**
- * Form constructor
- * @param $param Request
- */
- public function __construct( $param )
- {
- parent::__construct();
- // creates the form
- $this->form = new TForm('form_Socio');
- $this->form->class = 'tform'; // change CSS class
- //$this->form = new BootstrapFormWrapper($this->form);
- $this->form->style = 'display: table;width:100%'; // change style
- $notebook1 = new BootstrapNotebookWrapper( new TNotebook(500, 460) );
- $this->form->add($notebook1);
- // create the form fields
- $id = new TEntry('id');
- $id->setEditable(FALSE);
- $id->setSize(100);
- $nome = new TEntry('nome');
- $nome->setSize('100%');
- $nome->addValidation('nome', new TRequiredValidator);
- $cpf = new TEntry('cpf');
- $cpf->addValidation('cpf', new TCPFValidator);
- $cpf->setMask('999.999.999-99');
- $cpf->setSize(200);
- $rg = new TEntry('rg');
- $rg->setSize(200);
- $data_nascimento = new TDate('data_nascimento');
- $data_nascimento->setMask('dd/mm/yyyy');
- $data_nascimento->setSize(178);
- $cadastrado = new TDate('cadastrado');
- $cadastrado->setEditable(FALSE);
- $cadastrado->setSize(200);
- $pai = new TEntry('pai');
- $pai->setSize('100%');
- $mae = new TEntry('mae');
- $mae->setSize('100%');
- $ativo = new TEntry('ativo');
- $ativo->setEditable(FALSE);
- $ativo->setSize(200);
- $sexo_id = new TDBRadioGroup('sexo_id', 'sistema', 'Sexo', 'id', 'descricao');
- $sexo_id->setLayout('horizontal');
- $estado_civil_id = new TDBCombo('estado_civil_id', 'sistema', 'EstadoCivil', 'id', 'descricao');
- $estado_civil_id->setSize(200);
- $cidade_nascimento_id = new ">TDBSeekButton('cidade_nascimento_id', 'sistema', 'form_Socio', 'Cidade', 'nome', 'cidade_nascimento_id', 'cidade_nascimento_nome');
- $cidade_nascimento_id->setSize(30);
- $cidade_nascimento_nome = new TEntry('cidade_nascimento_nome');
- $cidade_nascimento_nome->setEditable(FALSE);
- $cidade_nascimento_nome->setSize(146);
- $email = new TEntry('email');
- $email->addValidation('email', new TEmailValidator);
- $email->setSize('100%');
- $matricula = new TEntry('matricula');
- $matricula->addValidation('matricula', new TNumericValidator);
- $matricula->setSize(200);
- $ade = new TEntry('ade');
- $ade->setSize(200);
- $data_admissao = new TDate('data_admissao');
- $data_admissao->setMask('dd/mm/yyyy');
- $data_admissao->setSize(178);
- $cargo_funcao_id = new TDBCombo('cargo_funcao_id', 'sistema', 'CargoFuncao', 'id', 'descricao');
- $cargo_funcao_id->setSize(200);
- $departamento_id = new ">TDBSeekButton('departamento_id', 'sistema', 'form_Socio', 'Departamento', 'sigla', 'departamento_id', 'departamento_sigla');
- $departamento_id->setSize(30);
- $departamento_sigla = new TEntry('departamento_sigla');
- $departamento_sigla->setEditable(FALSE);
- $departamento_sigla->setSize(146);
- //prepara a tabela de dados pessoais
- $pessoais = new TTable;
- //$pessoais->border = '1';
- $row = $pessoais->addRow()->addCell(new TLabel(''))->width = 80;
- $row = $pessoais->addRow();
- $row->addCell(new TLabel('Id'));
- $row->addCell($id)->colspan = 3;
- $row = $pessoais->addRow();
- $row->addCell(new TLabel('Nome'));
- $row->addCell($nome)->colspan = 3;
- $row = $pessoais->addRow();
- $row->addCell(new TLabel('Mãe'));
- $row->addCell($mae)->colspan = 3;
- $row = $pessoais->addRow();
- $row->addCell(new TLabel('Pai'));
- $row->addCell($pai)->colspan = 3;
- $row = $pessoais->addRow();
- $row->addCell(new TLabel('E-mail'));
- $row->addCell($email)->colspan = 3;
- $pessoais->addRowSet( new TLabel(''));
- $row = $pessoais->addRow();
- $row->addCell(new TLabel(''));
- $row->addCell(new TLabel('CPF'))->width = 250;
- $row->addCell(new TLabel('Identidade'))->colspan = 2;
- $row = $pessoais->addRow();
- $row->addCell(new TLabel(''));
- $row->addCell($cpf);
- $row->addCell($rg)->colspan = 2;
- $row = $pessoais->addRow();
- $row->addCell(new TLabel(''));
- $row->addCell(new TLabel('Data de Nascimento'));
- $row->addCell(new TLabel('Cidade de Nascimento'))->colspan = 2;
- $row = $pessoais->addRow();
- $row->addCell(new TLabel(''));
- $row->addCell($data_nascimento);
- $row->addCell($cidade_nascimento_id);
- $row->addCell($cidade_nascimento_nome);
- $row = $pessoais->addRow();
- $row->addCell(new TLabel(''));
- $row->addCell(new TLabel('Estado Civil'));
- $row->addCell(new TLabel('Sexo'))->colspan = 2;
- $row = $pessoais->addRow();
- $row->addCell(new TLabel(''));
- $row->addCell($estado_civil_id);
- $row->addCell($sexo_id)->colspan = 2;
- $row = $pessoais->addRow();
- $row->addCell(new TLabel(''));
- $row->addCell(new TLabel('Situacao'));
- $row->addCell(new TLabel('Data de Cadastro'))->colspan = 2;
- $row = $pessoais->addRow();
- $row->addCell(new TLabel(''));
- $row->addCell($ativo);
- $row->addCell($cadastrado)->colspan = 2;
- //prepara a tabela de dados profissionais
- $profissional = new TTable;
- //$profissional->border = '1';
- $profissional->addRowSet( new TLabel(''));
- $row = $profissional->addRow();
- $row->addCell(new TLabel('Matrícula'))->colspan = 2;
- $row->addCell(new TLabel('Data de Admissão'));
- $row = $profissional->addRow();
- $row->addCell($matricula)->colspan = 2;
- $row->addCell($data_admissao);
- $row = $profissional->addRow();
- $row->addCell(new TLabel('ADE'))->colspan = 2;
- $row->addCell(new TLabel('Cargo/Função'));
- $row = $profissional->addRow();
- $row->addCell($ade)->colspan = 2;
- $row->addCell($cargo_funcao_id);
- $row = $profissional->addRow();
- $row->addCell(new TLabel('Lotação'))->colspan = 2;
- $row = $profissional->addRow();
- $row->addCell($departamento_id);
- $row->addCell($departamento_sigla)->width = 200;
- //prepara a tabela de endereco
- $endereco_id = new TEntry('endereco_id');
- $endereco = new TTable;
- //$endereco->border = '1';
- $endereco->addRowSet( new TLabel(''));
- $cep = new TEntry('cep');
- $cep->setMask('99.999-999');
- $cep->setSize(100);
- $busca_cep = new TAction( array($this, 'onBuscaCEP') );
- $cep->setExitAction($busca_cep);
- $estado_id = new TEntry('estado_id');
- $estado_id->setEditable(FALSE);
- $estado_id->setSize(200);
- $cidade_id = new TEntry('cidade_id');
- $cidade_id->setEditable(FALSE);
- $cidade_id->setSize(200);
- $bairro_id = new TEntry('bairro_id');
- $bairro_id->setEditable(FALSE);
- $bairro_id->setSize(200);
- $logradouro_id = new TEntry('logradouro_id');
- $logradouro_id->setSize(450);
- $numero = new TEntry('numero');
- $numero->setSize(200);
- $complemento = new TEntry('complemento');
- $complemento->setSize(200);
- $ponto_referencia = new TText('ponto_referencia');
- $ponto_referencia->setSize(450, 70);
- $row = $endereco->addRow();
- $row->addCell(new TLabel('CEP'))->colspan = 2;
- $row->addCell(new TLabel('Estado'));
- $row = $endereco->addRow();
- $row->addCell($cep)->colspan = 2;
- $row->addCell($estado_id);
- $row = $endereco->addRow();
- $row->addCell(new TLabel('Cidade'))->colspan = 2;
- $row->addCell(new TLabel('Bairro'));
- $row = $endereco->addRow();
- $row->addCell($cidade_id)->colspan = 2;
- $row->addCell($bairro_id);
- $row = $endereco->addRow();
- $row->addCell(new TLabel('Logradouro'))->colspan = 3;
- $row = $endereco->addRow();
- $row->addCell($logradouro_id)->colspan = 3;
- $row = $endereco->addRow();
- $row->addCell(new TLabel('Número'))->colspan = 2;
- $row->addCell(new TLabel('Complemento'));
- $row = $endereco->addRow();
- $row->addCell($numero)->colspan = 2;
- $row->addCell($complemento);
- $row = $endereco->addRow();
- $row->addCell(new TLabel('Ponto de Referência'))->colspan = 3;
- $row = $endereco->addRow();
- $cell = $row->addCell('');
- $cell->width = 500;
- $cell->colspan = 3;
- $cell->add($ponto_referencia);
- //montando o TMultiField de telefones
- $telefones = new TMultiField('telefones');
- $telefones->setHeight(160);
- $telefones->setOrientation('horizontal');
- $ddd = new TEntry('ddd');
- $ddd->setSize(70);
- $ddd->setMaxLength(2);
- $numero = new TEntry('numero');
- $numero->setSize(150);
- $telefones->addField('ddd', 'DDD', $ddd, 70);
- $telefones->addField('numero', 'Numero', $numero, 150, TRUE);
- //$operadora_id = new TDBCombo('operadora_id', 'sistema', 'Operadora', 'id', 'nome');
- //$operadora_id->setLayout('horizontal');
- $operadora = new TComboCombined('operadora_id', 'operadora_nome');
- $operadora->setSize(150);
- try
- {
- TTransaction::open('sistema');
- $items = Operadora::getObjects();
- $array = [];
- foreach ($items as $item)
- {
- $array[$item->id] = $item->nome;
- }
- $operadora->addItems( $array );
- $telefones->addField('operadora', 'Operadora', $operadora, 174);
- TTransaction::close();
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- //prepara a tabela de foto
- $table_foto = new TTable;
- //$table_foto->border = '1';
- $table_foto->addRowSet( new TLabel(''));
- $foto = new TFile('foto');
- $foto->setSize(350);
- // complete upload action
- $foto->setCompleteAction(new TAction(array($this, 'onComplete')));
- $table_foto->addRowSet(new TLabel('Selecione a Foto do Sócio'));
- $table_foto->addRowSet($foto);
- $this->frame = new TElement('div');
- $this->frame->id = 'photo_frame';
- $this->frame->style = 'width:400px; height:auto; min-height:200px; border:1px solid gray; padding:4px;';
- $table_foto->addRowSet($this->frame);
- //montando o TMultiField de dependentes
- $dependentes = new TMultiField('dependentes');
- $dependentes->setHeight(200);
- //$dependentes->setOrientation('horizontal');
- $dependente_nome = new TEntry('dependente_nome');
- $dependente_nome->setSize(350);
- $dependente_cpf = new TEntry('dependente_cpf');
- $dependente_cpf->addValidation('dependente_cpf', new TCPFValidator);
- $dependente_cpf->setMask('999.999.999-99');
- $dependente_cpf->setSize(200);
- $dependente_rg = new TEntry('dependente_rg');
- $dependente_rg->setSize(200);
- $dependente_data_nascimento = new TDate('dependente_data_nascimento');
- $dependente_data_nascimento->setMask('dd/mm/yyyy');
- $dependente_data_nascimento->setSize(178);
- $dependentes->addField('dependente_nome', 'Nome', $dependente_nome, 300, TRUE);
- $dependentes->addField('dependente_data_nascimento', 'Data de Nascimento', $dependente_data_nascimento, 80, TRUE);
- $dependentes->addField('dependente_cpf', 'CPF', $dependente_cpf, 120);
- $dependentes->addField('dependente_rg', 'Identidade', $dependente_rg, 150);
- $parentesco = new TComboCombined('parentesco_id', 'parentesco_nome');
- $parentesco->setSize(120);
- try
- {
- TTransaction::open('sistema');
- $items = Parentesco::getObjects();
- $array = [];
- foreach ($items as $item)
- {
- $array[$item->id] = $item->descricao;
- }
- $parentesco->addItems( $array );
- $dependentes->addField('parentesco', 'Parentesco', $parentesco, 174);
- TTransaction::close();
- }
- catch (Exception $e)
- {
- new TMessage('error', $e->getMessage());
- }
- $notebook1->appendPage('Dados Pessoais', $pessoais);
- $notebook1->appendPage('Dados Profissionais', $profissional);
- $notebook1->appendPage('Endereço', $endereco);
- $notebook1->appendPage('Telefones', $telefones);
- $notebook1->appendPage('Foto', $table_foto);
- $notebook1->appendPage('Dependentes', $dependentes);
- // create the form actions
- /*
- $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'fa:floppy-o');
- $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onClear')), 'bs:plus-sign green');
- */
- $button = TButton::create('save', array($this, 'onSave'), 'Save', 'fa:save');
- $this->form->setFields( array($id, $nome, $cpf, $rg, $data_nascimento, $cadastrado,
- $pai, $mae, $ativo, $sexo_id, $estado_civil_id, $cidade_nascimento_id,
- $cidade_nascimento_nome, $email, $matricula, $ade, $data_admissao, $foto,
- $departamento_id, $cargo_funcao_id, $endereco_id, $telefones,
- $cep, $estado_id, $cidade_id, $bairro_id, $logradouro_id, $numero,
- $complemento, $ponto_referencia) );
- $this->form->addField($button);
- $buttons_box = new TVBox;
- $buttons_box->add($button);
- // add a row for the form action
- $botoes = new TTable;
- $botoes->border = '1';
- $row = $botoes->addRow();
- $row->class = 'tformaction'; // CSS class
- $row->addCell($buttons_box)->colspan = 2;
- // vertical box container
- $container = new TVBox;
- $container->style = 'width: 90%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add(TPanelGroup::pack('Cadastro de Sócio', $this->form));
- parent::add($container);
- parent::add($botoes);
- /*
- $botoes = new TTable;
- $botoes->width = '90%';
- //$botoes->border = '1';
- $row = $botoes->addRow();
- $cell = $row->addCell($button);
- $cell->class = 'formaction';
- parent::add($botoes);*/
- }
- /**
- * On complete upload
- */
- public static function onComplete($param)
- {
- new TMessage('info', 'Carregamento Completo: '.$param['foto']);
- // refresh photo_frame
- TScript::create("$('#photo_frame').html('')");
- TScript::create("$('#photo_frame').append(\\"<img style='width:100%' src='tmp/{$param['foto']}'>\\");");
- }
- /**
- * method onBuscaCEP($param)
- * Executado quando sair do campo $cep
- * Busca o endereco conforme o cep informado, para preencher os demais campos do endereco
- */
- public static function onBuscaCEP($param)
- {
- //$obj = new StdClass;
- $obj = $this->form->getData('Endereco');
- //$data = new PCepProgs($param['cep']);
- //$data = new PCepProgs('51350250');
- print_r($obj);die;
- $obj->logradouro = $data->getRua();
- $obj->cidade = $data->getCidade();
- $obj->bairro = $data->getBairro();;
- $obj->estado_id = $data->getUf();;
- //TForm::sendData('form_Clientes', $obj);
- $this->form->setData($obj);
- }
- /**
- * Save form data
- * @param $param Request
- */
- public function onSave( $param )
- {
- try
- {
- TTransaction::open('sistema'); // open a transaction
- /**
- // Enable Debug logger for SQL operations inside the transaction
- TTransaction::setLogger(new TLoggerSTD); // standard output
- TTransaction::setLogger(new TLoggerTXT('log.txt')); // file
- **/
- $this->form->validate(); // validate form data
- $object = new Socio; // create an empty object
- $data = $this->form->getData(); // get form data as array
- $object->fromArray( (array) $data); // load the object with data
- $object->store(); // save the object
- // get the generated id
- $data->id = $object->id;
- $this->form->setData($data); // fill form 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();
- }
- /**
- * 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('sistema'); // open a transaction
- $object = new Socio($key); // instantiates the Active Record
- $this->form->setData($object); // fill the form
- TTransaction::close(); // close the transaction
- $image = new TImage($object->foto);
- $image->style = 'width: 100%';
- $this->frame->add( $image );
- }
- else
- {
- $this->form->clear();
- }
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- }
- ?>
Apos o upload a imagem foi transferida para a pasta tmp ?
O arquivo foi transferido com sucesso para a pasta tmp.
Inspeciona o caminho da imagem para ver se ficou o src='tmp/2.jpg' , ou seja, se o caminho ficou certinho.
Bom dia Ricardo Camara!
testei com a seguinte alteração e deu ok
Estou com esse mesmo problema....
Boa Tarde!
Estou com problema bastante semelhante. Após selecionar a foto não exibe o preview.
O src está correto tmp/foto.jpg mas não exibe a imagem. No onEdit exibe normalmente, e se, durante o preview eu copio ela do /tmp para a raiz do projeto, e altero no src pelo inspecionar elemento exibe normalmente. Voltando o /tmp no src já não exibe (mesmo com ela lá no /tmp).
Obrigado a todos pela participação, especialmente ao Jorge Cenci,
eu descobri o problema.
Tinha um arquivo .htaccess dentro da pasta tmp, depois que o exclui funcionou perfeitamente.