RF
Erro Warning: number_format() expects parameter 1 to be float, s
Boa tarde,
Alguem sabe me dizer onde estou errando?
Eu adptei o código de um exemplo do tutor mas não funcionou, segue código e tela.
Alguem sabe me dizer onde estou errando?
Eu adptei o código de um exemplo do tutor mas não funcionou, segue código e tela.
- <?php
- /**
- * ListaCadatroProdutos Form List
- * @author <your name here>
- */
- class ListaCadatroProdutos extends TPage
- {
- protected $form; // form
- protected $datagrid; // datagrid
- protected $pageNavigation;
- protected $loaded;
- /**
- * Form constructor
- * @param $param Request
- */
- public function __construct( $param )
- {
- parent::__construct();
- // creates the form
- $this->form = new TQuickForm('form_Produto');
- $this->form->class = 'tform'; // change CSS class
- $this->form->style = 'display: table;width:100%'; // change style
- $this->form->setFormTitle('Produto');
- // create the form fields
- $id = new TEntry('id');
- $tipo = new TCombo('tipo');
- $bairro = new TEntry('bairro');
- $unidis = new TEntry('unidis');
- $vlmaior = new TEntry('vlmaior');
- $vlmenor = new TEntry('vlmenor');
- $empreendimento = new TEntry('empreendimento');
- // definição dos formatos dos campos
- $vlmaior -> setNumericMask(2,',', '.', true);
- $vlmenor -> setNumericMask(2,',', '.', true);
- $empreendimento ->style = "text-transform: uppercase";
- $bairro ->style = "text-transform: uppercase";
- $unidis ->style = "text-transform: uppercase";
- $id ->setEditable(FALSE);
- //Itens do estagio
- $itemEstagio= array();
- $itemEstagio['LANÇAMENTO'] = 'LANÇAMENTO';
- $itemEstagio['EM OBRAS'] = 'EM OBRAS';
- $itemEstagio['PRONTO PARA MORAR'] = 'PRONTO PARA MORAR';
- $tipo->setValue('PRONTO PARA MORAR');
- $tipo->addItems($itemEstagio);
- // add the fields
- $this->form->addQuickField('Código', $id, 200 );
- $this->form->addQuickField('Tipo', $tipo, 200 );
- $this->form->addQuickField('Bairro', $bairro, 200 );
- $this->form->addQuickField('Unidade disponível', $unidis, 450 );
- $this->form->addQuickField('Maior valor', $vlmaior, 200 );
- $this->form->addQuickField('Menor valor', $vlmenor, 200 );
- $this->form->addQuickField('Produto', $empreendimento, 200 );
- /** samples
- $this->form->addQuickFields('Date', array($date1, new TLabel('to'), $date2)); // side by side fields
- $fieldX->addValidation( 'Field X', new TRequiredValidator ); // add validation
- $fieldX->setSize( 100, 40 ); // set size
- **/
- // 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');
- // creates a Datagrid
- $this->datagrid = new TDataGrid;
- ##LIST_DECORATOR##
- $this->datagrid->style = 'width: 100%';
- $this->datagrid->setHeight(320);
- // $this->datagrid->datatable = 'true';
- // $this->datagrid->enablePopover('Popover', 'Hi <b> {name} </b>');
- // creates the datagrid columns
- $column_id = new TDataGridColumn('id', 'Código', 'left');
- $column_tipo = new TDataGridColumn('tipo', 'Tipo', 'left');
- $column_bairro = new TDataGridColumn('bairro', 'Bairro', 'left');
- $column_unidis = new TDataGridColumn('unidis', 'Unidade disponível', 'left');
- $column_vlmaior = new TDataGridColumn('vlmaior', 'Maior valor', 'left');
- $column_vlmenor = new TDataGridColumn('vlmenor', 'Menor valor', 'left');
- $column_empreendimento = new TDataGridColumn('empreendimento', 'Produto', 'left');
- $column_vlmaior->setTransformer(array($this, 'formatValor'));
- $column_vlmenor->setTransformer(array($this, 'formatValor'));
- // add the columns to the DataGrid
- $this->datagrid->addColumn($column_id);
- $this->datagrid->addColumn($column_tipo);
- $this->datagrid->addColumn($column_bairro);
- $this->datagrid->addColumn($column_unidis);
- $this->datagrid->addColumn($column_vlmaior);
- $this->datagrid->addColumn($column_vlmenor);
- $this->datagrid->addColumn($column_empreendimento);
- // creates two datagrid actions
- $action1 = new TDataGridAction(array($this, 'onEdit'));
- $action1->setUseButton(TRUE);
- $action1->setButtonClass('btn btn-default');
- //$action1->setLabel(_t('Edit'));
- $action1->setImage('fa:pencil-square-o blue fa-lg');
- $action1->setField('id');
- /*
- $action2 = new TDataGridAction(array($this, 'onDelete'));
- $action2->setUseButton(TRUE);
- $action2->setButtonClass('btn btn-default');
- $action2->setLabel(_t('Delete'));
- $action2->setImage('fa:trash-o red fa-lg');
- $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());
- /* vertical box container
- $container = new TVBox;
- $container->style = 'width: 90%';
- // $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- $container->add($this->datagrid);
- $container->add($this->pageNavigation);
- parent::add($container);
- */
- // Sub Menu Bonito
- $container = new TVBox;
- $container->style = 'width: 90%';
- $container->add(new TXMLBreadCrumb('menu.xml', __CLASS__));
- $container->add($this->form);
- $container->add(TPanelGroup::pack('', $this->datagrid));
- $container->add($this->pageNavigation);
- parent::add($container);
- }
- /**
- Classes ---------------------------------------------------
- */
- public function formatValor($param)
- {
- $param = number_format($param, 2, ',', '.');
- return $param;
- }
- /**
- * Load the datagrid with data
- */
- public function onReload($param = NULL)
- {
- try
- {
- // open a transaction with database 'sample'
- TTransaction::open('sample');
- // creates a repository for Produto
- $repository = new TRepository('Produto');
- $limit = 10;
- // creates a criteria
- $criteria = new TCriteria;
- // default order
- if (empty($param['order']))
- {
- $param['order'] = 'id';
- $param['direction'] = 'asc';
- }
- $criteria->setProperties($param); // order, offset
- $criteria->setProperty('limit', $limit);
- if (TSession::getValue('Produto_filter'))
- {
- // add the filter stored in the session to the criteria
- $criteria->add(TSession::getValue('Produto_filter'));
- }
- // load the objects according to criteria
- $objects = $repository->load($criteria, FALSE);
- $this->datagrid->clear();
- if ($objects)
- {
- // iterate the collection of active records
- foreach ($objects as $object)
- {
- // add the object inside the datagrid
- $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();
- }
- }
- /**
- * Ask before deletion
- */
- public 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);
- }
- /**
- * Delete a record
- */
- public function Delete($param)
- {
- try
- {
- $key=$param['key']; // get the parameter $key
- TTransaction::open('sample'); // open a transaction with database
- $object = new Produto($key, FALSE); // instantiates the Active Record
- $object->delete(); // deletes the object from the database
- TTransaction::close(); // close the transaction
- $this->onReload( $param ); // reload the listing
- new TMessage('info', TAdiantiCoreTranslator::translate('Record deleted')); // success message
- }
- catch (Exception $e) // in case of exception
- {
- new TMessage('error', '<b>Error</b> ' . $e->getMessage()); // shows the exception error message
- TTransaction::rollback(); // undo all pending operations
- }
- }
- /**
- * Save form data
- * @param $param Request
- */
- public function onSave( $param )
- {
- try
- {
- TTransaction::open('sample'); // 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 Produto; // create an empty object
- $data = $this->form->getData(); // get form data as array
- $data ->bairro = strtoupper($data->bairro);
- $data ->empreendimento = strtoupper($data->empreendimento);
- $data ->unidis = strtoupper($data->unidis);
- $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')); // success message
- $this->onReload(); // reload the listing
- }
- 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);
- }
- /**
- * 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('sample'); // open a transaction
- $object = new Produto($key); // instantiates the Active Record
- $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
- }
- }
- /**
- * method show()
- * Shows the page
- */
- public function show()
- {
- // check if the datagrid is already loaded
- if (!$this->loaded AND (!isset($_GET['method']) OR $_GET['method'] !== 'onReload') )
- {
- $this->onReload( func_get_arg(0) );
- }
- parent::show();
- }
- }
Em alguma situação o php está interpretando a varíavel $param como string quando deveria ser float:
Algum dos valores dessas colunas está vindo vazio do banco?
Você está gravando os dados no banco com vírgula e ponto(413.400,00)?
Boa tarde...
Na verdade eu só queria ver na DataGrid o valor dos campos Maior Valor e Menor Valor em formato númerico, se não for possível tudo bem eu vou sobreviver.
Pra não aparecer o erro é só fazer o cast da variável para float:
Muito obrigado, era o que eu precisava. Tenho outras dúvidas mas depois eu posto, outro dia eu pergunto mais...