\myMVC_BaseAction

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:

$definition['field on form'] = array('type' => myMVC_BaseAction::$VALIDATE_xxx, params);

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 and $VALIDATE_UPLOAD) 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.
comparefield like comparedb, but it compares two input fields. Useful for action like "password change", when the user has to give the password twice.

$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. $VALIDATE_UPLOAD has the parameters 'minlen' (in KB), 'maxlen' (in KB), 'empty' and 'allowed_types'. See the example. PLEASE REMENBER TO SET 'upload_max_filesize' in your php.ini!!

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

public function construct($request, $session) { parent::construct($request, $session); $this->setCheckFunction('POST', 'myValidateFunction'); }

public function myValidateFunction() { $definitions = array( 'testBoolean' => array('type' => myMVC_BaseAction::$VALIDATE_BOOLEAN), 'testBooleanNull' => array('type' => myMVC_BaseAction::$VALIDATE_BOOLEAN, 'empty' => true), 'testNumber' => array('type' => myMVC_BaseAction::$VALIDATE_NUMBER), 'testNumberNull' => array('type' => myMVC_BaseAction::$VALIDATE_NUMBER, 'empty' => true), 'testNumberRange1' => array('type' => myMVC_BaseAction::$VALIDATE_NUMBER, 'min' => 2, 'max' => 10), 'testNumberRange2' => array('type' => myMVC_BaseAction::$VALIDATE_NUMBER, 'range' => array(1, 3, 5, 7, 9)), 'testNumberRangeSQL' => array('type' => myMVC_BaseAction::$VALIDATE_NUMBER, 'rangesql' => 'SELECT id FROM test'), 'testDate' => array('type' => myMVC_BaseAction::$VALIDATEDATETIME, 'format' => ('%m/%d/%Y')), 'testDateNull' => array('type' => myMVC_BaseAction::$VALIDATEDATETIME, 'format' => ('%m/%d/%Y'), 'empty' => true), // see strftime for format 'testDateTime' => array('type' => myMVC_BaseAction::$VALIDATEDATETIME, 'format' => ('%m/%d/%Y %H:%M:%S')), 'testDateTimeNull' => array('type' => myMVC_BaseAction::$VALIDATEDATETIME, 'format' => ('%m/%d/%Y %H:%M:%S'), 'empty' => true), 'testEMail' => array('type' => myMVC_BaseAction::$VALIDATE_EMAIL), 'testEMailNull' => array('type' => myMVC_BaseAction::$VALIDATE_EMAIL, 'empty' => true), 'testEMailRange' => array('type' => myMVC_BaseAction::$VALIDATE_EMAIL, 'range' => array('test@test.de', 'test@test.com')), 'testEMailRangeSQL' => array('type' => myMVC_BaseAction::$VALIDATE_EMAIL, 'rangesql' => 'SELECT email FROM user WHERE active = 1'), 'testWeb' => array('type' => myMVC_BaseAction::$VALIDATE_WEB), 'testWebNull' => array('type' => myMVC_BaseAction::$VALIDATE_WEB, 'empty' => true), 'testWebRange' => array('type' => myMVC_BaseAction::$VALIDATE_WEB, 'range' => array('www.test.de', 'www.test.com')), 'testWebRangeSQL' => array('type' => myMVC_BaseAction::$VALIDATE_WEB, 'rangesql' => 'SELECT name FROM domain WHERE active = 1'), 'testDomain' => array('type' => myMVC_BaseAction::$VALIDATE_WEB, 'justdomain' => true), 'testString1' => array('type' => myMVC_BaseAction::$VALIDATE_STRING, 'maxlen' => 20), 'testString2' => array('type' => myMVC_BaseAction::$VALIDATE_STRING, 'maxlen' => 20, 'minlen' => 5), 'testString3' => array('type' => myMVC_BaseAction::$VALIDATE_STRING, 'maxlen' => 20, 'empty' => false), 'testStringRange' => array('type' => myMVC_BaseAction::$VALIDATE_STRING, 'range' => array('A', 'B', 'C')), 'testStringRangeSQL' => array('type' => myMVC_BaseAction::$VALIDATE_STRING, 'rangesql' => 'SELECT name FROM test'), 'testStringRegExp' => array('type' => myMVC_BaseAction::$VALIDATE_STRING, 'regexp' => '/^x.=3$/'), 'testExtraFunction' => array('type' => myMVC_BaseAction::$VALIDATE_EXTERN, 'checkFunction' => 'MyClass::checkValue'), 'testNewLogin' => array('type' => myMVC_BaseAction::$VALIDATE_STRING, 'maxlen' => 20, 'minlen' => 5, 'empty' => false, 'comparedb' => array('query' => "SELECT COUNT() FROM user WHERE login = {checkValue}", 'compareType' => '==', 'compareValue' => 0)), 'testPass1' => array('type' => myMVC_BaseAction::$VALIDATE_STRING, 'maxlen' => 20, 'minlen' => 5, 'empty' => true), 'testPass2' => array('type' => myMVC_BaseAction::$VALIDATE_STRING, 'maxlen' => 20, 'minlen' => 5, 'empty' => true, 'comparefield' => array('field' => 'testPass1', 'compareType' => '==')), 'testFile' => array('type' => myMVC_BaseAction::$VALIDATE_UPLOAD, 'minlen' => 1024, 'maxlen' => 65535, 'allowedtypes' = array('text/plain', 'text/html'), 'empty' => false), ); $names = array( 'field1' => ('Field ONE'), 'field2' => _('Field TWO') ); $errorMsgs = array( 'field1' => array(myMVC_BaseAction::$ERROREMPTY => sprintf(("%s cannot be empty'"), '{field}'), myMVC_BaseAction::$ERRORMIN => sprintf(("%s must be at least %s char long'"), '{field}', '{checkValue}'), ); return parent::validateInput($definitions, $names, $errorMsgs); } It is possible to use an external function to validate input:

public static function checkValue($request, $value, &$errMsg) { if($value != 'abcdef') { $errMsg = sprintf(_("Invalid value: %s", $value)); return false; } else return true; }

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}).

