RC
Data desconfigurada na na Datagrid e no formulário
Fechado
Olá pessoal. Alguém poderia me ajudar a resolver dois erros que estou tendo?
Criei um formulário de cadastro de consulta, onde no mesmo tem um campo do tipo TDate! Acontece que quando eu salvo e clico no botão "Voltar para a listagem", a data aparece na Datagrid de forma desconfigurada...
E se eu carregar o formulário em modo de edição e clicar na tecla F5 para atualizar a página, a data no campo fica desconfigurada também!
Vou enviar a imagem das telas e o meu código pra que possam me ajudar!
Meus arquivos:
Criei um formulário de cadastro de consulta, onde no mesmo tem um campo do tipo TDate! Acontece que quando eu salvo e clico no botão "Voltar para a listagem", a data aparece na Datagrid de forma desconfigurada...
E se eu carregar o formulário em modo de edição e clicar na tecla F5 para atualizar a página, a data no campo fica desconfigurada também!
Vou enviar a imagem das telas e o meu código pra que possam me ajudar!
Meus arquivos:
- <?php
- class SystemAgendaForm extends TStandardForm {
- protected $form;
- function __construct() {
- parent::__construct();
- $this->form = new TQuickForm('form_SystemAgenda');
- $this->form->setFormTitle('Agenda');
- $this->form->class = 'tform';
- // defines the database
- parent::setDatabase('permission');
- // defines the active record
- parent::setActiveRecord('SystemAgenda');
- 1508 = new TEntry('id');
- $exame = new TEntry('exame');
- $paciente = new TEntry('paciente');
- $dataconsulta = new TDate('dataconsulta');
- 1508->setEditable(false);
- $dataconsulta->setMask("dd/mm/yyyy");
- // add the fields
- $this->form->addQuickField('ID', 1508, 50);
- $this->form->addQuickField('Exame' . ': ', $exame, 200);
- $this->form->addQuickField('Paciente' . ': ', $paciente, 200);
- $this->form->addQuickField('Data' . ': ', $dataconsulta, 200);
- // validations
- $exame->addValidation(_t('Exame'), new TRequiredValidator);
- $paciente->addValidation(('Paciente'), new TRequiredValidator);
- $dataconsulta->addValidation(('Data'), new TRequiredValidator);
- // add form actions
- $this->form->addQuickAction(_t('Save'), new TAction(array($this, 'onSave')), 'ico_save.png');
- $this->form->addQuickAction(_t('New'), new TAction(array($this, 'onEdit')), 'ico_new.png');
- $this->form->addQuickAction(_t('Back to the listing'), new TAction(array('SystemAgendaList', 'onReload')), 'ico_datagrid.png');
- $container = new TTable;
- $container->style = 'width: 80%';
- $container->addRow()->addCell(new TXMLBreadCrumb('menu.xml', 'SystemAgendaList'));
- $container->addRow()->addCell($this->form);
- // add the form to the page
- parent::add($container);
- }
- function onSave() {
- try {
- // open a transaction with database 'una'
- TTransaction::open('permission');
- // get the form data into an active record Alunos
- $object = $this->form->getData('SystemAgenda');
- // form validation
- $this->form->validate();
- // stores the object
- $object->dataconsulta = TDate::date2us($object->dataconsulta);
- $object->store();
- // set the data back to the form
- //$this->form->setData($object);
- TTransaction::close();
- // shows the success message
- new TMessage('info', TAdiantiCoreTranslator::translate('Record saved'));
- $this->form->clear();
- // reload the listing
- } catch (Exception $e) { // in case of exception
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- function onEdit($param) {
- try {
- if (isset($param['key'])) {
- $key = $param['key'];
- TTransaction::open('permission');
- $object = new SystemAgenda($key);
- $object->dataconsulta = TDate::date2br($object->dataconsulta);
- $object->store();
- $this->form->setData($object);
- TTransaction::close();
- } else {
- $this->form->clear();
- }
- } catch (Exception $e) {
- new TMessage('error', 'Error ' . $e->getMessage());
- TTransaction::rollback();
- }
- }
- }
- ?>
- <?php
- /**
- * SystemAgendaList Listing
- * @author <your name here>
- */
- class SystemAgendaList extends TPage {
- private $form; // registration form
- private $datagrid; // listing
- private $pageNavigation;
- private $loaded;
- /**
- * Class constructor
- * Creates the page, the form and the listing
- */
- public function __construct() {
- parent::__construct();
- // creates the form
- $this->form = new TForm('form_search_SystemAgenda');
- $this->form->class = 'tform';
- // creates a table
- $table = new TTable;
- $table->style = 'width:100%';
- $table->addRowSet(new TLabel('Agenda'), '')->class = 'tformtitle';
- // add the table inside the form
- $this->form->add($table);
- // create the form fields
- $exame = new TEntry('exame');
- $exame->setValue(TSession::getValue('SystemAgenda_name'));
- $paciente = new TEntry('paciente');
- $paciente->setValue(TSession::getValue('SystemAgenda_control'));
- // add rows for the filter fields
- $row = $table->addRowSet(new TLabel('Exame' . ': '), $exame);
- $row = $table->addRowSet(new TLabel('Paciente' . ': '), $paciente);
- // create two action buttons to the form
- $find_button = new TButton('find');
- $new_button = new TButton('new');
- // define the button actions
- $find_button->setAction(new TAction(array($this, 'onSearch')), _t('Find'));
- $find_button->setImage('ico_find.png');
- $new_button->setAction(new TAction(array('SystemAgendaForm', 'onEdit')), _t('New'));
- $new_button->setImage('ico_new.png');
- // define wich are the form fields
- $this->form->setFields(array($exame, $paciente, $find_button, $new_button));
- $container = new THBox;
- $container->add($find_button);
- $container->add($new_button);
- $row = $table->addRow();
- $row->class = 'tformaction';
- $cell = $row->addCell($container);
- $cell->colspan = 2;
- // creates a DataGrid
- $this->datagrid = new TDataGrid;
- $this->datagrid->style = 'width: 100%';
- $this->datagrid->setHeight(320);
- // creates the datagrid columns
- 1508 = new TDataGridColumn('id', 'ID', 'right');
- $exame = new TDataGridColumn('exame', 'Exame', 'left');
- $paciente = new TDataGridColumn('paciente', 'Paciente', 'left');
- $data_consulta = new TDataGridColumn('dataconsulta', 'Data Consulta', 'left', 100);
- // add the columns to the DataGrid
- $this->datagrid->addColumn(1508);
- $this->datagrid->addColumn($exame);
- $this->datagrid->addColumn($paciente);
- $this->datagrid->addColumn($data_consulta);
- // creates the datagrid column actions
- $order_id = new TAction(array($this, 'onReload'));
- $order_id->setParameter('order', 'id');
- 1508->setAction($order_id);
- $order_exame = new TAction(array($this, 'onReload'));
- $order_exame->setParameter('order', 'exame');
- $exame->setAction($order_exame);
- $order_paciente = new TAction(array($this, 'onReload'));
- $order_paciente->setParameter('order', 'paciente');
- $paciente->setAction($order_paciente);
- $order_data_consulta = new TAction(array($this, 'onReload'));
- $order_data_consulta->setParameter('order', 'dataconsulta');
- $data_consulta->setAction($order_paciente);
- // inline editing
- $exame_edit = new TDataGridAction(array($this, 'onInlineEdit'));
- $exame_edit->setField('id');
- $exame->setEditAction($exame_edit);
- $paciente_edit = new TDataGridAction(array($this, 'onInlineEdit'));
- $paciente_edit->setField('id');
- $paciente->setEditAction($paciente_edit);
- // creates two datagrid actions
- $action1 = new TDataGridAction(array('SystemAgendaForm', 'onEdit'));
- $action1->setLabel(_t('Edit'));
- $action1->setImage('ico_edit.png');
- $action1->setField('id');
- $action2 = new TDataGridAction(array($this, 'onDelete'));
- $action2->setLabel(_t('Delete'));
- $action2->setImage('ico_delete.png');
- $action2->setField('id');
- // add the actions to the datagrid
- $this->datagrid->addAction($action1);
- $this->datagrid->addAction($action2);
- // create the datagrid model
- $this->datagrid->createModel();
- // creates the page navigation
- $this->pageNavigation = new TPageNavigation;
- $this->pageNavigation->setAction(new TAction(array($this, 'onReload')));
- $this->pageNavigation->setWidth($this->datagrid->getWidth());
- // creates the page structure using a table
- $table = new TTable;
- $table->style = 'width: 80%';
- $table->addRow()->addCell(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $table->addRow()->addCell($this->form);
- $table->addRow()->addCell($this->datagrid);
- $table->addRow()->addCell($this->pageNavigation);
- // add the table inside the page
- parent::add($table);
- }
- /**
- * method onInlineEdit()
- * Inline record editing
- * @param $param Array containing:
- * key: object ID value
- * field name: object attribute to be updated
- * value: new attribute content
- */
- function onInlineEdit($param) {
- try {
- // get the parameter $key
- $field = $param['field'];
- $key = $param['key'];
- $value = $param['value'];
- // open a transaction with database 'permission'
- TTransaction::open('permission');
- // instantiates object SystemAgenda
- $object = new SystemAgenda($key);
- // deletes the object from the database
- $object->{$field} = $value;
- $object->store();
- // close the transaction
- TTransaction::close();
- // reload the listing
- $this->onReload($param);
- // shows the success message
- new TMessage('info', "Registro Atualizado com Sucesso!");
- } catch (Exception $e) { // in case of exception
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method onSearch()
- * Register the filter in the session when the user performs a search
- */
- function onSearch() {
- // get the search form data
- $data = $this->form->getData();
- TSession::setValue('SystemProgram_name_filter', NULL);
- TSession::setValue('SystemAgenda_name', '');
- TSession::setValue('SystemProgram_control_filter', NULL);
- TSession::setValue('SystemAgenda_control', '');
- TSession::setValue('AlunosListPess_filter_datanascimento', NULL);
- // check if the user has filled the form
- if ($data->exame) {
- // creates a filter using what the user has typed
- $filter = new TFilter('exame', 'like', "%{$data->exame}%");
- // stores the filter in the session
- TSession::setValue('SystemProgram_name_filter', $filter);
- TSession::setValue('SystemAgenda_name', $data->exame);
- }
- if ($data->paciente) {
- // creates a filter using what the user has typed
- $filter = new TFilter('paciente', 'like', "%{$data->paciente}%");
- // stores the filter in the session
- TSession::setValue('SystemProgram_control_filter', $filter);
- TSession::setValue('SystemAgenda_control', $data->paciente);
- }
- if (isset($data->dataconsulta) AND ( $data->dataconsulta)) {
- $filter = new TFilter('dataconsula', 'like', "%{$data->dataconsula}%"); // create the filter
- TSession::setValue('AlunosListPess_filter_dt_nascimento', $filter); // stores the filter in the session
- }
- if (isset($data->dataconsulta) AND ( $data->dataconsulta)) {
- $filter = new TFilter('dataconsulta', 'like', "%{$data->dataconsulta}%"); // create the filter
- TSession::setValue('AlunosListPess_filter_datanascimento', $filter); // stores the filter in the session
- }
- // fill the form with data again
- $this->form->setData($data);
- $param = array();
- $param['offset'] = 0;
- $param['first_page'] = 1;
- $this->onReload($param);
- }
- /**
- * method onReload()
- * Load the datagrid with the database objects
- */
- function onReload($param = NULL) {
- try {
- // open a transaction with database 'permission'
- TTransaction::open('permission');
- // creates a repository for SystemProgram
- $repository = new TRepository('SystemAgenda');
- $limit = 5;
- // creates a criteria
- $criteria = new TCriteria;
- if (!isset($param['order'])) {
- $param['order'] = 'id';
- $param['direction'] = 'asc';
- }
- $criteria->setProperties($param); // order, offset
- $criteria->setProperty('limit', $limit);
- if (TSession::getValue('SystemProgram_name_filter')) {
- // add the filter stored in the session to the criteria
- $criteria->add(TSession::getValue('SystemProgram_name_filter'));
- }
- if (TSession::getValue('SystemProgram_control_filter')) {
- // add the filter stored in the session to the criteria
- $criteria->add(TSession::getValue('SystemProgram_control_filter'));
- }
- if (TSession::getValue('AlunosListPess_filter_dataconsulta')) {
- $criteria->add(TSession::getValue('AlunosListPess_filter_datanascimento')); // add the session filter
- }
- // load the objects according to criteria
- $objects = $repository->load($criteria);
- $this->datagrid->clear();
- if ($objects) {
- // iterate the collection of active records
- foreach ($objects as $object) {
- // add the object inside the datagrid
- $object->dataconsulta = TDate::date2br($object->dataconsulta);
- $this->datagrid->addItem($object);
- }
- }
- // reset the criteria for record count
- $criteria->resetProperties();
- $count = $repository->count($criteria);
- $this->pageNavigation->setCount($count); // count of records
- $this->pageNavigation->setProperties($param); // order, page
- $this->pageNavigation->setLimit($limit); // limit
- // close the transaction
- TTransaction::close();
- $this->loaded = true;
- } catch (Exception $e) { // in case of exception
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method onDelete()
- * executed whenever the user clicks at the delete button
- * Ask if the user really wants to delete the record
- */
- function onDelete($param) {
- // define the delete action
- $action = new TAction(array($this, 'Delete'));
- $action->setParameters($param); // pass the key parameter ahead
- // shows a dialog to the user
- new TQuestion(TAdiantiCoreTranslator::translate('Do you really want to delete ?'), $action);
- }
- /**
- * method Delete()
- * Delete a record
- */
- function Delete($param) {
- try {
- // get the parameter $key
- $key = $param['key'];
- // open a transaction with database 'permission'
- TTransaction::open('permission');
- // instantiates object SystemProgram
- $object = new SystemAgenda($key);
- // deletes the object from the database
- $object->delete();
- // close the transaction
- TTransaction::close();
- // reload the listing
- $this->onReload($param);
- // shows the success message
- new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted'));
- } catch (Exception $e) { // in case of exception
- // shows the exception error message
- new TMessage('error', '<b>Error</b> ' . $e->getMessage());
- // undo all pending operations
- TTransaction::rollback();
- }
- }
- /**
- * method show()
- * Shows the page
- */
- function show() {
- // check if the datagrid is already loaded
- if (!$this->loaded) {
- $this->onReload(func_get_arg(0));
- }
- parent::show();
- }
- }
- ?>
Existe um forum anterior que trata de um problema parecido.
www.adianti.com.br/forum/pt/view_1501?formatacao-campo-tdate-para-fo
Boa sorte.