Maslosoft Mangan API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan\Validators\BuiltIn;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Interfaces\Validators\ValidatorInterface;
use Maslosoft\Mangan\Meta\ManganMeta;
use Maslosoft\Mangan\Validators\Traits\Messages;
use Maslosoft\Mangan\Validators\Traits\OnScenario;
use Maslosoft\Mangan\Validators\Traits\Safe;
use Maslosoft\Mangan\Validators\Traits\SkipOnError;
use Maslosoft\Mangan\Validators\Traits\Strict;
use Maslosoft\Mangan\Validators\Traits\When;
/**
* RequiredValidator
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class RequiredValidator implements ValidatorInterface
{
use Strict,
Messages,
SkipOnError,
OnScenario,
Safe,
When;
/**
* @var mixed the desired value that the attribute must have.
* If this is null, the validator will validate that the specified attribute does not have null or empty value.
* If this is set as a value that is not null, the validator will validate that
* the attribute has a value that is the same as this property value.
* Defaults to null.
*/
public $requiredValue = null;
/**
* @var boolean whether the value should be trimmed with php trim() function when comparing strings.
* When set to false, the attribute value is not considered empty when it contains spaces.
* Defaults to true, meaning the value will be trimmed.
* @since 1.1.14
*/
public $trim = true;
/**
* @Label('{attribute} must be {value}')
* @var string
*/
public $msgExact = '';
/**
* @Label('{attribute} cannot be blank')
* @var string
*/
public $msgBlank = '';
public function isValid(AnnotatedInterface $model, $attribute)
{
if (!$this->whenValidate($model))
{
return true;
}
$value = $model->$attribute;
$label = ManganMeta::create($model)->field($attribute)->label;
if (!empty($this->requiredValue))
{
if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue)
{
$this->addError('msgExact', ['{attribute}' => $label, '{value}' => $this->requiredValue]);
return false;
}
}
elseif ($this->isEmpty($value, $this->trim))
{
$this->addError('msgBlank', ['{attribute}' => $label]);
return false;
}
return true;
}
/**
* Checks if the given value is empty.
* A value is considered empty if it is null, an empty array, or the trimmed result is an empty string.
* Note that this method is different from PHP empty(). It will return false when the value is 0.
* @param mixed $value the value to be checked
* @param boolean $trim whether to perform trimming before checking if the string is empty. Defaults to false.
* @return boolean whether the value is empty
*/
protected function isEmpty($value, $trim = false)
{
return $value === null || $value === array() || $value === '' || $trim && is_scalar($value) && trim($value) === '';
}
}
API documentation generated by ApiGen