If you want to call the Action as AJAX, you can (but not must) define the methods performAjax() and manageInvalidDataAjax() with same parameters of perform() and manageInvalidData(). To validate the data, you should pass "AJAX" as first parameter to setCheckFunction(). So you can have an Action that supports normal calls (GET and/or POST) and AJAX

Summary

Methods
Properties
Constants
__construct()
saveRenderEngine()
getActionToken()
getActionMenuName()
getActionMenuPosition()
hasUserRightForAction()
getCSSClass()
isMenuAction()
validate()
perform()
manageInvalidData()
$VALIDATE_BOOLEAN
$VALIDATE_NUMBER
$VALIDATE_DATETIME
$VALIDATE_EMAIL
$VALIDATE_WEB
$VALIDATE_STRING
$VALIDATE_EXTERN
$VALIDATE_UPLOAD
$ERROR_NOTBOOL
$ERROR_NOTDATETIME
$ERROR_NOTEMAIL
$ERROR_NOTNUMBER
$ERROR_NOTSTRING
$ERROR_NOTWEB
$ERROR_MAX
$ERROR_MIN
$ERROR_MAXLEN
$ERROR_MINLEN
$ERROR_EMPTY
$ERROR_RANGE
$ERROR_REGEXP
$ERROR_COMPAREDB
$ERROR_COMPAREFIELD
$ERROR_INVALID
$ERROR_INVALIDTYPE
$ERROR_UPLOADERROR
No constants found
getLoggedUser()
saveParameterForNextPage()
getParameterFromPreviousPage()
getFromPreviousSubmit()
getInvalidFromPreviousSubmit()
isParameterFromPreviousSubmitValid()
getParameterErrors()
getRenderEngine()
setCheckFunction()
isComingFromView()
isCalledFromMenu()
validateInput()
getNumber()
$renderEngine
$request
$session
N/A
createErrorString()
checkExtraParameters()
$checkFunctionAll
$checkFunctionPost
$checkFunctionGet
$checkFunctionAjax
N/A

Properties

$VALIDATE_BOOLEAN

$VALIDATE_BOOLEAN : 

