Hello,
I have some troubles to get the file field working in a CActive form. Here is the script :
model :
public function rules() {
return array(
array('firstname, lastname, mail, notification, userid', 'required', 'message'=>Yii::t('common', 'form.field.required'), 'on'=>'register'),
array('userid, firstname, lastname, mail, notification', 'safe', 'on'=>'search'),
array('mail', 'email', 'message'=>Yii::t('common', 'form.field.email')),
array('file', 'file', 'types'=>'jpg, gif, png', 'allowEmpty'=>true, 'message'=>Yii::t('common', 'form.field.email')),
array('avatar, notification', 'default', 'setOnEmpty' => true, 'value' => null),
array('userid, firstname, lastname, mail', 'length', 'max'=>128),
array('notification', 'numerical', 'integerOnly'=>true),
);
}
Controller :
public function actionRegister()
{
//Create the models
$user_model = new User('register');
$profile_model = new Profile('register');
if(isset($_POST['User']) && $_POST['Profile'])
{
//Massive assigments from $_POST
$user_model->attributes=$_POST['User'];
$profile_model->attributes=$_POST['Profile'];
$profile_model->userid=$_POST['User']['username'];
$profile_model->file=CUploadedFile::getInstance($profile_model,'file');
//Check if the models are valided
if($user_model->validate() && $profile_model->validate())
{
//Then save
$user_model->save();
$profile_model->save();
$this->refresh();
}
}
//Render the wiew with the models as params
$this->render('register', array(
'user_model'=>$user_model,
'profile_model'=>$profile_model,
));
}
and view :
<?php
$user_form = $this->beginWidget('CActiveForm',array(
'id'=>'user-form',
'clientOptions' => array(
'focus'=>'#'.CHtml::activeId($user_model,'username'),
),
'htmlOptions'=>array(
'enctype' => 'multipart/form-data',
),
));
?>
<div class="control-group">
<?php echo $user_form->textField($user_model,'username', array('placeholder'=>$user_model->getAttributeLabel('username'))); ?>
<?php $user_form->error($user_model,'username'); ?>
</div>
<div class="control-group">
<?php echo $user_form->passwordField($user_model,'password', array('placeholder'=>$user_model->getAttributeLabel('password'))); ?>
<?php $user_form->error($user_model,'password'); ?>
</div>
<div class="control-group">
<?php echo $user_form->passwordField($user_model,'repeat_password', array('placeholder'=>$user_model->getAttributeLabel('repeat_password'))); ?>
<?php $user_form->error($user_model,'repeat_password'); ?>
</div>
<div class="control-group">
<?php echo $user_form->textField($profile_model,'firstname', array('placeholder'=>$profile_model->getAttributeLabel('firstname'))); ?>
<?php $user_form->error($profile_model,'firstname'); ?>
</div>
<div class="control-group">
<?php echo $user_form->textField($profile_model,'lastname', array('placeholder'=>$profile_model->getAttributeLabel('lastname'))); ?>
<?php $user_form->error($profile_model,'lastname'); ?>
</div>
<div class="control-group">
<?php echo $user_form->textField($profile_model,'mail', array('placeholder'=>$profile_model->getAttributeLabel('mail'))); ?>
<?php $user_form->error($profile_model,'mail'); ?>
</div>
<div class="control-group">
<label class="checkbox">
<?php echo $user_form->checkBox($profile_model,'notification'); ?> <?php echo $profile_model->getAttributeLabel('notification'); ?>
</label>
</div>
<div class="control-group">
<?php echo $user_form->fileField($profile_model,'file', array('class'=>'input-file')); ?>
<?php echo $user_form->error($profile_model,'file'); ?>
</div>
<?php echo CHtml::submitButton(Yii::t('common', 'common.save'), array('class'=>'btn btn-primary')) ?>
<?php echo CHtml::link(Yii::t('common', 'common.back'), Yii::app()->getRequest()->urlReferrer, array('class'=>'btn btn-warning')) ?>
<?php $this->endWidget(); ?>
-
When I try to send an image, there is no problem.
-
When I try to send a txt file, there is no problem, the error message is displayed
-
When I try to send nothing, there is no problem
- When I try to send any video file, the browser is turning again and again but nothing happens.
In Chrome I have the following on submit :
The submit button doesn’t even try to reload the page. The upload is stuck at 0%. (i’m using wamp here)
Why the hell can I submit txt or images files but not video (like avi or wmv) ?
Thanks,
Maxime.