James Dawson
James Dawson

Reputation: 5409

Showing validation errors for specific input field

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?

Upvotes: 0

Views: 259

Answers (1)

ADmad
ADmad

Reputation: 8100

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.

Upvotes: 1

Related Questions