Check for a boolean value

Type

$VALIDATE_NUMBER

$VALIDATE_NUMBER : 

Check for a numeric value

Type

$VALIDATE_DATETIME

$VALIDATE_DATETIME : 

Check for a date/time value

Type

$VALIDATE_EMAIL

$VALIDATE_EMAIL : 

Check for an E-Mail

Type

$VALIDATE_WEB

$VALIDATE_WEB : 

Check for an URL

Type

$VALIDATE_STRING

$VALIDATE_STRING : 

Check for a generic string

Type

$VALIDATE_EXTERN

$VALIDATE_EXTERN : 

Check using external function

Type

$VALIDATE_UPLOAD

$VALIDATE_UPLOAD : 

Check for upload file

Type

$ERROR_NOTBOOL

$ERROR_NOTBOOL : 

Error not boolean

Type

$ERROR_NOTDATETIME

$ERROR_NOTDATETIME : 

Error not date/time

Type

$ERROR_NOTEMAIL

$ERROR_NOTEMAIL : 

Error not E-Mail

Type

$ERROR_NOTNUMBER

$ERROR_NOTNUMBER : 

Error not numeric

Type

$ERROR_NOTSTRING

$ERROR_NOTSTRING : 

Error not string

Type

$ERROR_NOTWEB

$ERROR_NOTWEB : 

Error not URL

Type

$ERROR_MAX

$ERROR_MAX : 

Error greater than given maximum

Type

$ERROR_MIN

$ERROR_MIN : 

Error lesser than given minimum

Type

$ERROR_MAXLEN

$ERROR_MAXLEN : 

Error longer than given maximum

Type

$ERROR_MINLEN

$ERROR_MINLEN : 

Error smaller than given minimum

Type

$ERROR_EMPTY

$ERROR_EMPTY : 

Error empty

Type

$ERROR_RANGE

$ERROR_RANGE : 

Error not in given range

Type

$ERROR_REGEXP

$ERROR_REGEXP : 

Error not matched in regular expression

Type

$ERROR_COMPAREDB

$ERROR_COMPAREDB : 

Error not matched in compare database

Type

$ERROR_COMPAREFIELD

$ERROR_COMPAREFIELD : 

Error not matched in compare fields

Type

$ERROR_INVALID

$ERROR_INVALID : 

Error invalid (generic)

Type

$ERROR_INVALIDTYPE

$ERROR_INVALIDTYPE : 

Error invalid type for upload

Type

$ERROR_UPLOADERROR

$ERROR_UPLOADERROR : 

Generic error for upload

Type

$renderEngine

$renderEngine : 

Type

$request

$request : 

Type

$session

$session : 

Type

$checkFunctionAll

$checkFunctionAll : 

Type

$checkFunctionPost

$checkFunctionPost : 

Type

$checkFunctionGet

$checkFunctionGet : 

Type

$checkFunctionAjax

$checkFunctionAjax : 

Type

Methods

__construct()

__construct(\myMVC_HttpRequest  $request, \myMVC_HttpSession  $session) 

Constructor

This function does nothing! This class will not be used, but just derived (see myMVC_BaseAction and myMVC_BaseView)

Parameters

\myMVC_HttpRequest $request

The current request

\myMVC_HttpSession $session

The current session

saveRenderEngine()

saveRenderEngine(\Class  $renderEngine) 

Save the current used render engine.

It can be used to instanciate Mailview to be used in MessageManager

Parameters

\Class $renderEngine

The render engine

getActionToken()

getActionToken() : string

Returns the token used to identify the current Action

Returns

string —

The token

getActionMenuName()

getActionMenuName() : string

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

Only necessary if isMenuAction() returns true

Returns

string —

The menu item

getActionMenuPosition()

getActionMenuPosition() : integer

Returns the position of this action in the menu (created by controller) Only necessary if isMenuAction() returns true

Returns

integer —

The position in the menu (lower is "upper")

hasUserRightForAction()

hasUserRightForAction() : boolean

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

Returns

boolean —

true if the user may call this Action, false otherwise

getCSSClass()

getCSSClass() : string

Returns the CSS class of this action

Returns

string —

