Flourish PHP Unframework

fValidationException

class, v1.0.0b4

An exception caused by a data not matching a rule or set of rules

Changes:
1.0.0b4Added support for nested error arrays 10/3/10
1.0.0b3Added removeFieldNames() 5/26/10
1.0.0b2Added a custom __construct() to handle arrays of messages 9/17/09
1.0.0bThe initial implementation 6/14/07

Genealogy

Class Tree

Exception
   |
   --fException
      |
      --fExpectedException
         |
         --fValidationException

Inherited Variables

$code
$file
$line
$message
$previous
$string
$trace

Inherited Methods

fException::__construct()
Sets the message for the exception, allowing for string interpolation and internationalization
fException::__get()
All requests that hit this method should be requests for callbacks
fException::compose()
Composes text using fText if loaded
fException::dump()
Creates a string representation of any variable using predefined strings for booleans, NULL and empty strings
fException::formatTrace()
Gets the backtrace to currently called exception
fException::getCSSClass()
Returns the CSS class name for printing information about the exception
fException::prepare()
Prepares content for output into HTML
fException::printMessage()
Prints the message inside of a div with the class being 'exception %THIS_EXCEPTION_CLASS_NAME%'
fException::printTrace()
Prints the backtrace to currently called exception inside of a pre tag with the class being 'exception %THIS_EXCEPTION_CLASS_NAME% trace'
fException::registerCallback()
Adds a callback for when certain types of exceptions are created
fException::reorderMessage()
Reorders list items in the message based on simple string matching
fException::setMessage()
Allows the message to be overwriten
fException::splitMessage()
Splits an exception with an HTML list into multiple strings each containing part of the original message
constructor __construct ( [$message = ], [$code = ], [$previous = ] )
__clone ( )
__toString ( )
getCode ( )
getFile ( )
getLine ( )
getMessage ( )
getPrevious ( )
getTrace ( )
getTraceAsString ( )

Static Variables

::$field_format protected

The formatting string to use for field names

Type

string

Static Methods

::formatField() public

Accepts a field name and formats it based on the formatting string set via setFieldFormat()

Signature

string formatField( string $field )

Parameters

string $field The name of the field to format

Returns

The formatted field name

::removeFieldNames() public

Removes the field names from normal validation messages, leaving just the message part

Signature

array removeFieldNames( array $messages )

Parameters

array $messages The messages to remove the field names from

Returns

The messages without field names

::setFieldFormat() public

Set the format to be applied to all field names used in fValidationExceptions

The format should contain exactly one %s sprintf() conversion specification, which will be replaced with the field name. Any literal % characters should be written as %%.

The default format is just %s: , which simply inserts a : and space after the field name.

Signature

void setFieldFormat( string $format )

Parameters

string $format A string to format the field name with - %s will be replaced with the field name

Methods

->__construct() public

Sets the message for the exception, allowing for custom formatting beyond fException

If this method receives exactly two parameters, a string and an array, the string will be used as a message in a HTML <p> tag and the array will be turned into an unorder list <ul> tag with each element in the array being an <li> tag. It is possible to pass an optional exception code as a third parameter.

The following PHP:

throw new fValidationException(
    'The following problems were found:',
    array(
        'Please provide your name',
        'Please provide your email address'
    )
);

Would create the message:

The following problems were found:

  • Please provide your name
  • Please provide your email address

If the parameters are anything else, they will be passed to fException::__construct().

Signature

fException __construct( string $message='', array $sub_messages, mixed $code )

Parameters

string $message The beginning message for the exception. This will be placed in a <p> tag.
array $sub_messages An array of strings to place in a <ul> tag
mixed $code The optional exception code

Overrides

fException::__construct()
Sets the message for the exception, allowing for string interpolation and internationalization