Nella forms, datetime vrací NULL
- SvvimX
- Člen | 65
Ahoj,
chtěl jsem využít Nella forms, které krásně implementují metody jako addDateTime, dokonce snad cosi s Tagy, ale jednoduchý form:
class BaseForm extends \Nella\Forms\Form {
$this ->addDateTime( 'start', 'Start date' )
->setAttribute( 'class', 'inputbox');
;
}
mi vrací neustále NULL
start => NULL
Také by se mi hodil nějaký menší návod, jak pracovat s políčkem pro tagy a co to umí.
- SvvimX
- Člen | 65
Ok, ale na tom asi není nic zvláštního, mám takle x formulářů a všechna data se mi odešlou, jen addDateTime z Nelly nějak nechce, přitom se tak hezky vykreslí..
<?php
namespace CCM\Form;
class BaseForm extends \Nella\Forms\Form {
public function __construct( \Nette\ComponentModel\IContainer $parent = NULL, $name = NULL ) {
parent::__construct( $parent, $name );
$this -> setTranslator ( \Nette\Environment::getService("translator") );
}
}
<?php
/**
*
* @author Ondra
*/
namespace CCM\Form;
use Nella\Forms\Form;
class ProjectForm extends BaseForm {
public function __construct( array $workers, array $companies, \Nette\ComponentModel\IContainer $parent = NULL, $name = NULL ) {
parent::__construct( $parent, $name );
$this -> setMethod ( "POST" );
$this -> addText( 'title', 'Title' )
-> setRequired ( 'Please enter %label' )
->setAttribute( 'class', 'inputbox');
;
$this -> addTextArea( 'description', 'Description' )
-> setRequired ( 'Please enter %label' )
->setAttribute( 'class', 'inputbox');
;
$this ->addMultiSelect( 'customers', 'Customers', $companies )
-> setRequired ( 'Please enter %label' )
->setAttribute( 'class', 'inputbox');
;
unset ( $companies[1] ); // mother company cannot be coworker (customer yep)
$this ->addMultiSelect( 'coworkers', 'Coworkers', $companies )
->setAttribute( 'class', 'inputbox');
;
$this ->addDateTime( 'start', 'Start date' )
//-> setRequired ( 'Please enter %label' )
->setAttribute( 'class', 'inputbox');
;
$this ->addDateTime( 'end', 'End date' )
->setAttribute( 'class', 'inputbox');
;
$this ->addNumber( 'price', 'Price', 100, 0 )
->setAttribute( 'class', 'inputbox');
;
$this ->addNumber( 'time', 'Expected time in hours', 1, 0 )
->setAttribute( 'class', 'inputbox');
;
$this ->addCheckbox( 'done', 'Project done' )
->setAttribute( 'class', 'inputbox');
;
$this ->addCheckbox( 'private', 'Private' )
->setAttribute( 'class', 'inputbox');
;
$this -> addSelect( 'garant', 'Garant', $workers )
->setAttribute( 'class', 'inputbox')
;
$this -> addSubmit ( 'submit', 'Save' )
->setAttribute( 'class', 'btn btn-primary btn-large');
}
}
<?php
/**
* Project presenter.
*
* @author Ondrej Sejvl
* @package Project contracts management
* @version 1.0
*/
class ProjectPresenter extends DefaultPresenter
{
/**
* Manager
*
* @var \CCM\Manager\ProjectManager
*/
protected $manager;
/** @persistent int */
public $id;
/**
* Factory for entity form
*
* @param string $name
* @return \CCM\Form\ProjectForm
* @throws \Nette\Application\BadRequestException
*/
protected function createComponentProjectForm ( $name = NULL )
{
$workersEnt = $this -> getService("workerManager") -> getAll ( array ( "surname" => "ASC" ) );
$companiesEnt = $this -> getService("companyManager") -> getAll ( array ( "firm" => "ASC" ) );
$workers = array ();
foreach ( $workersEnt as $ent ) $workers [ $ent -> id ] = $ent -> name . " " . $ent -> surname;
$companies = array ();
foreach ( $companiesEnt as $ent ) $companies [ $ent -> id ] = $ent -> firm;
$form = new \CCM\Form\ProjectForm ( $workers, $companies );
$form->onSuccess[] = $this -> processProjectFormSubmitted;
if ( $this -> id ) {
$entity = $this -> manager -> getOneBy ( array ( "id" => $this -> id ) );
if ( ! $entity )
throw new \Nette\Application\BadRequestException;
$defValue = $this -> manager ->toArray ( $entity );
$defValue [ 'garant' ] = $entity -> garant -> id ;
$customers = array ();
foreach ( $entity -> customers as $customer )
$customers [ ] = $customer -> id;
$coworkers = array ();
foreach ( $entity -> coworkers as $coworker )
$coworkers [ ] = $coworker -> id;
$defValue [ 'customers' ] = $customers;
$defValue [ 'coworkers' ] = $coworkers;
$form ->setDefaults( $defValue );
}
return $form;
}
/**
* Process Form, create or update entity depends on persistent id
*
* @param \CCM\Form\ProjectForm $form
* @return void
*/
public function processProjectFormSubmitted ( \CCM\Form\ProjectForm $form )
{
$values = (array)$form->getValues();
**dump ( $values );**
**exit; **
try {
if ( $this -> id )
$this -> manager ->update ( $this -> id, $values ) ;
else
$this -> manager ->create ( $values ) ;
} catch ( \CCM\Exception\DatabaseException $e ) {
error_log( $e -> getMessage() );
$form -> addError ( "Project can not be " . ( $this -> id ? "updated" : "created" ) );
return;
}
$this -> flashMessage ( "Project was sucessfully " . ( $this -> id ? "updated" : "created" ), "success" );
$this -> id = NULL;
$this->redirect('Project:');
}
- SvvimX
- Člen | 65
Přišel jsem na důvod, není to v mé třídě, je to tím, že Nella forms využívají html5 input type=„datetime“ který vrací datum ve formátu date velké písmeno „T“ time a „Z“ tedy např: 1990–12–31T23:59:60Z
a v nella forms baseDateTime je
public function getValue()
{
$value = parent::getValue();
$value = DateTime::createFromFormat(static::$format, $value);
$err = DateTime::getLastErrors();
if ($err['error_count']) {
$value = FALSE;
}
return $value ?: NULL;
}
následující datum naplní proměnnou $err
"2013-05-29T12:00Z" (17)
array(4) {
warning_count => 0
warnings => array(0)
error_count => 2
errors => array(2) {
10 => "Unexpected data found." (22)
16 => "Trailing data" (13)
}
}
Chápu to tedy jako bug v Nella Forms?
- SvvimX
- Člen | 65
Nechal jsem tam default, v domění že vše samo poběží :-)
Navíc dateTime nemá funkci setFormat, očekává se tedy, že si přepíši formát přímo ve skriptu?
/** @var string */
public static $format = 'Y-n-j H:i';
Nebo ve formuláři jaksi:
$this ->addDateTime( 'end', 'End date' )
->setAttribute( 'class', 'inputbox input-xxlarge');
-> format = "Y-n-jTH:iZ"; // navíc, kdo ví, jak ten formát má vypadat...?
- Elfoslav
- Člen | 15
Neprepisoval by som cudzí kód. Vytvor si vlastnú komponentu, ktorá bude dediť z Nella\Forms\Controls\Date a tam prepíš $format.
namespace CCM\Controls;
class Date extends \Nella\Forms\Controls\Date {
/** @var string */
public static $format = 'd.m.Y';
}
Túto komponentu použi v tvojom formulári:
namespace CCM\Form;
use CCM\Controls\Date;
class BaseForm extends \Nella\Forms\Form {
public function __construct( \Nette\ComponentModel\IContainer $parent = NULL, $name = NULL ) {
parent::__construct( $parent, $name );
$this -> setTranslator ( \Nette\Environment::getService("translator") );
}
public function addDate($name, $label = NULL, $cols = NULL) {
return $this[$name] = new Date($label, $cols, NULL);
}
}
To isté pre DateTime…