cakephp-upload不会验证

时间:2015-07-24 14:20:28

标签: php validation cakephp

我正在使用插件 https://github.com/josegonzalez/cakephp-upload 用于图像上传。 我不会禁用添加帖子而不上传图片。 如何写验证?

class BlogContent extends AppModel
{

public $validate = array(
    'title' => array(
        'rule' => 'notEmpty',
        'message' => 'Titulok musí byť zadaný'
    ),
    'text' => array(
        'rule' => 'notEmpty',
        'required' => true,
        'message' => 'Text musí byť zadaný'
    ),
    'photo' => array(
        'isUpload' => array(
            'rule' => 'isFileUpload',
            'required' => true,
            'message' => 'Článok musí obsahovať obrázok',
             'last' => false
        ),
        'isCompletedUpload' => array(
            'rule' => 'isCompletedUpload',
            'message' => 'Obrázok nebol úspešne nahratý',
            'last' => false
        ),
        'tempDirExists' => array(
            'rule' => 'tempDirExists',
            'message' => 'Priečinok TEMP nie je dostupný',
            'last' => false
        ),
...

如果我将required=> true用于isUpload,它将永远无法验证。但是,如果我没有输入它,它将在不上传图像的情况下进行验证。

查看代码:admin_add.ctp

<div class="blogContents form">
    <?php echo $this->Form->create('BlogContent', array('type' => 'file'));
    echo $this->Form->input('title', array('label' => 'Titulok'));
    echo $this->Form->input('text', array('class' => 'ckeditor', 'label' => ''));
    echo $this->Form->input('image', array('label' => '', 'type' => 'file', 'accept' => 'image/*'));
    echo $this->Html->link(__('Vybrať obrázok'), '#', array('class' => 'btn btn-default', 'id' => 'upload'));
    echo _('<p>Zvolený obrázok:</p>');
    echo $this->Html->div('filelist', '<p>Žiaden obrázok nebol zvolený</p>');
    echo $this->Form->input('image_dir', array('type' => 'hidden'));
    echo $this->Form->input('active', array('label' => 'Publikované'));
    echo $this->Form->submit(__('Uložiť článok'), array('class' => 'btn btn-success')) ?>
</div>

0 个答案:

没有答案