ファイルアップロードとバリデーション

拡張子によるバリデーション

  • Model
<?php


    public $validate = array(
        'file' => array(
            'extension' => array(
                'rule' => array(
                    'extension',
                    array('jpg', 'jpg', 'pdf'),
                ),
                'message' =>'ファイルのタイプが違います'
            ),
        ),
    );
  • View
    • typeにはfileを指定
<?php

echo $this->Form->create(
    'モデル名',
    array(
        'type' => 'file',
        'url' => array(
            'controller' => 'コントローラ名',
            'action' => 'アクション名', パラメータ)
        )
    );
?>


  • Controller
<?php

$file = $this->request->data['モデル名'];
$this->モデル名->set($file);
$this->モデル名->validates();

動的なバリデーション