Page 1 of 1

Managing NULL and worse

Posted: Sun Jul 09, 2006 6:45 am
by Ollie Saunders
I've got this code:

Code: Select all

$in = $this->getInput();
$clean = array('txtTitle'           => $this->_escape($in->getRaw('txtTitle'))           or '',
               'selClient'          => $in->getInt('selClient')                          or 0,
               'selSubclient'       => $in->getInt('selSubclient')                       or 0,
               'fsVideo'            => $in->getInt('fsVideo')                            or true,
               'txtUrlStream'       => $this->_escape($in->getRaw('txtUrlStream'))       or '',
               'txtUrlDownload'     => $this->_escape($in->getRaw('txtUrlDownload'))     or '',
               'txtWidth'           => $this->getInt('txtWidth')                         or 0,
               'txtHeight'          => $this->getInt('txtHeight')                        or 0,
               'txtMsgFeedback'     => $this->_escape($in->getRaw('txtMsgFeedback'))     or '',
               'txtMsgStream'       => $this->_escape($in->getRaw('txtMsgStream'))       or '',
               'txtMsgDownload'     => $this->_escape($in->getRaw('txtMsgDownload'))     or '',
               'fsQuestionnaire'    => $this->getInt('fsQuestionnaire')                  or true,
               'txtMsgOnStart'      => $this->_escape($in->getRaw('txtMsgOnStart'))      or '',
               'txtMsgOnceFinished' => $this->_escape($in->getRaw('txtMsgOnceFinished')) or '');
$clean = (object)$clean;
It's getting posted values from a form or if they aren't present assigning them with a default.

However there are a couple of problems:
  • The first line ($in = $this->getInput();) can fail, as in return NULL, if the form hasn't been posted in which case $in->getRaw('txtTitle') generates an E_FATAL because $in isn't an object.
  • I can't make the assignment of $clean conditional based on $in because then my defaults wouldn't be assigned.
So basically I'm looking for a nice solution for managing default values where form isn't posted or any single field isn't posted.

Posted: Sun Jul 09, 2006 7:13 am
by jayshields
Seems overkill to me how you're managing the form input.

Why not just do this:

Code: Select all

if the form has been submitted
    do whatever with the data
end if
which is usually for me

Code: Select all

if(isset($_POST['submit'])) {
    //escape stuff
}