Menu
Index > Helpers

todoValueValidate

todoValueValidate($todo_value, $options = array())

Description:
Returns array:
'result' => Boolean
'empty' => Boolean
'validationErrors' => string

Options:
(array) $todo_value - the TodoValue array.
(array) $options - not employed.

Example:
//
$todoValueValidate = $this->Deals->todoValueValidate($todo_value);
//
echo 'This TodoValue is ' . ($todoValueValidate['result'] ? 'valid' : 'invalid - ' . $todoValueValidate['validationErrors']) . '.';
//
echo 'This TodoValue is ' . ($todoValueValidate['empty'] ? 'empty' : 'not empty') . '.';

	public function todoValueValidate($todo_value, $options = array()) {
		//
		$return			= array();
		//
		$return['result']	= false;
		//
		$return['empty']	= false;
		//
		$defaults		= array(
						'bool'	=> false
						,
					);
		//
		$options		= am($defaults, $options);
		//
		if (isset($todo_value['validation']) && !is_null($todo_value['validation'])) {
			//
			if ($todo_value['validation'] == 'file') {
				// 
				if (!isset($todo_value['Document'])) {
					//
					$todo_value	= $this->find(
									'first'
									, array(
										'conditions'	=> array(
											$this->alias . '.' . $this->primaryKey	=> $todo_value[$this->primaryKey]
											, 
										)
										, 'contain'	=> array(
											'Document'
											,
										)
										,
									)
								);
				}
				//
				if (isset($todo_value['Document'][0]) && !empty($todo_value['Document'][0])) {
					//
					$return['result']	= true;
				//
				} else {
					//
					$return['validationErrors']	= array(
						'A document is required.'
						,
					);
					//
					$return['empty']	= true;
				}
			// 
			} elseif ($todo_value['validation'] == 'email') {
				//
				if (filter_var($todo_value['todo_value'], FILTER_VALIDATE_EMAIL)) {
					//
					$return['result']	= true;
				} else {
					//
					$return['validationErrors']	= array(
						'Invalid email.'
						,
					);
				}
				//
				if (strlen($todo_value['todo_value']) < 1) {
					//
					$return['empty']	= true;
				}
			// notEmpty
			} else {
				//
				if (strlen($todo_value['todo_value']) > 0) {
					//
					$return['result']	= true;
				} else {
					//
					$return['validationErrors']	= array(
						'notEmpty.'
						,
					);
					//
					$return['empty']	= true;
				}
			}
		// notEmpty
		} else {
			//
			if (isset($todo_value['todo_value']) && strlen($todo_value['todo_value']) > 0) {
				//
				$return['result']	= true;
			} else {
				//
				$return['validationErrors']	= array(
					'notEmpty.'
					,
				);
				//
				$return['empty']	= true;
			}
		}
		// 
		return		!$options['bool']
				? $return
				: $return['result'];
	}