SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast irkengir's Avatar
    Join Date
    Mar 2006
    Location
    UK
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Managing default values in form submissions

    I've got this code:
    Code:
    $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.

  2. #2
    is_empty(2); foofoonet's Avatar
    Join Date
    Mar 2006
    Posts
    1,000
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do some conditional testing to see if the object is set, is_object() http://fr.php.net/is_object might be one, or if not a simple
    if($in = $this->getInput()){
    //do stuff
    } might do it.

    or else, as you say it return NULL;

    if($this->getInput()==NULL){stop script}

    http://www.deformedweb.co.uk/php_variable_tests.php Now beware, testing for null is not my best skill, take a look at that table in the link!

    This is one of those cases where UnitTesting would help keep your sanity - and save you Sooooo much time.
    http://www.lastcraft.com/simple_test.php
    http://www.marcclifton.com/tabid/98/Default.aspx
    Upgrading to Mysql 5? Auto-increment fields now strict
    use NULL
    Or zero or leave the field name out completely.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •