0

我有一个为个人资料图片上传文件的表单,我正在验证文件上传,如下所示:

'picture' => array(
    'kosher' => array(
        'rule' => 'validateImage',
        'message' => 'Only images are allowed to be uploaded'
    ),
    'size' => array(
        'rule' => array('fileSize', '<=', '2MB'),
        'message' => 'Picture must be less than 2 MB'
    )
)

验证运行,并且有效,但是当我提交不正确的数据时,表单上没有显示验证错误。我正在构建整个表单,如下所示:

<?php echo $this->Form->create('Profile', array('type' => 'file')); ?>
<?php echo $this->Form->hidden('id'); ?>
<?php echo $this->Form->label('Profile.picture', 'Profile picture'); ?>
<?php echo $this->Form->file('picture'); ?>
<?php echo $this->Form->input('firstname'); ?>
<?php echo $this->Form->input('lastname'); ?>
<?php echo $this->Form->input('email', array('between' => 'This is the email other users will use to contact you')); ?>
<?php echo $this->Form->input('Skill', array('type' => 'text', 'value' => $skills, 'label' => 'Your skills (seperate each skill by space)')); ?>
<?php echo $this->Form->input('course'); ?>
<?php echo $this->Form->input('bio'); ?>
<?php echo $this->Form->end('Save changes'); ?>

其他字段错误消息正确显示,而不是文件上传。如何使验证错误显示在文件输入周围?

4

1 回答 1

1

您已经使用$this->Form->file('picture');了它只会生成文件上传字段。

在其下方添加一个$this->Form->error('picture');将显示验证错误消息或用于$this->Form->input('picture', array('type' => 'file'));生成文件上传输入的内容。

Form::input()是一种包装方法,它生成所需的输入字段、标签标签并显示验证错误消息(如果有)。请阅读手册。

于 2013-03-21T14:35:25.410 回答