Edited at

PHPフレームワークの空判定バリデーションを比較

More than 5 years have passed since last update.

PHPフレームワークの空白判定バリデーション比較


Codeigniter2.1


/Form_validation.php

/**

* Required
*
* @access public
* @param string
* @return bool
*/

public function required($str)
{
if ( ! is_array($str))
{
return (trim($str) == '') ? FALSE : TRUE;
}
else
{
return ( ! empty($str));
}
}

value
required(value)

$undefined
 false

null
 false

true          
true

false     
false

'' 
false

0
 true

'0'    
true

array()
false


fuel1.6


validation.php

/**

* Special empty method because 0 and '0' are non-empty values
*
* @param mixed
* @return bool
*/

public static function _empty($val)
{
return ($val === false or $val === null or $val === '' or $val === array());
}

value
!_empty(value)

$undefined
false

null            
false

true          
true

false          
false

''                
false

0                
true

'0'              
true

array()        
false


Symfony1.4


sfValidatorBase.class.php

  /**

* Returns true if the value is empty.
*
* @param mixed $value The input value
*
* @return bool true if the value is empty, false otherwise
*/

protected function isEmpty($value)
{
return in_array($value, array(null, '', array()), true);
}

value
!isEmpty(value)

$undefined
false

null            
false

true          
true

false          
true

''                
false

0                
true

'0'              
true

array()        
false


Symfony2.3


NotBlankValidator.php

class NotBlankValidator extends ConstraintValidator

{
/**
* {@inheritDoc}
*/

public function validate($value, Constraint $constraint)
{
if (false === $value || (empty($value) && '0' != $value)) {
$this->context->addViolation($constraint->message);
}
}
}

value
validate(value)

$undefined
false

null            
false

true          
true

false          
false

''                
false

0                
true

'0'              
true

array()        
false


BlankValidator.php

/**

* @author Bernhard Schussek <bschussek@gmail.com>
*
* @api
*/

class BlankValidator extends ConstraintValidator
{
/**
* {@inheritDoc}
*/

public function validate($value, Constraint $constraint)
{
if ('' !== $value && null !== $value) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
}
}
}

value
!validate(value)

$undefined
false

null            
false

true          
true

false          
true

''                
false

0                
true

'0'              
true

array()        
true


cake2.4


Validation.php

/**

* Checks that a string contains something other than whitespace
*
* Returns true if string contains something other than whitespace
*
* $check can be passed as an array:
* array('check' => 'valueToCheck');
*
* @param string|array $check Value to check
* @return boolean Success
*/

public static function notEmpty($check) {
if (is_array($check)) {
extract(self::_defaults($check));
}

if (empty($check) && $check != '0') {
return false;
}
return self::_check($check, '/[^\s]+/m');
}