0

I have a form set up with a file upload for profile pictures, and I'm validating the file upload like so:

'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'
    )
)

The validation runs, and works, however no validation errors are shown on the form when I submit incorrect data. I'm building the entire form like so:

<?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'); ?>

The other fields error messages get shown correctly, just not the file upload. How can I make the validation errors show up around the file input?

5
  • 'picture' is defined as an array for <?php echo $this->Form->file('picture'); ?> but not size. Far as I can tell. Commented Mar 20, 2013 at 23:38
  • I don't understand what you're saying? Commented Mar 20, 2013 at 23:42
  • Your 'size' is not being echo'ed anywhere, as compared to 'picture', 'firstname' etc. Isn't the error message for the file upload 'message' => 'Picture must be less than 2 MB'? Commented Mar 20, 2013 at 23:48
  • Maybe you need to add <?php echo $this->Form->file('size'); ?> somewhere. Then again, I may be wrong. Commented Mar 20, 2013 at 23:57
  • show validate action for 'validateImage' Commented Mar 21, 2013 at 7:52

1 Answer 1

1

You have used $this->Form->file('picture'); which will only generate the file upload field nothing else.

Either add a $this->Form->error('picture'); below it which will show the validation error message or use $this->Form->input('picture', array('type' => 'file')); to generate the file upload input also.

Form::input() is a wrapper method which generates the required input field, label tag and shows validation error message if any. Please read the manual.

Sign up to request clarification or add additional context in comments.

2 Comments

You'll probably have to specify the type of input to use for the 'picture' field, because the CakePHP FormHelper will not automatically create file inputs; i.e. echo $this->Form->input('picture', array('type' => 'file'));
Right, forgot to mention this. Need to specify the 'type' => 'file' as it cannot be automatically inferred. I updated my original answer accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.