クラス yii\validators\ImageValidator
ImageValidator は、属性が有効な画像を受け取っているかどうかを検証します。
公開プロパティ
公開メソッド
保護されたメソッド
メソッド | 説明 | 定義元 |
---|---|---|
formatMessage() | I18Nを使用してメッセージをフォーマットするか、\Yii::$app が利用できない場合は単純なstrtrを使用します。 |
yii\validators\Validator |
getMimeTypeByFile() | ファイルパスからMIMEタイプを取得します | yii\validators\FileValidator |
validateExtension() | 指定されたアップロード済みファイルの型(拡張子)が現在のバリデーター設定に対して正しいかどうかをチェックします。 | yii\validators\FileValidator |
validateImage() | 画像ファイルを検証します。 | yii\validators\ImageValidator |
validateMimeType() | $mimeTypesプロパティのリストに対して$fileのmimeTypeをチェックします。 | yii\validators\FileValidator |
validateValue() | 値を検証します。 | yii\validators\ImageValidator |
プロパティの詳細
最大高さ(ピクセル単位)。デフォルトはnullで、制限がないことを意味します。
画像の高さが大きすぎる場合に使用されるカスタマイズされたメッセージについては、$overHeightも参照してください。
最大高さ(ピクセル単位)。デフォルトはnullで、制限がないことを意味します。
画像の幅が大きすぎる場合に使用されるカスタマイズされたメッセージについては、$overWidthも参照してください。
最小高さ(ピクセル単位)。デフォルトはnullで、制限がないことを意味します。
画像の高さが小さすぎる場合に使用されるカスタマイズされたメッセージについては、$underHeightも参照してください。
最小幅(ピクセル単位)。デフォルトはnullで、制限がないことを意味します。
画像の幅が小さすぎる場合に使用されるカスタマイズされたメッセージについては、$underWidthも参照してください。
アップロードされたファイルが画像ではない場合に使用されるエラーメッセージ。メッセージでは次のトークンを使用できます。
- {attribute}: 属性名
- {file}: アップロードされたファイル名
画像が$maxHeightを超えている場合に使用されるエラーメッセージ。メッセージでは次のトークンを使用できます。
- {attribute}: 属性名
- {file}: アップロードされたファイル名
- {limit}: $maxHeightの値
画像が$minHeight未満の場合に使用されるエラーメッセージ。メッセージには以下のトークンを使用できます。
- {attribute}: 属性名
- {file}: アップロードされたファイル名
- {limit}: $minHeightの値
メソッド詳細
定義元: yii\base\Component::__call()
クラスメソッドではない名前付きメソッドを呼び出します。
このメソッドは、アタッチされたビヘイビアが指定された名前のメソッドを持っているかどうかを確認し、利用可能な場合は実行します。
このメソッドは、未知のメソッドが呼び出されたときに暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
public mixed __call ( $name, $params ) | ||
$name | string |
メソッド名 |
$params | array |
メソッドのパラメータ |
return | mixed |
メソッドの戻り値 |
---|---|---|
throws | yii\base\UnknownMethodException |
未知のメソッドを呼び出した場合 |
public function __call($name, $params)
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $object) {
if ($object->hasMethod($name)) {
return call_user_func_array([$object, $name], $params);
}
}
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
定義元: yii\base\Component::__clone()
このメソッドは、既存のオブジェクトを複製してオブジェクトが作成された後に呼び出されます。
古いオブジェクトにアタッチされているため、すべてのビヘイビアを削除します。
public void __clone ( ) |
public function __clone()
{
$this->_events = [];
$this->_eventWildcards = [];
$this->_behaviors = null;
}
定義元: yii\base\BaseObject::__construct()
コンストラクター。
デフォルトの実装では、以下の2つの処理を行います。
- 指定された構成
$config
でオブジェクトを初期化します。 - init()を呼び出します。
このメソッドが子クラスでオーバーライドされている場合、推奨されるのは、
- コンストラクタの最後のパラメータが、ここでの
$config
のような構成配列であることです。 - コンストラクタの最後に親の実装を呼び出すことです。
public void __construct ( $config = [] ) | ||
$config | array |
オブジェクトのプロパティを初期化するために使用される名前と値のペア |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義元: yii\base\Component::__get()
コンポーネントのプロパティの値を返します。
このメソッドは、以下の順序で確認し、それに応じて動作します。
- ゲッターによって定義されたプロパティ:ゲッターの結果を返します
- ビヘイビアのプロパティ:ビヘイビアのプロパティ値を返します
このメソッドは、$value = $component->property;
を実行するときに暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
関連項目:__set()。
public mixed __get ( $name ) | ||
$name | string |
プロパティ名 |
return | mixed |
プロパティ値またはビヘイビアのプロパティの値 |
---|---|---|
throws | yii\base\UnknownPropertyException |
プロパティが定義されていない場合 |
throws | yii\base\InvalidCallException |
プロパティが書き込み専用の場合 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
// read property, e.g. getName()
return $this->$getter();
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name;
}
}
if (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定義元: yii\base\Component::__isset()
プロパティが設定されているか(つまり、定義されていてnullではないか)どうかをチェックします。
このメソッドは、以下の順序で確認し、それに応じて動作します。
- セッターによって定義されたプロパティ:プロパティが設定されているかどうかを返します
- ビヘイビアのプロパティ:プロパティが設定されているかどうかを返します
- 存在しないプロパティに対して
false
を返します
このメソッドは、isset($component->property)
を実行するときに暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
public boolean __isset ( $name ) | ||
$name | string |
プロパティ名またはイベント名 |
return | boolean |
指定されたプロパティが設定されているかどうか |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name)) {
return $behavior->$name !== null;
}
}
return false;
}
定義元: yii\base\Component::__set()
コンポーネントのプロパティの値を設定します。
このメソッドは、以下の順序で確認し、それに応じて動作します。
- セッターによって定義されたプロパティ:プロパティ値を設定します
- "on xyz"の形式のイベント:ハンドラーをイベント"xyz"にアタッチします
- "as xyz"の形式のビヘイビア:名前が"xyz"のビヘイビアをアタッチします
- ビヘイビアのプロパティ:ビヘイビアのプロパティ値を設定します
このメソッドは、$component->property = $value;
を実行するときに暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
関連項目:__get()。
public void __set ( $name, $value ) | ||
$name | string |
プロパティ名またはイベント名 |
$value | mixed |
プロパティ値 |
throws | yii\base\UnknownPropertyException |
プロパティが定義されていない場合 |
---|---|---|
throws | yii\base\InvalidCallException |
プロパティが読み取り専用の場合 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
// set property
$this->$setter($value);
return;
} elseif (strncmp($name, 'on ', 3) === 0) {
// on event: attach event handler
$this->on(trim(substr($name, 3)), $value);
return;
} elseif (strncmp($name, 'as ', 3) === 0) {
// as behavior: attach behavior
$name = trim(substr($name, 3));
$this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = $value;
return;
}
}
if (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
定義元: yii\base\Component::__unset()
コンポーネントのプロパティをnullに設定します。
このメソッドは、以下の順序で確認し、それに応じて動作します。
- セッターによって定義されたプロパティ:プロパティ値をnullに設定します
- ビヘイビアのプロパティ:プロパティ値をnullに設定します
このメソッドは、unset($component->property)
を実行するときに暗黙的に呼び出されるPHPのマジックメソッドであるため、直接呼び出さないでください。
public void __unset ( $name ) | ||
$name | string |
プロパティ名 |
throws | yii\base\InvalidCallException |
プロパティが読み取り専用の場合 |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
return;
}
// behavior property
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name)) {
$behavior->$name = null;
return;
}
}
throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}
定義元: yii\validators\Validator::addError()
指定された属性に関するエラーをモデルオブジェクトに追加します。
これは、メッセージの選択と国際化を実行するヘルパーメソッドです。
public void addError ( $model, $attribute, $message, $params = [] ) | ||
$model | yii\base\Model |
バリデーションされるデータモデル |
$attribute | string |
バリデーションされる属性 |
$message | string |
エラーメッセージ |
$params | array |
エラーメッセージ内のプレースホルダーの値 |
public function addError($model, $attribute, $message, $params = [])
{
$params['attribute'] = $model->getAttributeLabel($attribute);
if (!isset($params['value'])) {
$value = $model->$attribute;
if (is_array($value)) {
$params['value'] = 'array()';
} elseif (is_object($value) && !method_exists($value, '__toString')) {
$params['value'] = '(object)';
} else {
$params['value'] = $value;
}
}
$model->addError($attribute, $this->formatMessage($message, $params));
}
定義元: yii\base\Component::attachBehavior()
このコンポーネントにビヘイビアをアタッチします。
このメソッドは、与えられた構成に基づいてビヘイビアオブジェクトを作成します。その後、yii\base\Behavior::attach() メソッドを呼び出すことによって、ビヘイビアオブジェクトはこのコンポーネントにアタッチされます。
関連項目: detachBehavior()。
public yii\base\Behavior attachBehavior ( $name, $behavior ) | ||
$name | string |
ビヘイビアの名前。 |
$behavior | string|array|yii\base\Behavior |
ビヘイビアの構成。次のいずれかになります
|
return | yii\base\Behavior |
ビヘイビアオブジェクト |
---|
public function attachBehavior($name, $behavior)
{
$this->ensureBehaviors();
return $this->attachBehaviorInternal($name, $behavior);
}
定義元: yii\base\Component::attachBehaviors()
コンポーネントにビヘイビアのリストをアタッチします。
各ビヘイビアはその名前でインデックス付けされ、yii\base\Behavior オブジェクト、ビヘイビアクラスを指定する文字列、またはビヘイビアを作成するための構成配列である必要があります。
関連項目: attachBehavior()。
public void attachBehaviors ( $behaviors ) | ||
$behaviors | array |
コンポーネントにアタッチされるビヘイビアのリスト |
public function attachBehaviors($behaviors)
{
$this->ensureBehaviors();
foreach ($behaviors as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
定義元: yii\base\Component::behaviors()
このコンポーネントがビヘイビアとして動作するべきビヘイビアのリストを返します。
子クラスは、動作させたいビヘイビアを指定するために、このメソッドをオーバーライドできます。
このメソッドの戻り値は、ビヘイビア名でインデックス付けされたビヘイビアオブジェクトまたは構成の配列である必要があります。ビヘイビア構成は、ビヘイビアクラスを指定する文字列、または次の構造の配列のいずれかです。
'behaviorName' => [
'class' => 'BehaviorClass',
'property1' => 'value1',
'property2' => 'value2',
]
ビヘイビアクラスは yii\base\Behavior から拡張する必要があることに注意してください。ビヘイビアは、名前を使用するか、匿名でアタッチできます。名前が配列キーとして使用されている場合、この名前を使用して、後で getBehavior() を使用してビヘイビアを取得したり、detachBehavior() を使用してデタッチしたりできます。匿名のビヘイビアは取得またはデタッチできません。
このメソッドで宣言されたビヘイビアは、自動的に(必要に応じて)コンポーネントにアタッチされます。
public array behaviors ( ) | ||
return | array |
ビヘイビアの構成。 |
---|
public function behaviors()
{
return [];
}
定義元: yii\base\Component::canGetProperty()
プロパティが読み取り可能かどうかを示す値を返します。
プロパティは、次の場合は読み取ることができます。
- クラスに指定された名前に関連付けられたゲッターメソッドがある場合(この場合、プロパティ名は大小文字を区別しません)。
- クラスに指定された名前のメンバ変数がある場合(
$checkVars
が true の場合)。 - アタッチされたビヘイビアに、指定された名前の読み取り可能なプロパティがある場合(
$checkBehaviors
が true の場合)。
関連項目: canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
プロパティ名 |
$checkVars | boolean |
メンバ変数をプロパティとして扱うかどうか |
$checkBehaviors | boolean |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか |
return | boolean |
プロパティが読み取り可能かどうか |
---|
public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canGetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
定義元: yii\base\Component::canSetProperty()
プロパティが設定可能かどうかを示す値を返します。
プロパティは、次の場合は書き込み可能です。
- クラスに指定された名前に関連付けられたセッターメソッドがある場合(この場合、プロパティ名は大小文字を区別しません)。
- クラスに指定された名前のメンバ変数がある場合(
$checkVars
が true の場合)。 - アタッチされたビヘイビアに、指定された名前の書き込み可能なプロパティがある場合(
$checkBehaviors
が true の場合)。
関連項目: canGetProperty()。
public boolean canSetProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
プロパティ名 |
$checkVars | boolean |
メンバ変数をプロパティとして扱うかどうか |
$checkBehaviors | boolean |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか |
return | boolean |
プロパティが書き込み可能かどうか |
---|
public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->canSetProperty($name, $checkVars)) {
return true;
}
}
}
return false;
}
::class
を使用してください。
定義元: yii\base\BaseObject::className()
このクラスの完全修飾名を返します。
public static string className ( ) | ||
return | string |
このクラスの完全修飾名。 |
---|
public static function className()
{
return get_called_class();
}
クライアント側のバリデーションを実行するために必要なJavaScriptを返します。
getClientOptions() を呼び出して、クライアントサイドバリデーション用のオプション配列を生成します。
バリデーターがクライアントサイドバリデーションをサポートできる場合は、このメソッドをオーバーライドして、JavaScriptバリデーションコードを返すことができます。
次の JavaScript 変数が定義済みで、バリデーションコードで使用できます
attribute
: バリデーションされる属性を記述するオブジェクト。value
: バリデーションされる値。messages
: 属性のバリデーションエラーメッセージを保持するために使用される配列。deferred
: 非同期バリデーション用の遅延オブジェクトを保持するために使用される配列$form
: フォーム要素を含む jQuery オブジェクト
attribute
オブジェクトには次のプロパティが含まれています
id
: フォーム内の属性を識別する一意の ID (例: "loginform-username")name
: 属性名または式 (例: 表形式の入力の場合は "[0]content")container
: 入力フィールドのコンテナの jQuery セレクターinput
: フォームのコンテキスト下での入力フィールドの jQuery セレクターerror
: コンテナのコンテキスト下でのエラータグの jQuery セレクターstatus
: 入力フィールドの状態、0: 空、以前に入力されていない、1: 検証済み、2: 検証保留中、3: 検証中
public string|null clientValidateAttribute ( $model, $attribute, $view ) | ||
$model | yii\base\Model |
バリデーションされるデータモデル |
$attribute | string |
バリデーションされる属性の名前。 |
$view | yii\web\View |
このバリデーターが適用されたモデルフォームを含むビューまたはビューファイルをレンダリングするために使用されるビューオブジェクト。 |
return | string|null |
クライアントサイドバリデーションスクリプト。バリデーターがクライアントサイドバリデーションをサポートしていない場合は Null。 |
---|
public function clientValidateAttribute($model, $attribute, $view)
{
ValidationAsset::register($view);
$options = $this->getClientOptions($model, $attribute);
return 'yii.validation.image(attribute, messages, ' . Json::htmlEncode($options) . ', deferred);';
}
定義元: yii\validators\Validator::createValidator()
バリデーターオブジェクトを作成します。
public static yii\validators\Validator createValidator ( $type, $model, $attributes, $params = [] ) | ||
$type | string|Closure |
バリデーターのタイプ。これは以下のいずれかです。
|
$model | yii\base\Model |
検証されるデータモデル。 |
$attributes | array|string |
検証される属性のリスト。これは属性名の配列、またはコンマ区切りの属性名の文字列のいずれかです。 |
$params | array |
バリデーターのプロパティに適用される初期値。 |
return | yii\validators\Validator |
バリデーター |
---|
public static function createValidator($type, $model, $attributes, $params = [])
{
$params['attributes'] = $attributes;
if ($type instanceof \Closure) {
$params['class'] = __NAMESPACE__ . '\InlineValidator';
$params['method'] = $type;
} elseif (!isset(static::$builtInValidators[$type]) && $model->hasMethod($type)) {
// method-based validator
$params['class'] = __NAMESPACE__ . '\InlineValidator';
$params['method'] = [$model, $type];
} else {
unset($params['current']);
if (isset(static::$builtInValidators[$type])) {
$type = static::$builtInValidators[$type];
}
if (is_array($type)) {
$params = array_merge($type, $params);
} else {
$params['class'] = $type;
}
}
return Yii::createObject($params);
}
定義元: yii\base\Component::detachBehavior()
コンポーネントからビヘイビアをデタッチします。
ビヘイビアのyii\base\Behavior::detach()メソッドが呼び出されます。
public yii\base\Behavior|null detachBehavior ( $name ) | ||
$name | string |
ビヘイビアの名前。 |
return | yii\base\Behavior|null |
デタッチされたビヘイビア。ビヘイビアが存在しない場合はNull。 |
---|
public function detachBehavior($name)
{
$this->ensureBehaviors();
if (isset($this->_behaviors[$name])) {
$behavior = $this->_behaviors[$name];
unset($this->_behaviors[$name]);
$behavior->detach();
return $behavior;
}
return null;
}
定義元: yii\base\Component::detachBehaviors()
コンポーネントからすべてのビヘイビアをデタッチします。
public void detachBehaviors ( ) |
public function detachBehaviors()
{
$this->ensureBehaviors();
foreach ($this->_behaviors as $name => $behavior) {
$this->detachBehavior($name);
}
}
定義元: yii\base\Component::ensureBehaviors()
behaviors()で宣言されたビヘイビアがこのコンポーネントにアタッチされていることを確認します。
public void ensureBehaviors ( ) |
public function ensureBehaviors()
{
if ($this->_behaviors === null) {
$this->_behaviors = [];
foreach ($this->behaviors() as $name => $behavior) {
$this->attachBehaviorInternal($name, $behavior);
}
}
}
定義元: yii\validators\Validator::formatMessage()
I18Nを使用してメッセージをフォーマットするか、\Yii::$app
が利用できない場合は単純なstrtrを使用します。
protected string formatMessage ( $message, $params ) | ||
$message | string | |
$params | array |
protected function formatMessage($message, $params)
{
if (Yii::$app !== null) {
return \Yii::$app->getI18n()->format($message, $params, Yii::$app->language);
}
$placeholders = [];
foreach ((array) $params as $name => $value) {
$placeholders['{' . $name . '}'] = $value;
}
return ($placeholders === []) ? $message : strtr($message, $placeholders);
}
定義元: yii\validators\Validator::getAttributeNames()
先頭の!
文字を除いたクリーンな属性名を返します。
public array getAttributeNames ( ) | ||
return | array |
属性名。 |
---|
public function getAttributeNames()
{
return array_map(function ($attribute) {
return ltrim($attribute, '!');
}, $this->attributes);
}
定義元: yii\base\Component::getBehavior()
名前付きのビヘイビアオブジェクトを返します。
public yii\base\Behavior|null getBehavior ( $name ) | ||
$name | string |
ビヘイビア名 |
return | yii\base\Behavior|null |
ビヘイビアオブジェクト。ビヘイビアが存在しない場合は null |
---|
public function getBehavior($name)
{
$this->ensureBehaviors();
return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}
定義元: yii\base\Component::getBehaviors()
このコンポーネントにアタッチされたすべてのビヘイビアを返します。
public yii\base\Behavior[] getBehaviors ( ) | ||
return | yii\base\Behavior[] |
このコンポーネントにアタッチされたビヘイビアのリスト |
---|
public function getBehaviors()
{
$this->ensureBehaviors();
return $this->_behaviors;
}
クライアント側のバリデーションオプションを返します。
このメソッドは通常、clientValidateAttribute()から呼び出されます。クライアント側の検証に渡されるオプションを変更するために、このメソッドをオーバーライドすることができます。
public array getClientOptions ( $model, $attribute ) | ||
$model | yii\base\Model |
検証されるモデル |
$attribute | string |
検証される属性名 |
return | array |
クライアント側の検証オプション |
---|
public function getClientOptions($model, $attribute)
{
$options = parent::getClientOptions($model, $attribute);
$label = $model->getAttributeLabel($attribute);
if ($this->notImage !== null) {
$options['notImage'] = $this->formatMessage($this->notImage, [
'attribute' => $label,
]);
}
if ($this->minWidth !== null) {
$options['minWidth'] = $this->minWidth;
$options['underWidth'] = $this->formatMessage($this->underWidth, [
'attribute' => $label,
'limit' => $this->minWidth,
]);
}
if ($this->maxWidth !== null) {
$options['maxWidth'] = $this->maxWidth;
$options['overWidth'] = $this->formatMessage($this->overWidth, [
'attribute' => $label,
'limit' => $this->maxWidth,
]);
}
if ($this->minHeight !== null) {
$options['minHeight'] = $this->minHeight;
$options['underHeight'] = $this->formatMessage($this->underHeight, [
'attribute' => $label,
'limit' => $this->minHeight,
]);
}
if ($this->maxHeight !== null) {
$options['maxHeight'] = $this->maxHeight;
$options['overHeight'] = $this->formatMessage($this->overHeight, [
'attribute' => $label,
'limit' => $this->maxHeight,
]);
}
return $options;
}
定義元: yii\validators\FileValidator::getMimeTypeByFile()
ファイルパスからMIMEタイプを取得します
protected string|null getMimeTypeByFile ( $filePath ) | ||
$filePath | string | |
throws | yii\base\InvalidConfigException |
---|
protected function getMimeTypeByFile($filePath)
{
return FileHelper::getMimeType($filePath);
}
定義元: yii\validators\FileValidator::getSizeLimit()
アップロードされたファイルに許可される最大サイズを返します。
これは4つの要因に基づいて決定されます。
- php.ini の 'upload_max_filesize'
- php.ini の 'post_max_size'
- 'MAX_FILE_SIZE' 隠しフィールド
- $maxSize
public integer getSizeLimit ( ) | ||
return | integer |
アップロードされたファイルのサイズ制限。 |
---|
public function getSizeLimit()
{
// Get the lowest between post_max_size and upload_max_filesize, log a warning if the first is < than the latter
$limit = $this->sizeToBytes(ini_get('upload_max_filesize'));
$postLimit = $this->sizeToBytes(ini_get('post_max_size'));
if ($postLimit > 0 && $postLimit < $limit) {
Yii::warning('PHP.ini\'s \'post_max_size\' is less than \'upload_max_filesize\'.', __METHOD__);
$limit = $postLimit;
}
if ($this->maxSize !== null && $limit > 0 && $this->maxSize < $limit) {
$limit = $this->maxSize;
}
if (isset($_POST['MAX_FILE_SIZE']) && $_POST['MAX_FILE_SIZE'] > 0 && $_POST['MAX_FILE_SIZE'] < $limit) {
$limit = (int) $_POST['MAX_FILE_SIZE'];
}
return $limit;
}
定義元: yii\validators\Validator::getValidationAttributes()
このバリデーターが適用される属性のリストを返します。
public array|null getValidationAttributes ( $attributes = null ) | ||
$attributes | array|string|null |
バリデーション対象の属性のリスト。
|
return | array|null |
属性名のリスト。 |
---|
public function getValidationAttributes($attributes = null)
{
if ($attributes === null) {
return $this->getAttributeNames();
}
if (is_scalar($attributes)) {
$attributes = [$attributes];
}
$newAttributes = [];
$attributeNames = $this->getAttributeNames();
foreach ($attributes as $attribute) {
// do not strict compare, otherwise int attributes would fail due to to string conversion in getAttributeNames() using ltrim().
if (in_array($attribute, $attributeNames, false)) {
$newAttributes[] = $attribute;
}
}
return $newAttributes;
}
定義元: yii\base\Component::hasEventHandlers()
名前付きのイベントにハンドラーがアタッチされているかどうかを示す値を返します。
public boolean hasEventHandlers ( $name ) | ||
$name | string |
イベント名 |
return | boolean |
イベントにアタッチされているハンドラーがあるかどうか。 |
---|
public function hasEventHandlers($name)
{
$this->ensureBehaviors();
if (!empty($this->_events[$name])) {
return true;
}
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
return true;
}
}
return Event::hasHandlers($this, $name);
}
定義元: yii\base\Component::hasMethod()
メソッドが定義されているかどうかを示す値を返します。
メソッドが定義されている場合、
- クラスに指定された名前のメソッドがある
- アタッチされたビヘイビアに指定された名前のメソッドがある (
$checkBehaviors
が true の場合)。
public boolean hasMethod ( $name, $checkBehaviors = true ) | ||
$name | string |
プロパティ名 |
$checkBehaviors | boolean |
ビヘイビアのメソッドをこのコンポーネントのメソッドとして扱うかどうか |
return | boolean |
メソッドが定義されているかどうか |
---|
public function hasMethod($name, $checkBehaviors = true)
{
if (method_exists($this, $name)) {
return true;
} elseif ($checkBehaviors) {
$this->ensureBehaviors();
foreach ($this->_behaviors as $behavior) {
if ($behavior->hasMethod($name)) {
return true;
}
}
}
return false;
}
定義元: yii\base\Component::hasProperty()
このコンポーネントに対してプロパティが定義されているかどうかを示す値を返します。
プロパティが定義されている場合、
- クラスに、指定された名前に関連付けられたゲッターまたはセッターメソッドがある (この場合、プロパティ名はケースインセンシティブです);
- クラスに指定された名前のメンバ変数がある場合(
$checkVars
が true の場合)。 - アタッチされたビヘイビアに指定された名前のプロパティがある (
$checkBehaviors
が true の場合)。
参考
public boolean hasProperty ( $name, $checkVars = true, $checkBehaviors = true ) | ||
$name | string |
プロパティ名 |
$checkVars | boolean |
メンバ変数をプロパティとして扱うかどうか |
$checkBehaviors | boolean |
ビヘイビアのプロパティをこのコンポーネントのプロパティとして扱うかどうか |
return | boolean |
プロパティが定義されているかどうか |
---|
public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
{
return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
}
オブジェクトを初期化します。
このメソッドは、与えられた構成でオブジェクトが初期化された後、コンストラクターの最後に呼び出されます。
public void init ( ) |
public function init()
{
parent::init();
if ($this->notImage === null) {
$this->notImage = Yii::t('yii', 'The file "{file}" is not an image.');
}
if ($this->underWidth === null) {
$this->underWidth = Yii::t('yii', 'The image "{file}" is too small. The width cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
}
if ($this->underHeight === null) {
$this->underHeight = Yii::t('yii', 'The image "{file}" is too small. The height cannot be smaller than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
}
if ($this->overWidth === null) {
$this->overWidth = Yii::t('yii', 'The image "{file}" is too large. The width cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
}
if ($this->overHeight === null) {
$this->overHeight = Yii::t('yii', 'The image "{file}" is too large. The height cannot be larger than {limit, number} {limit, plural, one{pixel} other{pixels}}.');
}
}
定義元: yii\validators\Validator::isActive()
指定されたシナリオおよび属性に対してバリデーターがアクティブかどうかを示す値を返します。
バリデーターがアクティブになるのは、
- バリデーターの
on
プロパティが空の場合、または - バリデーターの
on
プロパティに指定されたシナリオが含まれている場合
public boolean isActive ( $scenario ) | ||
$scenario | string |
シナリオ名 |
return | boolean |
バリデーターが指定されたシナリオに適用されるかどうか。 |
---|
public function isActive($scenario)
{
return !in_array($scenario, $this->except, true) && (empty($this->on) || in_array($scenario, $this->on, true));
}
定義元: yii\validators\FileValidator::isEmpty()
指定された値が空かどうかをチェックします。
値が null、空の配列、または空の文字列の場合、空とみなされます。このメソッドは PHP の empty() とは異なることに注意してください。値が 0 の場合は false を返します。
public boolean isEmpty ( $value, $trim = false ) | ||
$value | mixed |
チェックする値 |
$trim | boolean | |
return | boolean |
値が空かどうか |
---|
public function isEmpty($value, $trim = false)
{
$value = is_array($value) ? reset($value) : $value;
return !($value instanceof UploadedFile) || $value->error == UPLOAD_ERR_NO_FILE;
}
定義元: yii\base\Component::off()
このコンポーネントから既存のイベントハンドラーをデタッチします。
このメソッドは on() の反対です。
注意: イベント名にワイルドカードパターンが渡された場合、このワイルドカードで登録されたハンドラーのみが削除され、このワイルドカードに一致するプレーン名で登録されたハンドラーは残ります。
参考 on()。
public boolean off ( $name, $handler = null ) | ||
$name | string |
イベント名 |
$handler | callable|null |
削除するイベントハンドラー。null の場合、指定された名前のイベントにアタッチされているすべてのハンドラーが削除されます。 |
return | boolean |
ハンドラーが見つかり、デタッチされた場合 |
---|
public function off($name, $handler = null)
{
$this->ensureBehaviors();
if (empty($this->_events[$name]) && empty($this->_eventWildcards[$name])) {
return false;
}
if ($handler === null) {
unset($this->_events[$name], $this->_eventWildcards[$name]);
return true;
}
$removed = false;
// plain event names
if (isset($this->_events[$name])) {
foreach ($this->_events[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_events[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_events[$name] = array_values($this->_events[$name]);
return true;
}
}
// wildcard event names
if (isset($this->_eventWildcards[$name])) {
foreach ($this->_eventWildcards[$name] as $i => $event) {
if ($event[0] === $handler) {
unset($this->_eventWildcards[$name][$i]);
$removed = true;
}
}
if ($removed) {
$this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
// remove empty wildcards to save future redundant regex checks:
if (empty($this->_eventWildcards[$name])) {
unset($this->_eventWildcards[$name]);
}
}
}
return $removed;
}
イベントにイベントハンドラーをアタッチします。
イベントハンドラーは有効な PHP コールバックである必要があります。以下にいくつかの例を示します
function ($event) { ... } // anonymous function
[$object, 'handleClick'] // $object->handleClick()
['Page', 'handleClick'] // Page::handleClick()
'handleClick' // global function handleClick()
イベントハンドラーは、次のシグネチャで定義する必要があります。
function ($event)
ここで、$event
は、イベントに関連付けられたパラメーターを含む yii\base\Event オブジェクトです。
2.0.14 以降、イベント名をワイルドカードパターンとして指定できます
$component->on('event.group.*', function ($event) {
Yii::trace($event->name . ' is triggered.');
});
参考 off()。
public void on ( $name, $handler, $data = null, $append = true ) | ||
$name | string |
イベント名 |
$handler | callable |
イベントハンドラー |
$data | mixed |
イベントがトリガーされたときにイベントハンドラーに渡されるデータ。イベントハンドラーが呼び出されたとき、このデータは yii\base\Event::$data を介してアクセスできます。 |
$append | boolean |
新しいイベントハンドラーを既存のハンドラーリストの最後に追加するかどうか。false の場合、新しいハンドラーは既存のハンドラーリストの先頭に挿入されます。 |
public function on($name, $handler, $data = null, $append = true)
{
$this->ensureBehaviors();
if (strpos($name, '*') !== false) {
if ($append || empty($this->_eventWildcards[$name])) {
$this->_eventWildcards[$name][] = [$handler, $data];
} else {
array_unshift($this->_eventWildcards[$name], [$handler, $data]);
}
return;
}
if ($append || empty($this->_events[$name])) {
$this->_events[$name][] = [$handler, $data];
} else {
array_unshift($this->_events[$name], [$handler, $data]);
}
}
定義元: yii\base\Component::trigger()
イベントをトリガーします。
このメソッドは、イベントの発生を表します。クラスレベルのハンドラーを含め、イベントにアタッチされているすべてのハンドラーを呼び出します。
public void trigger ( $name, yii\base\Event $event = null ) | ||
$name | string |
イベント名 |
$event | yii\base\Event|null |
イベントインスタンス。設定されていない場合は、デフォルトのyii\base\Eventオブジェクトが作成されます。 |
public function trigger($name, Event $event = null)
{
$this->ensureBehaviors();
$eventHandlers = [];
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (StringHelper::matchWildcard($wildcard, $name)) {
$eventHandlers[] = $handlers;
}
}
if (!empty($this->_events[$name])) {
$eventHandlers[] = $this->_events[$name];
}
if (!empty($eventHandlers)) {
$eventHandlers = call_user_func_array('array_merge', $eventHandlers);
if ($event === null) {
$event = new Event();
}
if ($event->sender === null) {
$event->sender = $this;
}
$event->handled = false;
$event->name = $name;
foreach ($eventHandlers as $handler) {
$event->data = $handler[1];
call_user_func($handler[0], $event);
// stop further handling if the event is handled
if ($event->handled) {
return;
}
}
}
// invoke class-level attached handlers
Event::trigger($this, $name, $event);
}
public boolean validate ( $value, &$error = null ) | ||
$value | mixed |
検証するデータ値。 |
$error | string|null |
検証に失敗した場合に返されるエラーメッセージ。 |
return | boolean |
データが有効かどうか。 |
---|
public function validate($value, &$error = null)
{
$result = $this->validateValue($value);
if (empty($result)) {
return true;
}
list($message, $params) = $result;
$params['attribute'] = Yii::t('yii', 'the input value');
if (is_array($value)) {
$params['value'] = 'array()';
} elseif (is_object($value)) {
$params['value'] = 'object';
} else {
$params['value'] = $value;
}
$error = $this->formatMessage($message, $params);
return false;
}
定義元: yii\validators\FileValidator::validateAttribute()
単一の属性を検証します。
子クラスは、実際の検証ロジックを提供するためにこのメソッドを実装する必要があります。
public void validateAttribute ( $model, $attribute ) | ||
$model | yii\base\Model |
検証するデータモデル。 |
$attribute | string |
バリデーションされる属性の名前。 |
public function validateAttribute($model, $attribute)
{
if ($this->maxFiles != 1 || $this->minFiles > 1) {
$rawFiles = $model->$attribute;
if (!is_array($rawFiles)) {
$this->addError($model, $attribute, $this->uploadRequired);
return;
}
$files = $this->filterFiles($rawFiles);
$model->$attribute = $files;
if (empty($files)) {
$this->addError($model, $attribute, $this->uploadRequired);
return;
}
$filesCount = count($files);
if ($this->maxFiles && $filesCount > $this->maxFiles) {
$this->addError($model, $attribute, $this->tooMany, ['limit' => $this->maxFiles]);
}
if ($this->minFiles && $this->minFiles > $filesCount) {
$this->addError($model, $attribute, $this->tooFew, ['limit' => $this->minFiles]);
}
foreach ($files as $file) {
$result = $this->validateValue($file);
if (!empty($result)) {
$this->addError($model, $attribute, $result[0], $result[1]);
}
}
} else {
$result = $this->validateValue($model->$attribute);
if (!empty($result)) {
$this->addError($model, $attribute, $result[0], $result[1]);
}
}
}
定義元: yii\validators\Validator::validateAttributes()
指定されたオブジェクトを検証します。
public void validateAttributes ( $model, $attributes = null ) | ||
$model | yii\base\Model |
バリデーションされるデータモデル |
$attributes | array|string|null |
検証する属性のリスト。属性がバリデーターに関連付けられていない場合、無視されることに注意してください。このパラメーターがnullの場合、$attributesにリストされているすべての属性が検証されます。 |
public function validateAttributes($model, $attributes = null)
{
$attributes = $this->getValidationAttributes($attributes);
foreach ($attributes as $attribute) {
$skip = $this->skipOnError && $model->hasErrors($attribute)
|| $this->skipOnEmpty && $this->isEmpty($model->$attribute);
if (!$skip) {
if ($this->when === null || call_user_func($this->when, $model, $attribute)) {
$this->validateAttribute($model, $attribute);
}
}
}
}
定義元: yii\validators\FileValidator::validateExtension()
指定されたアップロード済みファイルの型(拡張子)が現在のバリデーター設定に対して正しいかどうかをチェックします。
protected boolean validateExtension ( $file ) | ||
$file | yii\web\UploadedFile |
protected function validateExtension($file)
{
$extension = mb_strtolower($file->extension, 'UTF-8');
if ($this->checkExtensionByMimeType) {
$mimeType = FileHelper::getMimeType($file->tempName, null, false);
if ($mimeType === null) {
return false;
}
$extensionsByMimeType = FileHelper::getExtensionsByMimeType($mimeType);
if (!in_array($extension, $extensionsByMimeType, true)) {
return false;
}
}
if (!empty($this->extensions)) {
foreach ((array) $this->extensions as $ext) {
if ($extension === $ext || StringHelper::endsWith($file->name, ".$ext", false)) {
return true;
}
}
return false;
}
return true;
}
画像ファイルを検証します。
protected array|null validateImage ( $image ) | ||
$image | yii\web\UploadedFile |
一連のルールに対してチェックするために渡されるアップロードされたファイル |
return | array|null |
エラーメッセージと、エラーメッセージに挿入されるパラメーター。データが有効な場合はNullを返す必要があります。 |
---|
protected function validateImage($image)
{
if (false === ($imageInfo = getimagesize($image->tempName))) {
return [$this->notImage, ['file' => $image->name]];
}
list($width, $height) = $imageInfo;
if ($width == 0 || $height == 0) {
return [$this->notImage, ['file' => $image->name]];
}
if ($this->minWidth !== null && $width < $this->minWidth) {
return [$this->underWidth, ['file' => $image->name, 'limit' => $this->minWidth]];
}
if ($this->minHeight !== null && $height < $this->minHeight) {
return [$this->underHeight, ['file' => $image->name, 'limit' => $this->minHeight]];
}
if ($this->maxWidth !== null && $width > $this->maxWidth) {
return [$this->overWidth, ['file' => $image->name, 'limit' => $this->maxWidth]];
}
if ($this->maxHeight !== null && $height > $this->maxHeight) {
return [$this->overHeight, ['file' => $image->name, 'limit' => $this->maxHeight]];
}
return null;
}
定義元: yii\validators\FileValidator::validateMimeType()
$mimeTypesプロパティのリストに対して$fileのmimeTypeをチェックします。
$mimeTypesも参照してください。
protected boolean validateMimeType ( $file ) | ||
$file | yii\web\UploadedFile | |
return | boolean |
$fileのmimeTypeが許可されているかどうか。 |
---|---|---|
throws | yii\base\InvalidConfigException |
protected function validateMimeType($file)
{
$fileMimeType = $this->getMimeTypeByFile($file->tempName);
if ($fileMimeType === null) {
return false;
}
foreach ($this->mimeTypes as $mimeType) {
if (strcasecmp($mimeType, $fileMimeType) === 0) {
return true;
}
if (strpos($mimeType, '*') !== false && preg_match($this->buildMimeTypeRegexp($mimeType), $fileMimeType)) {
return true;
}
}
return false;
}
値を検証します。
バリデータークラスは、データモデルのコンテキスト外でのデータ検証をサポートするために、このメソッドを実装できます。
protected array|null validateValue ( $value ) | ||
$value | mixed |
検証するデータ値。 |
return | array|null |
エラーメッセージと、エラーメッセージに挿入されるパラメーターの配列。
} return null; データが有効な場合はNullを返す必要があります。 |
---|---|---|
throws | yii\base\NotSupportedException |
バリデーターがモデルなしのデータ検証をサポートしていない場合 |
protected function validateValue($value)
{
$result = parent::validateValue($value);
return empty($result) ? $this->validateImage($value) : $result;
}
コメントするには、サインアップまたはログインしてください。