mediaãã©ã°ã¤ã³å¿ç¨(3) ãã¡ã¤ã«ãã§ãã¯
ã¢ãããã¼ããããã¡ã¤ã«ã®ç¨®é¡ããµã¤ãºããã§ãã¯ããè¨å®ã追å ãã¾ããã
端çã«è¨ãã¨ãããªãã¼ã·ã§ã³ã§ãã
åºæ¬çã«ãããªã«é£ãããªãã®ã§ããã$fieldListæå®ã追å ãã¦ããããã«ãã¡ãã£ã¨ãããã¾ããã»ã»ã»
Mediaãã©ã°ã¤ã³ã§validationãåç´ã«å
¥ãããã¨æã£ããã以ä¸ã®è³æã®18ãã¼ã¸ç®ãé常ã«ãããããããµã³ãã«ã«ãªã£ã¦ãã¾ãã
The_CakePHP_Media_Plugin.pdf
ã¾ãã¯media/models/attachment.phpã«ã¯åºæ¬çãª$validateè¨å®ãæ¢ã«è¨å®ãã¦ããã®ã§ããããè¦ãã ãã§å¤§ä½ã¤ããã¾ãã
ãããã®è¨å®ã®ãã¡ãå¿ è¦ãªãã®ãAttachmentã®æ¡å¼µã¢ãã«ããã³controllerã§å¤æ´ãã¾ããã
app/plugins/media/models/attachment_ex.php
@@ -9,8 +9,17 @@ class AttachmentEx extends Attachment { function __construct($id = false, $table = null, $ds = null) { $this->actsAs['Media.Transfer']['destinationFile'] = ':Medium.short::DS::uuid:'.'.'.':Source.extension:'; + // validate + $this->validate['file']['extension'] = array('rule' => array('checkExtension', false, array( + 'jpg', 'jpeg', 'png', 'gif', + ))); + $this->validate['file']['notEmpty'] = array('rule' => array('checkMimeType', false, array( + 'image/jpeg', 'image/png', 'image/gif' + ))); + parent::__construct($id, $table, $ds); }
app/controllers/users_controller.php
@@ -91,6 +91,24 @@ class UsersController extends ModuleController { +// validate +// ãã¡ã¤ã«å®¹é +$filesize = Configure::read('Upload.imagefile.FileSizeMax'); +if (empty($filesize) || $filesize <= 0) { + unset($this->User->Attachment->validate['file']['size']); +} else { + $this->User->Attachment->validate['file']['size']['rule'] = array('checkSize', $filesize.'K' +} +//ãã¡ã¤ã«ãµã¤ãº +$width = Configure::read('Upload.imagefile.WidthMax'); +$height = Configure::read('Upload.imagefile.HeightMax'); +if ((empty($width) || $width <= 0) || (empty($height) || $height <= 0)) { + unset($this->User->Attachment->validate['file']['pixels']); +} else { + $this->User->Attachment->validate['file']['pixels']['rule'] = array('checkPixels', $width.'x +}
ãã ãæåAttachmentã§fileã«é¢ããå
¨ã¦ã®ããªãã¼ã·ã§ã³ãå¹ããªãç¶æ
ã§ããã
saveAll()ã®$fieldListè¨å®ã§ãDBã®é
ç®ã«ã¯ãªãããã¨ãfileãæå®ãã¦ããªãã£ãã®ãåå ã§ããã
$fieldListã¯ãé ç®ãã®ãã®ãDBã®ãã¼ãã«ã«ãªãã¦ããvalidateã®å¯¾è±¡ã«ãªãå ´åã«ã¯å¿ è¦ã§ãã
cake/libs/model/model.php
function invalidFields($options = array()) { (ä¸ç¥ï¼ $whitelist = $this->whitelist; if (array_key_exists('fieldList', $options)) { $whitelist = $options['fieldList'];// â$fieldListã«è¨å®æ¸ã¿ã®é ç®ã®ã¿$whilelist㸠} if (!empty($whitelist)) { $validate = array(); foreach ((array)$whitelist as $f) { // $whitelistã®é ç®ã®ã¿validate if (!empty($this->validate[$f])) { $validate[$f] = $this->validate[$f]; } } }