Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9274315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:19:32+00:00 2026-06-18T16:19:32+00:00

I’ve been working on Yii-based application. And I’ve faced with weird thing… What I’m

  • 0

I’ve been working on Yii-based application. And I’ve faced with weird thing…

What I’m trying to do is to add input(type=file) into form. Form is created via form builder(CForm class). But input is not going to appear.

My code. Controller/action:

$model=MyModel::model()->findByPk(700);
$model->scenario='my-scenario';
$form=new CForm('path.to.forms.my-form', $model);

$this->render('view', array('form'=>$form));

View:

echo $form

Form config:

return array(
    'attributes' => array(
        'enctype' => 'multipart/form-data',
    ),
    'elements' => array(
        'name' => array(
            'type' => 'text',
        ),
        'image' => array(
            'type' => 'file',
        ),
    ),
    'buttons' => array(
        'save' => array(
            'type' => 'submit',
            'label' => 'Save',
        ),
    ),
);

Model:

//.....
public $image;
// ....
public function rules()
{
    return array(
    //...
        array('image', 'file', 'types'=>'png', 'on'=>'my-scenario'),

    );
}

With code above I expected to see two fields – text and file. But only text one appears.

If I change file validator to, say, required – it works, but I need file validator.

I’m using Yii version 1.1.13.

The most intresting that code above works as expected with earlier Yii(1.1.9). Is this a known bug in new version? If yes – is there a solution? or do I have to rollback to previous version?

Thanks in advance.

UPDATE:

If you add a second validator (one for file and one for required) does it work?

No, it doesn’t. I believe I found why. See bellow.

It seems to be caused by this line in CForm..

Yes, correct. Yesterday, armed with debugger I went deeper:)

CFormElement::getVisible() eventually calls CModel::isAttributeSafe() and CModel::getSafeAttributeNames().

CForm::getSafeAttributeNames() gets all model validators and leaves only safe ones. As we can see CFileValidator is not safe.

So, it doesn’t matter how many safe validators(required or any other) have attribute assigned. CForm::getSafeAttributeNames() removes it from whitelist if there is at least one unsafe(file).
File validator is unsafe since 1.1.12 version. That is why it worked perfectly for me in 1.1.9 🙂

Hence the problem is in CFileValidator(or at least connected with it) and not in CForm.

The only one solution I can see so far is creating own validator extended from CFileValidator marked safe and using it instead of built in. But I can’t even imagine what problems it may cause(I believe Yii developers had a good reason for making it unsafe).

I hope this will be helpful for somebody.

UPDATE 2

array('image', 'file', 'safe'=>true, 'types'=>'png', 'on'=>'my-scenario')

this validation rule(explicit safe=true) also works.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T16:19:33+00:00Added an answer on June 18, 2026 at 4:19 pm

    Issue has been posted on github

    https://github.com/yiisoft/yii/issues/2089

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to render a haml file in a javascript response like so:
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I am trying to understand how to use SyndicationItem to display feed which is
I have a small JavaScript validation script that validates inputs based on Regex. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.