Client Side Validation Of File

Hello,

Adding client side validation to FileValidator will be a nice as errors are always displayed after form is submitted and file is uploaded.

What did you want to check client side?

There’s no way to technically achieve it. File should be uploaed in order to be validated.

The only thing you can check client side with the current validation system is file size and file extension (Will only work in HTML5 supported browser too).

it’s possible! by adding HTML options on code generation parameter

HTML BASICS (this is what yii generated code should look like)

typical file upload on HTML

<input name="file]" type="file">

to add clientside filter you do set the ‘accept’ HTML property

<input accept="image/jpeg" type="file">

TYPICAL YII FILE UPLOAD FIELD

in Yii typical file upload would look like this

<?php echo CHtml::activeFileField($avatmodel, ‘image’)?>

this would generate

<input name="avatar[image]" id="avatar_image" type="file">

YII FILE UPLOAD FIELD WITH CLIENT SIDE FILE FILTER

to add clientside filter you add HTML options array

<?php echo CHtml::activeFileField($avatmodel, ‘image’,array(‘accept’=>‘image/jpeg’))?>

this would generate

<input accept="image/jpeg" name="avatar[image]" id="avatar_image" type="file">

ADDITIONAL INFO

the HTML options array also supports generally all HTML options and client side events, such as ‘style’ ‘onclick’ etc etc etc

<?php echo CHtml::activeFileField($avatmodel, ‘image’,array(‘accept’=>‘image/jpeg’, ‘style’=>‘width:100%;’))?>

would generate

<input accept="image/jpeg" style="width:100%;" name="avatar[image]" id="avatar_image" type="file">