Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to change the form data display style in the VIEWer?

For example a datatime table field, it's a Unix timestamp:

  `start_time` int(16) unsigned DEFAULT NULL,

And in the Form class:

class MyForm extends Form
{
    public function initialize($entity = null)
    {
        $start_time = new Text('start_time', array(
            'class' => 'form-control'
        ));
        $this->add($start_time);
    }
}

in the Controller:

$robot = Robots::findFirstById($id);
$this->view->form = new MyForm($robot);

in the Viewer:

{{ form.render('start_time') }}

In this way, the start_time shows a unix timestamp, it's not readable by human. How to change it to a usual datatime format in the viewer's form?



8.1k

Thank you very much!

built-in functions can work for this example. Can I use my own functions? and how to?

You can use built in functions in volt: https://docs.phalconphp.com/en/latest/reference/volt.html#functions



8.1k
edited Dec '15

Hi, Thanks for your answers!

For a single variable I can use built-in functions like this {{ date(start_time) }}

But it's a form value: {{ form.render("start_time") }}, I mean , How to change the form value's formate?

That's , by default {{ form.render("start_time") }} will produce <input type="text" name="start_time" value="1234567890">, But I expect <input type="text" name="start_time" value="2015-12-14 10:58:00">, focus the value formate, it's readable by human

You can use built in functions in volt: https://docs.phalconphp.com/en/latest/reference/volt.html#functions

edited Dec '15

Here is sample code from app of mine:

        $field = new Text('first_name', array(
            'aria-required' => 'true',
            'placeholder' => $this->translations->public->registrationForm->firstName,
            'title' => $this->translations->public->registrationForm->firstName,
        )); 
        $field->setLabel($this->translations->public->registrationForm->firstName); 
        $field->addValidators(array(
            new PresenceOf(array(
                'message' => $this->translations->jsValidation->jsValidation->required
            ))
        )); 
        $field->setDefault($this->session->user->firstName);
        $this->add($field);

The line you are looking for is:

$field->setDefault($this->session->user->firstName);
// In your case, something like 
$field->setDefault(date('Y-m-d H:i:s', $stamp));


8.1k
edited Dec '15

Thanks!

I change my code to this, add a setDefault function:

class MyForm extends Form
{
    public function initialize($entity = null)
    {
        $start_time = new Text('start_time', array(
            'class' => 'form-control'
        ));
        if (isset($entity))
            $start_time->setDefault(date('Y-m-d H:i:s', $entity->start_time));
        else
            $start_time->setDefault(date('Y-m-d H:00:00')); //time of now
        $this->add($start_time);
    }
}

If $entity is null, it can display time properyly, but if $entity is not null, it still display a long integer, not a datetime format! why?

Here is sample code from app of mine:

```php $field = new Text('first_name', array( 'aria-required' => 'true', 'placeholder' => $this->translations->public->registrationForm->firstName, 'title' => $this->translations->public->registrationForm->firstName, )); $field->setLabel($this->translations->public->registrationForm->firstName); $field->addValidators(array( new PresenceOf(array( 'message' => $this->translations->jsValidation->jsValidation->required )) )); $field->setDefault($this->session->user->firstName); $this->add($field); ```

The line you are looking for is:

```php $field->setDefault($this->session->user->firstName); // In your case, something like $field->setDefault(date('Y-m-d H:i:s', $stamp)); ```

Looking at your if in the code:

// This will always be TRUE since you give default value to $entity in tour function definition.
if (isset($entity))

// Perhaps modify like this
if (isset($entity->start_time))
// Or even 
if (!is_null($entity))


8.1k

Oh~Thanks! that's an error!

But it seems to be not the rea causation! No matter how I change the code, it still remains the orginal value that stores in the database. For example, if the value of start_time stores in the database is 12345678, and the result of the code below is 12345678

if (isset($entity->start_time))
    $start_time->setDefault(date('Y-m-d H:i:s', $entity->start_time));  
else
    $start_time->setDefault(date('Y-m-d H:00:00'));
$this->add($start_time);

and I change to $start_time->setDefault($entity->start_time / 10000);, the result is still 12345678!

Looking at your if in the code:

```php // This will always be TRUE since you give default value to $entity in tour function definition. if (isset($entity))

// Perhaps modify like this if (isset($entity->starttime)) // Or even if (!isnull($entity)) ```



62.7k
Accepted
answer

