Abstract Class myMVC_BaseAction

Description

Base class for all actions.

You must derive your action's classes from this class.

In the constructor you must define one or more function to validate the input.
Use the method setCheckFunction() to do that.

Example to validate input:
Every field (with the name of the form!!) must have an associative array with
the check type and his parameters.

$definitions is an associative array with the description of the checks.
$names is an associative array with the name of the fields, used for the error message.
$errorMsgs is an associative array with the possible errors. For every check of the field, it should contain a description of the error. {field} is a placeholder for the name of the field (see $names). {checkValue} is a placeholder for the given value for the check (for example: 'min' => 2).
If $errorMsg does not contain a message for the current error, it will just return '{field} is not valid' (parsed with GetText)

$definition has to be created as follow:

  1.  $definition['field on form'array('type' => myMVC_BaseAction::$VALIDATE_xxxparams);

In myMVC_BaseAction there are many $VALIDATE_xxx defines to validate the different input fields.
Every validate-check can have more parameters.
In the following example-code you can find the validate-checks with their parameters explained.
You can give more than one parameter for every check.

Valid parameters for all checks (but $VALIDATE_EXTERN) are:

empty true or false. It decides wheter an empty field should be accepted.
range array of valid values for the checked field.
rangei same as range, but case insensitive (just for $VALIDATE_STRING, $VALIDATE_EMAIL and $VALIDATE_WEB).
rangesql SQL-query to get the valid values for the checked field.
rangesqli same as rangesql, but case insensitive (just for $VALIDATE_STRING, $VALIDATE_EMAIL and $VALIDATE_WEB).
comparedb Execute an SQL-query and compare the result with the given value.
In the query you can use {checkValue} as placeholder for the user's given input value. See example to use it.
WARNING: the query must return just one record with one column, otherwise it throws an exception.

$VALIDATE_NUMBER has 'min' and 'max', too, to restrict the valid numbers into a range.
$VALIDATE_DATETIME has 'format' to give the format of the date (see strftime). This parameter is mandatory!
$VALIDATE_STRING has the parameters 'minlen' and 'maxlen' to restrict the length of the input string, and the parameter 'regexp' to check the string against a given regular expression.
$VALIDATE_EXTERN has just the parameter 'checkFunction'. See the example.

If a required parameter wasn't given, the Framework generates an Exception.

  1.    public function __construct($request$session)
  2.    {
  3.      parent::__construct($request$session);
  4.      $this->setCheckFunction('POST''myValidateFunction');
  5.    }
  6.  
  7.    public function myValidateFunction()
  8.    {
  9.      $definitions array(
  10.          'testBoolean' => array('type' => myMVC_BaseAction::$VALIDATE_BOOLEAN),
  11.          'testBooleanNull' => array('type' => myMVC_BaseAction::$VALIDATE_BOOLEAN,
  12.                                               'empty' => true),
  13.          'testNumber' => array('type' => myMVC_BaseAction::$VALIDATE_NUMBER),
  14.          'testNumberNull' => array('type' => myMVC_BaseAction::$VALIDATE_NUMBER,
  15.                                               'empty' => true),
  16.          'testNumberRange1' => array('type' => myMVC_BaseAction::$VALIDATE_NUMBER,
  17.                                               'min' => 2'max' => 10),
  18.          'testNumberRange2' => array('type' => myMVC_BaseAction::$VALIDATE_NUMBER,
  19.                                               'range' => array(13579)),
  20.          'testNumberRangeSQL' => array('type' => myMVC_BaseAction::$VALIDATE_NUMBER,
  21.                                               'rangesql' => 'SELECT id FROM test'),
  22.          'testDate' => array('type' => myMVC_BaseAction::$VALIDATE_DATETIME,
  23.                                               'format' => _('%m/%d/%Y')),
  24.          'testDateNull' => array('type' => myMVC_BaseAction::$VALIDATE_DATETIME,
  25.                                               'format' => _('%m/%d/%Y')'empty' => true),
  26.                                             // see strftime for format
  27.          'testDateTime' => array('type' => myMVC_BaseAction::$VALIDATE_DATETIME,
  28.                                               'format' => _('%m/%d/%Y %H:%M:%S')),
  29.          'testDateTimeNull' => array('type' => myMVC_BaseAction::$VALIDATE_DATETIME,
  30.                                               'format' => _('%m/%d/%Y %H:%M:%S')'empty' => true),
  31.          'testEMail' => array('type' => myMVC_BaseAction::$VALIDATE_EMAIL),
  32.          'testEMailNull' => array('type' => myMVC_BaseAction::$VALIDATE_EMAIL,
  33.                                               'empty' => true),
  34.          'testEMailRange' => array('type' => myMVC_BaseAction::$VALIDATE_EMAIL,
  35.                                               'range' => array('test@test.de''test@test.com')),
  36.          'testEMailRangeSQL' => array('type' => myMVC_BaseAction::$VALIDATE_EMAIL,
  37.                                               'rangesql' => 'SELECT email FROM user WHERE active = 1'),
  38.          'testWeb' => array('type' => myMVC_BaseAction::$VALIDATE_WEB),
  39.          'testWebNull' => array('type' => myMVC_BaseAction::$VALIDATE_WEB'empty' => true),
  40.          'testWebRange' => array('type' => myMVC_BaseAction::$VALIDATE_WEB,
  41.                                               'range' => array('www.test.de''www.test.com')),
  42.          'testWebRangeSQL' => array('type' => myMVC_BaseAction::$VALIDATE_WEB,
  43.                                               'rangesql' => 'SELECT name FROM domain WHERE active = 1'),
  44.          'testDomain' => array('type' => myMVC_BaseAction::$VALIDATE_WEB'justdomain' => true),
  45.          'testString1' => array('type' => myMVC_BaseAction::$VALIDATE_STRING'maxlen' => 20),
  46.          'testString2' => array('type' => myMVC_BaseAction::$VALIDATE_STRING'maxlen' => 20,
  47.                                               'minlen' => 5),
  48.          'testString3' => array('type' => myMVC_BaseAction::$VALIDATE_STRING'maxlen' => 20,
  49.                                               'empty' => false),
  50.          'testStringRange' => array('type' => myMVC_BaseAction::$VALIDATE_STRING,
  51.                                               'range' => array('A''B''C')),
  52.          'testStringRangeSQL' => array('type' => myMVC_BaseAction::$VALIDATE_STRING,
  53.                                               'rangesql' => 'SELECT name FROM test'),
  54.          'testStringRegExp' => array('type' => myMVC_BaseAction::$VALIDATE_STRING,
  55.                                               'regexp' => '/^x.*=3$/'),
  56.          'testExtraFunction' => array('type' => myMVC_BaseAction::$VALIDATE_EXTERN,
  57.                                               'checkFunction' => 'MyClass::checkValue'),
  58.          'testNewLogin' => array('type' => myMVC_BaseAction::$VALIDATE_STRING'maxlen' => 20'minlen' => 5,
  59.                                               'empty' => false,
  60.                                               'comparedb' => array('query' => "SELECT COUNT(*) FROM user WHERE login = {checkValue}",
  61.                                                                    'compareType' => '==''compareValue' => 0)),
  62.      );
  63.      $names array(
  64.          'field1' => _('Field ONE'),
  65.          'field2' => _('Field TWO')
  66.      );
  67.      $errorMsgs array(
  68.          'field1' => array(myMVC_BaseAction::$ERROR_EMPTY => sprintf(_("%s cannot be empty'"),
  69.                                                               '{field}'),
  70.                            myMVC_BaseAction::$ERROR_MIN   => sprintf(_("%s must be at least %s char long'"),
  71.                                                               '{field}''{checkValue}'),
  72.      );
  73.      return parent::validateInput($definitions$names$errorMsgs);
  74.    }
It is possible to use an external function to validate input:

  1.    public static function checkValue($request$value&$errMsg)
  2.    {
  3.       if($value != 'abcdef')
  4.      {
  5.        $errMsg sprintf(_("Invalid value: %s"$value));
  6.        return false;
  7.      }
  8.      else
  9.        return true;
  10.    }

this function must return true if data are valid, and false otherwise. In this case, $errMsg can contain an error message, that will be parsed with $errorMsgs (placeholder: {externError}).

  • abstract:

Located in /myMVC/BaseAction.php (line 176)

myMVC_BasePage
   |
   --myMVC_BaseAction
Direct descendents
Class Description
Abstract class myMVC_BaseLoginAction Action-class to manage a login system.
 class myMVC_BaseLogoutAction Action-class to manage a login system.
Variable Summary
 static mixed $ERROR_COMPAREDB
 static mixed $ERROR_EMPTY
 static mixed $ERROR_INVALID
 static mixed $ERROR_MAX
 static mixed $ERROR_MAXLEN
 static mixed $ERROR_MIN
 static mixed $ERROR_MINLEN
 static mixed $ERROR_NOTBOOL
 static mixed $ERROR_NOTDATETIME
 static mixed $ERROR_NOTEMAIL
 static mixed $ERROR_NOTNUMBER
 static mixed $ERROR_NOTSTRING
 static mixed $ERROR_NOTWEB
 static mixed $ERROR_RANGE
 static mixed $ERROR_REGEXP
 static mixed $VALIDATE_BOOLEAN
 static mixed $VALIDATE_DATETIME
 static mixed $VALIDATE_EMAIL
 static mixed $VALIDATE_EXTERN
 static mixed $VALIDATE_NUMBER
 static mixed $VALIDATE_STRING
 static mixed $VALIDATE_WEB
 mixed $request
 mixed $session
Method Summary
 static string getActionMenuName ()
 static string getActionToken ()
 static string getCSSClass ()
 static boolean hasUserRightForAction ()
 static boolean isMenuAction ()
 myMVC_BaseAction __construct (myMVC_HttpRequest $request, myMVC_HttpSession $session)
 boolean isCalledFromMenu ()
 boolean isComingFromView ( $view, class $views)
 void setCheckFunction (string $type, string $func)
 boolean validate ()
 boolean validateInput (array $definitions, array $names, array $errorMsgs)
Variables
static mixed $ERROR_COMPAREDB = 'comparedb' (line 266)

Error not matched in compare database

  • access: public
static mixed $ERROR_EMPTY = 'empty' (line 254)

Error empty

  • access: public
static mixed $ERROR_INVALID = 'invalid' (line 270)

Error invalid (generic)

  • access: public
static mixed $ERROR_MAX = 'max' (line 238)

Error greater than given maximum

  • access: public
static mixed $ERROR_MAXLEN = 'maxlen' (line 246)

Error longer than given maximum

  • access: public
static mixed $ERROR_MIN = 'min' (line 242)

Error lesser than given minimum

  • access: public
static mixed $ERROR_MINLEN = 'minlen' (line 250)

Error smaller than given minimum

  • access: public
static mixed $ERROR_NOTBOOL = 'notbool' (line 214)

Error not boolean

  • access: public
static mixed $ERROR_NOTDATETIME = 'notdatetime' (line 218)

Error not date/time

  • access: public
static mixed $ERROR_NOTEMAIL = 'notemail' (line 222)

Error not E-Mail

  • access: public
static mixed $ERROR_NOTNUMBER = 'notnumber' (line 226)

Error not numeric

  • access: public
static mixed $ERROR_NOTSTRING = 'notstring' (line 230)

Error not string

  • access: public
static mixed $ERROR_NOTWEB = 'notweb' (line 234)

Error not URL

  • access: public
static mixed $ERROR_RANGE = 'range' (line 258)

Error not in given range

  • access: public
static mixed $ERROR_REGEXP = 'regexp' (line 262)

Error not matched in regular expression

  • access: public
static mixed $VALIDATE_BOOLEAN = 'bool' (line 185)

Check for a boolean value

  • access: public
static mixed $VALIDATE_DATETIME = 'dateTime' (line 193)

Check for a date/time value

  • access: public
static mixed $VALIDATE_EMAIL = 'email' (line 197)

Check for an E-Mail

  • access: public
static mixed $VALIDATE_EXTERN = 'extern' (line 209)

Check using external function

  • access: public
static mixed $VALIDATE_NUMBER = 'number' (line 189)

Check for a numeric value

  • access: public
static mixed $VALIDATE_STRING = 'string' (line 205)

Check for a generic string

  • access: public
static mixed $VALIDATE_WEB = 'web' (line 201)

Check for an URL

  • access: public
mixed $request (line 178)
  • access: protected
mixed $session (line 179)
  • access: protected
Methods
static getActionMenuName (line 334)

Returns the string to be displayed in the menu to identify the current action.

Only necessary if isMenuAction() returns true

  • return: The menu item
  • access: public
string getActionMenuName ()

Redefined in descendants as:
static getActionToken (line 326)

Returns the token used to identify the current Action

  • return: The token
  • abstract:
  • access: public
string getActionToken ()

Redefined in descendants as:
static getCSSClass (line 351)

Returns the CSS class of this action

  • return: The CSS class
  • access: public
string getCSSClass ()
static hasUserRightForAction (line 344)

Returns whether the current user is allowed to call the current Action

  • return: true if the user may call this Action, false otherwise
  • abstract:
  • access: public
boolean hasUserRightForAction ()

Redefined in descendants as:
static isMenuAction (line 361)

Returns whether this action should appear in the menu (created by controller)

  • return: true if the action should appear in menu
  • access: public
boolean isMenuAction ()

Redefined in descendants as:
Constructor __construct (line 278)

Constructor

  • access: public
myMVC_BaseAction __construct (myMVC_HttpRequest $request, myMVC_HttpSession $session)

Redefinition of:
myMVC_BasePage::__construct()
Constructor.
isCalledFromMenu (line 383)

Returns if the current action was called from menu or from a view

  • return: true if the action was called from a menu, false if it was called from a view
  • access: protected
boolean isCalledFromMenu ()
isComingFromView (line 372)

Returns whether this action was called from the given view

  • return: true if the action was called from the given view, false otherwise
  • access: protected
boolean isComingFromView ( $view, class $views)
  • class $views: The view
  • $view
manageInvalidData (line 438)

Called if user data are not valid (check with validate)

  • return: Response code for the next Action or View (typically the same view, with error messages)
  • access: public
myMVC_HttpResponse manageInvalidData ()
perform (line 427)

Perform the Action

  • return: Response code for the next Action or View
  • access: public
myMVC_HttpResponse perform ()

Redefined in descendants as:
setCheckFunction (line 293)

Set the functions to be used to validate the user's input.

  • access: protected
void setCheckFunction (string $type, string $func)
  • string $type: The type of the request (ALL, POST, GET)
  • string $func: The method of the current class, to validate the input. It must return a boolean value (true: all valid, false: at least one field not validated)
validate (line 397)

Validate the data from user.

It call the proper validate function, depending on the request method (GET or POST), to allow different checks. WARNING: this function should not be overrided! Please create proper validatePost() or validateGet() functions!

  • return: true if data are valid, false otherwise
  • access: public
boolean validate ()
validateInput (line 630)

Check if the given data (from Request) are valid.

The function save in the session (namespace: validate) an associative array with the invalid fields.

  • return: true if ALL fields are valid, false otherwise
  • throws: myMVC_MVCException On problems
  • access: protected
boolean validateInput (array $definitions, array $names, array $errorMsgs)
  • array $definitions: The definition of the fields to validate as associative array ('field' => VALIDATE_TYPE)
  • array $names: The names of the fields to be used for displaying errors as associative array ('field' => 'name')
  • array $errorMsgs: The error messages for the fields as associative array ('field' => array('error' => 'message'))

Inherited Methods

Inherited From myMVC_BasePage

 myMVC_BasePage::__construct()
 myMVC_BasePage::getFromPreviousSubmit()
 myMVC_BasePage::getInvalidFromPreviousSubmit()
 myMVC_BasePage::getLoggedUser()
 myMVC_BasePage::getParameterErrors()
 myMVC_BasePage::getParameterFromPreviousPage()
 myMVC_BasePage::isParameterFromPreviousSubmitValid()
 myMVC_BasePage::saveParameterForNextPage()

Documentation generated on Wed, 27 Jun 2012 09:45:09 +0200 by phpDocumentor 1.4.4