Yii Framework Forum: File Input - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

File Input Rate Topic: -----

#1 User is offline   ilikeme 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 11-August 11

Posted 21 November 2012 - 03:08 PM

when I choose file and send the form it show me
"Image cannot be blank."
Even that i choose image

this is the form Model
<?php

class AdminNewCatgory extends CFormModel
{
    public $name, $image;
    private $_identity;
    
    public function attributeLabels()
    {
        return array(
        );
    }
    
    public function rules()
    {
        return array(
            array('name', 'length', 'allowEmpty' => false, 'min'=>4, 'max'=>30),
            array(
                'image', 'file',
                'maxSize'    => 2097152,
                'types'      => array('jpg','jpeg','png','gif','bmp'),
            )
        );
    }
}
?>


the action in controller
public function actionCatgory($id=0)
{
    if(!Admin::IsLogin())
        throw new CHttpException(404);

    if($id == 0)
    {
        // choose catgory
        $catgories = Catgory::model()->findAll(array('order'=>'id DESC'));
        $model = new AdminNewCatgory();

        if(isset($_POST['AdminNewCatgory']))
        {
            $model->attributes = $_POST['AdminNewCatgory'];
            if($model->validate())
            {

            }
        }

        $this->render('catgory_select+new',array(
            'catgories'=>$catgories,
            'model'=>$model
        ));
    }
    else
    {

    }
}


this is the view
<?php $form = $this->beginWidget('CActiveForm'); ?>
    <div style="font-size:12px">
        <?php echo $form->errorSummary($model,'<span style="font-weight:bold;">אנא תקן את השגיאות הבאות:</span>'); ?>
    </div>

    <br />

    <b><?php echo $form->labelEx($model, 'name'); ?></b>
    <?php echo $form->textField($model, 'name'); ?>
    
    <br />
    
    <b><?php echo $form->labelEx($model, 'image'); ?> <span style="color:#ff0000;font-size:10px">(JPG,JPEG,PNG,GIF,BMP)(גודל מקסימלי 2MB)</span></b>
    <br />
    <?php echo $form->fileField($model, 'image'); ?>
    
    <br /><br />
    
    <?php echo CHtml::submitButton('צור קולקציה'); ?>
<?php $this->endWidget(); ?>

thanks for help ;)
0

#2 User is offline   notsoluckycharm 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 15-February 11

Posted 21 November 2012 - 03:43 PM

View Postilikeme, on 21 November 2012 - 03:08 PM, said:

when I choose file and send the form it show me
"Image cannot be blank."
Even that i choose image

this is the form Model
<?php

class AdminNewCatgory extends CFormModel
{
    public $name, $image;
    private $_identity;
    
    public function attributeLabels()
    {
        return array(
        );
    }
    
    public function rules()
    {
        return array(
            array('name', 'length', 'allowEmpty' => false, 'min'=>4, 'max'=>30),
            array(
                'image', 'file',
                'maxSize'    => 2097152,
                'types'      => array('jpg','jpeg','png','gif','bmp'),
            )
        );
    }
}
?>


the action in controller
public function actionCatgory($id=0)
{
    if(!Admin::IsLogin())
        throw new CHttpException(404);

    if($id == 0)
    {
        // choose catgory
        $catgories = Catgory::model()->findAll(array('order'=>'id DESC'));
        $model = new AdminNewCatgory();

        if(isset($_POST['AdminNewCatgory']))
        {
            $model->attributes = $_POST['AdminNewCatgory'];
            if($model->validate())
            {

            }
        }

        $this->render('catgory_select+new',array(
            'catgories'=>$catgories,
            'model'=>$model
        ));
    }
    else
    {

    }
}


this is the view
<?php $form = $this->beginWidget('CActiveForm'); ?>
    <div style="font-size:12px">
        <?php echo $form->errorSummary($model,'<span style="font-weight:bold;">אנא תקן את השגיאות הבאות:</span>'); ?>
    </div>

    <br />

    <b><?php echo $form->labelEx($model, 'name'); ?></b>
    <?php echo $form->textField($model, 'name'); ?>
    
    <br />
    
    <b><?php echo $form->labelEx($model, 'image'); ?> <span style="color:#ff0000;font-size:10px">(JPG,JPEG,PNG,GIF,BMP)(גודל מקסימלי 2MB)</span></b>
    <br />
    <?php echo $form->fileField($model, 'image'); ?>
    
    <br /><br />
    
    <?php echo CHtml::submitButton('צור קולקציה'); ?>
<?php $this->endWidget(); ?>

thanks for help ;)


You need to add multipart/encryption

//yours 
<?php $form = $this->beginWidget('CActiveForm'); ?>

should be: 

$form = $this->beginWidget(
    'CActiveForm',
    array(
        'id' => 'YOU-SHOULD-ID-IT',
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
    )
);


To submit a file via $_POST
0

#3 User is offline   ilikeme 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 21
  • Joined: 11-August 11

Posted 21 November 2012 - 04:16 PM

View Postnotsoluckycharm, on 21 November 2012 - 03:43 PM, said:

You need to add multipart/encryption

//yours 
<?php $form = $this->beginWidget('CActiveForm'); ?>

should be: 

$form = $this->beginWidget(
    'CActiveForm',
    array(
        'id' => 'YOU-SHOULD-ID-IT',
        'htmlOptions' => array('enctype' => 'multipart/form-data'),
    )
);


To submit a file via $_POST


first of all, thanks for your commment ;)
second thing where does I put the variable $model?

edit: never mind, thank you very much ;)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users