     1: <?php
     2: /**
     3:  * DataTables PHP libraries.
     4:  *
     5:  * PHP libraries for DataTables and DataTables Editor, utilising PHP 5.3+.
     6:  *
     7:  *  @author    SpryMedia
     8:  *  @copyright 2012-2014 SpryMedia ( http://sprymedia.co.uk )
     9:  *  @license   http://editor.datatables.net/license DataTables Editor
    10:  *  @link      http://editor.datatables.net
    11:  */
    12:
    13: namespace DataTables\Editor;
    14: if (!defined('DATATABLES')) exit();
    15:
    16: use DataTables;
    17:
    18: /**
    19:  * Common validation options that can be specified for all validation methods.
    20:  */
    21: class ValidateOptions extends DataTables\Ext {
    22:     private $_empty = true;
    23:     private $_message = 'Input not valid';
    24:     private $_optional = true;
    25:
    26:     function __construct( $opts=null )
    27:     {
    28:         if ( $opts ) {
    29:             if ( isset( $opts['empty'] ) ) {
    30:                 $this->allowEmpty( $opts['empty'] );
    31:             }
    32:             if ( isset( $opts['message'] ) ) {
    33:                 $this->message( $opts['message'] );
    34:             }
    35:             if ( isset( $opts['optional'] ) ) {
    36:                 $this->optional( $opts['optional'] );
    37:             }
    38:         }
    39:
    40:         return $this;
    41:     }
    42:
    43:     /**
    44:      * Get / set the error message to use if validation fails
    45:      * @param string $msg Error message to use. If not given, the currently
    46:      *   set message will be returned.
    47:      * @return ValidateOptions|string Self if setting, message if getting.
    48:      */
    49:     public function message ( $msg=null ) {
    50:         if ( $msg === null ) {
    51:             return $this->_message;
    52:         }
    53:
    54:         $this->_message = $msg;
    55:         return $this;
    56:     }
    57:
    58:     /**
    59:      * Get / set the field empty option
    60:      * @param boolean $empty `false` if the field is not allowed to be
    61:      *   empty. `true` if it can be.
    62:      * @return ValidateOptions|boolean Self if setting, current value if getting.
    63:      */
    64:     public function allowEmpty ( $empty=null ) {
    65:         if ( $empty === null ) {
    66:             return $this->_empty;
    67:         }
    68:
    69:         $this->_empty = $empty;
    70:         return $this;
    71:     }
    72:
    73:     /**
    74:      * Get / set the field optional option
    75:      * @param boolean $optional `false` if the field does not need to be
    76:      *   submitted. `true` if it must be.
    77:      * @return ValidateOptions|boolean Self if setting, current value if getting.
    78:      */
    79:     public function optional ( $optional=null ) {
    80:         if ( $optional === null ) {
    81:             return $this->_optional;
    82:         }
    83:
    84:         $this->_optional = $optional;
    85:         return $this;
    86:     }
    87:
    88:
    89:     /**
    90:      * @internal
    91:      */
    92:     static public function select ( $user ) {
    93:         if ( $user ) {
    94:             return $user;
    95:         }
    96:
    97:         return new ValidateOptions();
    98:     }
    99: }