The CSS class

isMenuAction()

isMenuAction() : boolean

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

Returns

boolean —

true if the action should appear in menu

validate()

validate() : boolean

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!

Returns

boolean —

true if data are valid, false otherwise

perform()

perform() : \myMVC_HttpResponse

Perform the Action

Returns

\myMVC_HttpResponse

Response code for the next Action or View

manageInvalidData()

manageInvalidData() : \myMVC_HttpResponse

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

Returns

\myMVC_HttpResponse

Response code for the next Action or View (typically the same view, with error messages)

getLoggedUser()

getLoggedUser() : \myMVC_BaseLogin

Returns the current logged user, if any.

This function has to be static, then it will normally called from static functions (eg hasUserRightForAction).

Returns

\myMVC_BaseLogin

The currently logged user, or NULL if no user is logged in, or the user's class is not a subclass of myMVC_BaseLogin

saveParameterForNextPage()

saveParameterForNextPage(string  $key, string  $value) 

Save a variable in the session, to be used by the next page (Action or view).

This variable can be read with the method getParameterFromPreviousPage of the classes BaseAction or BaseView

Parameters

string $key

The name of the variable

string $value

The value

getParameterFromPreviousPage()

getParameterFromPreviousPage(string  $key) 

Returns a variable saved from the previous class with saveParameterForNextPage

Parameters

string $key

The name of the variable

getFromPreviousSubmit()

getFromPreviousSubmit(  $field) 

Returns the value for the given field in the previous submitted form.

WARNING: This function just returns the fields, that passed the validation!!

Parameters

$field

getInvalidFromPreviousSubmit()

getInvalidFromPreviousSubmit(  $field) 

Returns the value for the given field in the previous submitted form.

WARNING: This function just returns the fields, that NOT passed the validation!!

Parameters

$field

isParameterFromPreviousSubmitValid()

isParameterFromPreviousSubmitValid(string  $field) : boolean

Returns whether the given parameter of the previous submitted form passed the validation or not.

Parameters

string $field

The name of the parameter

Returns

boolean —

true if the parameter passed the validation, false otherwise

getParameterErrors()

getParameterErrors() : array

Returns all error messages after the validation of the user's parameters

Returns

array —

An associative array with all the errors in the format 'field' => 'error message'

getRenderEngine()

getRenderEngine() : \Class

Return the current used render engine.

Returns

\Class —

The render engine

setCheckFunction()

setCheckFunction(string  $type, string  $func) 

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

Parameters

string $type

The type of the request (ALL, POST, GET, AJAX)

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)

isComingFromView()

isComingFromView(  $view) : boolean

Returns whether this action was called from the given view

Parameters

$view

Returns

boolean —

true if the action was called from the given view, false otherwise

isCalledFromMenu()

isCalledFromMenu() : boolean

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

Returns

boolean —

true if the action was called from a menu, false if it was called from a view

validateInput()

validateInput(array  $definitions, array  $names, array  $errorMsgs) : boolean

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

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

Parameters

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'))

Throws

\myMVC_MVCException

On problems

Returns

boolean —

true if ALL fields are valid, false otherwise

getNumber()

getNumber(string  $value) : \number

Convert a number to the internal PHP-Format, using current user's locale

Parameters

string $value

The number to be converted

Returns

\number —

The converted number

createErrorString()

createErrorString(array  $definitions, array  $names, array  $errorMsgs, string  $field, string  $check, string  $externError = NULL) : string

Create the error string for the given field.

Parameters

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'))

string $field

The checked field

string $check

The check that field does not passed

string $externError

Optional extra error (given from extern parsed check function). Default: NULL

Returns

string —

The formatted error string

checkExtraParameters()

checkExtraParameters(array  $checks, mixed  $userInput, array  $parameters) : string

Check if the given value from user's form comply with the given restrictions (min, max, range, and so on).

It does NOT check for empty value (this must be done before the first check).

Parameters

array $checks

The required checks as numeric array

mixed $userInput

User's input

array $parameters

The parameters to check the field

Returns

string —

The errors for this field (see myMVC_BaseAction::$ERROR_xxx) or false if no errors occoured