Hey, ive tested in one of my forms, here is what will do the job for you:

    public function initialize($entity = null, $options = null)
    {     
        // by default my title was "test test", but this way i overwrote it and it was succesfuly shown in the form
        $entity->title = 'asd';

        // in your case
        $entity->start_time = date('.......');


8.1k
edited Dec '15

That works! Thank you very much!

Is it a bug of Phalcon?

BTW, I'm looking for a solution of multi-lingual in the Controllers and Models. By the official manual, I only can achieve the multi-lingual in Viewers.

I notice that your code is multi-lingual in the Forms! That's amazing! and it's what I want to achieve.

Could you please show me some code of how you realized the multi-lingual system?

$field = new Text('first_name', array(
            'aria-required' => 'true',
            'placeholder' => $this->translations->public->registrationForm->firstName,
            'title' => $this->translations->public->registrationForm->firstName,
));

Hey, ive tested in one of my forms, here is what will do the job for you:

```php public function initialize($entity = null, $options = null) {
// by default my title was "test test", but this way i overwrote it and it was succesfuly shown in the form $entity->title = 'asd';

   // in your case
 $entity->start_time = date('.......');

```

edited Dec '15

Hey, it is not a phalcon bug. It was my mistake to advice you in first place to use setDefault, because that method sets default value in case $entity or $_POST does not have value for the input. My bad sorry :)

My multilanguage setup:

I have a base controller, which my other controllers extend. In this controller i have a method which loads translations from cache file and sets them as a shared resource. Here are fragments from that Controller:

class BaseController extends \Phalcon\DI\Injectable
{ 
    function __construct() 
    {   
        // Set language
        $this->setLanguage();
    }

    // Set language
    private function setLanguage() 
    {     
        // Interface language
        $file = 'admin_' . $this->session->cms->settings->interfaceLanguage;
        $this->loadLanguageFile('admin', $file);        

        // public translations based on interface Language
        $file = 'jsValidation_' . $this->session->cms->settings->interfaceLanguage;
        $this->loadLanguageFile('jsValidation', $file);            
    } 

    // Most important method
    public function loadLanguageFile($key, $file) 
    {  
        $obj = new \Models\Translations();
        // This gets the cached file, since i store my translations in Database and i dont want to query them everytime. You can use static phalcon translation files also.
        $translations = $obj->getCache($file); 

        // This section is just for me so i can have multiple "sections" in my translations. E.g.: public, admin, jsValidation e.t.c.
        if ($this->di->has('translations')) {
            $temp = $this->translations;
        } else {
            $temp = new \stdClass();            
        }
        $temp->{$key} = $translations; 

        // This is the most important line, allows you to access trnaslations from everywhere
        $this->di->setShared('translations', $temp);  
    } 
}

p.s. If your initial problem was solved you can mark the working answer as a solution, so others can use it :)



8.1k

Thanks! Thanks for your answers, thanks for your multi-lingual solutions...I'm trying to imitate your code.

Hey, it is not a phalcon bug. It was my mistake to advice you in first place to use setDefault, because that method sets default value in case $entity or $_POST does not have value for the input. My bad sorry :)

My multilanguage setup:

I have a base controller, which my other controllers extend. In this controller i have a method which loads translations from cache file and sets them as a shared resource. Here are fragments from that Controller:

```php

class BaseController extends \Phalcon\DI\Injectable { function __construct() {
// Set language $this->setLanguage(); }

// Set language private function setLanguage() {
// Interface language $file = 'admin_' . $this->session->cms->settings->interfaceLanguage; $this->loadLanguageFile('admin', $file);

   // public translations based on interface Language
 $file = 'jsValidation_' . $this->session->cms->settings->interfaceLanguage;
 $this->loadLanguageFile('jsValidation', $file);

}

// Most important method public function loadLanguageFile($key, $file) {
$obj = new \Models\Translations(); // This gets the cached file, since i store my translations in Database and i dont want to query them everytime. You can use static phalcon translation files also. $translations = $obj->getCache($file);

   // This section is just for me so i can have multiple "sections" in my translations. E.g.: public, admin, jsValidation e.t.c.
 if ($this->di->has('translations')) {
     $temp = $this->translations;
 } else {
     $temp = new \stdClass();            
 }
 $temp->{$key} = $translations; 

 // This is the most important line, allows you to access trnaslations from everywhere
 $this->di->setShared('translations', $temp);

} }

```

p.s. If your initial problem was solved you can mark the working answer as a solution, so others can use it :)