XoopsFormDateTime empty field?
#1
Just can't stay away
Just can't stay away


See User information
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.

Posted on: 2010/1/4 18:12
 Top  Twitter  Facebook    Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 


Re: XoopsFormDateTime empty field?
#2
Not too shy to talk
Not too shy to talk


See User information
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="$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'.

Posted on: 2010/1/7 11:35
 Top  Twitter  Facebook    Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 


Re: XoopsFormDateTime empty field?
#3
Not too shy to talk
Not too shy to talk


See User information
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.

Posted on: 2012/1/18 10:59
 Top  Twitter  Facebook    Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 


Re: XoopsFormDateTime empty field?
#4
Not too shy to talk
Not too shy to talk


See User information
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);



Posted on: 2012/1/19 12:11
 Top  Twitter  Facebook    Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 


Re: XoopsFormDateTime empty field?
#5
Moderator
Moderator


See User information
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!



Posted on: 2013/8/24 22:46
Support XOOPS => DONATE
Use 2.5.11 | Docs | Modules | Bugs
 Top  Twitter  Facebook    Linkedin  Del.icio.us  Digg  Reddit  Mr. Wong 







You can view topic.
You cannot start a new topic.
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You can vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[Advanced Search]


Login
Username:

Password:

Remember me



Lost Password?

Register now!
Search
Recent Posts
Who's Online
129 user(s) are online (95 user(s) are browsing Support Forums)

Members: 0
Guests: 129

more...
Donat-O-Meter
Make donations with PayPal!
Stats
Goal: AU$15.00
Due Date: Jul 31
Gross Amount: AU$0.00
Net Balance: AU$0.00
Left to go: AU$15.00
Latest GitHub Commits
Recent forum posts
Recent Comments
About us
Learn
Use
Develop GitHub
Contribute
Connect
Terms of Use | Privacy Policy | Hosted by Arvixe Hosting | RSS 2.0 Button