1
frankblack
XoopsFormDateTime empty field?
  • 2010/1/4 18:12

  • frankblack

  • Just can't stay away

  • Posts: 830

  • Since: 2005/6/13


When I use XoopsFormDateTime the script always enters a value into the text field, which is very nice, but not always wanted.

I just want to have an empty input field, so that the current date/time will be not inserted when not set on purpose.

2
bumciach
Re: XoopsFormDateTime empty field?
  • 2010/1/7 11:35

  • bumciach

  • Not too shy to talk

  • Posts: 153

  • Since: 2007/6/25


To do this I have made this function.
function pf_dateForm($c$n$s$v$alt='calendar')
{
  global 
$xoopsModule;
      
$jstime formatTimestampstrtotime(date('Y-m-d')), 'F j Y, H:i:s' );
    include_once 
XOOPS_ROOT_PATH '/include/calendarjs.php';
    
$tray = new XoopsFormElementTray($c,'');
       
$frm = new XoopsFormText(''$n$s10$v);
    
$tray->addElement($frm);
       
$img="<img alt='" $alt "' src='" XOOPS_URL "/modules/" $xoopsModule->getVar('dirname') . "/images/date.png' style='cursor: pointer;' onclick='return showCalendar("" . $n . "");' />";
       
$btn = new XoopsFormLabel('',$img);
  
$tray->addElement($btn);
  
  return 
$tray;
}

and then into script:
$form -> addElementpf_dateForm(...) );


However, I needed only date form without time, but I think it would be easy to add.
In my solution I replace the standard button [...] by image located in '/modules/mymodule/images/date.png'.

3
bumciach
Re: XoopsFormDateTime empty field?
  • 2012/1/18 10:59

  • bumciach

  • Not too shy to talk

  • Posts: 153

  • Since: 2007/6/25


NOTICE: This my function from above doesn't work since Xoops 2.5.4. There were changes in xoops calendar and the file: /include/calendarjs.php even not exist in the latest Xoops.

4
bumciach
Re: XoopsFormDateTime empty field?
  • 2012/1/19 12:11

  • bumciach

  • Not too shy to talk

  • Posts: 153

  • Since: 2007/6/25


Better solution - use OOP: extend original class XoopsFormTextDateSelect and add parameter $autofill

class pfFormTextDateSelect extends XoopsFormTextDateSelect
{

    
/**
     * Constructor
     *
     * @param string      $caption Caption
     * @param string      $name "name" attribute
     * @param int         $size Size
     * @param int,string  $value Initial date (timestamp format) or English textual datetime description which can be converted to timestamp (see: strtotime PHP function)
     * @param bool         $autofill If $value is empty or not recognized textual datetime the form field filled current date ($autofill = true) or leave empty    
     */
    
function pfFormTextDateSelect($caption$name$size 15$value 0$autofill true)
    {
        
// previous to PHP 5.1.0 you would compare with -1, instead of false
        
if ( strtotime($value) !== false OR strtotime($value) != -) {
            
$value strtotime($value);
        }        
        if ( 
$autofill === true ) {
          
$value = !is_numeric($value) ? time() : intval($value);
          
$value = ($value == 0) ? time() : $value;
        }
        else {
          
$value = !is_numeric($value) ? '' intval($value);
          
$value = ($value == 0) ? '' $value;
        }
        
$this->XoopsFormText($caption$name$size25$value);
    }

}


Moreover this class accepts English textual datetime description and convert it into a timestamp (see http://php.net/manual/en/function.strtotime.php).
Example, this displays a later date by one day:
new pfFormTextDateSelect('''test'10'+1 day'false);

Empty field:
new pfFormTextDateSelect('''test'10''false);



5
Mamba
Re: XoopsFormDateTime empty field?
  • 2013/8/24 22:46

  • Mamba

  • Moderator

  • Posts: 11245

  • Since: 2004/4/23


Yes, that was a very good solution!

And it shows how searching on the XOOPS Website can help people to avoid reinvent ing the wheel!

A client of mine wanted to have a Calendar field that would show as a default "YYYY/MM/DD", and to only change after the user clicks on the "calendar button". If not, it would save to the database the default value "YYYY/MM/DD".

I was curious if somebody was faced with similar issue, and I've found your post, and with a small modification, it was perfect!

So thank you for sharing! It was helpful and it saved me some time!

And it demonstrates nicely the power of OOP!


Support XOOPS => DONATE
Use 2.5.10 | Docs | Modules | Bugs

Login

Username:
Password:

Lost Password? Register now!

Who's Online

67 user(s) are online (36 user(s) are browsing Support Forums)


Members: 0


Guests: 67


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: May 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits