yii ajax xupload form submit is not working

2019-06-02 15:50发布

问题:

Q1 : form submitting is not working.

Q2 : how to limit upload files (e.g 1 - 5 files only)

status : create a form with ajax upload xupload

My model (fadepreciation.php)

public function afterSave( ) {
        $this->addImages( );
        parent::afterSave( );
    }

    public function addImages( ) {
        //If we have pending images
        if( Yii::app( )->user->hasState( 'images' ) ) {
            $userImages = Yii::app( )->user->getState( 'images' );
            //Resolve the final path for our images
            $path = Yii::app( )->getBasePath( )."/../images/uploads/{$this->id}/";
            //Create the folder and give permissions if it doesnt exists
            if( !is_dir( $path ) ) {
                mkdir( $path );
                chmod( $path, 0777 );
            }

            //Now lets create the corresponding models and move the files
            foreach( $userImages as $image ) {
                if( is_file( $image["path"] ) ) {
                    if( rename( $image["path"], $path.$image["filename"] ) ) {
                        chmod( $path.$image["filename"], 0777 );
                        $img = new Image( );
                        $img->size = $image["size"];
                        $img->mime = $image["mime"];
                        $img->name = $image["name"];
                        $img->source = "/images/uploads/{$this->id}/".$image["filename"];
                        $img->somemodel_id = $this->id;
                        if( !$img->save( ) ) {
                            //Its always good to log something
                            Yii::log( "Could not save Image:\n".CVarDumper::dumpAsString( 
                                $img->getErrors( ) ), CLogger::LEVEL_ERROR );
                            //this exception will rollback the transaction
                            throw new Exception( 'Could not save Image');
                        }
                    }
                } else {
                    //You can also throw an execption here to rollback the transaction
                    Yii::log( $image["path"]." is not a file", CLogger::LEVEL_WARNING );
                }
            }
            //Clear the user's session
            Yii::app( )->user->setState( 'images', null );
        }
    }

My view (_form.php)

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'fa-depreciation-form',
    'enableAjaxValidation'=>false,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>
<!-- Other Fields... -->
        <div class="row">
            <?php echo $form->labelEx($model,'photos'); ?>
            <?php
            $this->widget( 'xupload.XUpload', array(
                'url' => Yii::app( )->createUrl( "/fadepreciation/upload"),
                //our XUploadForm
                'model' => $photos,
                //We set this for the widget to be able to target our own form
                'htmlOptions' => array('id'=>'fa-depreciation-form'),
                'attribute' => 'file',
                'multiple' => true,
                //Note that we are using a custom view for our widget
                //Thats becase the default widget includes the 'form' 
                //which we don't want here
                //'formView' => 'application.views.faDepreciation._form',
                )    
            );
            ?>
        </div>
    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

My controller (fadepreciation.php)

public function actionCreate()
    {
        $model=new FaDepreciation;
        Yii::import( "xupload.models.XUploadForm" );
        $photos = new XUploadForm;
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['FaDepreciation']))
        {
            //Assign our safe attributes
            $model->attributes=$_POST['FaDepreciation'];
            //Start a transaction in case something goes wrong
            $transaction = Yii::app( )->db->beginTransaction( );
            try {
                //Save the model to the database
                if($model->save()){
                    $transaction->commit();
                    $this->redirect(array('view','id'=>$model->id));
                }
            } catch(Exception $e) {
                $transaction->rollback( );
                Yii::app( )->handleException( $e );
            }
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        Yii::import( "xupload.models.XUploadForm" );
        $photos = new XUploadForm;
        $this->render('create',array(
            'model'=>$model,
            'photos'=>$photos,
        ));

    }
public function actionUpload( ) // From xupload nothing change

回答1:

What you need to do is to create a custom form. Copy the content from xupload _form and paste it removing the begin form - end form. Add to your widget 'formView' the reference at the custom form.



回答2:

what is the issue about submission form?

yes file limit can be done. Please make sure you follow these http://www.yiiframework.com/wiki/348/xupload-workflow/



回答3:

Q1: form submition is not working, because the XUpload widget generates its own form tag. so your generated HTML has a form embebed in another form, you should use formView option of the widget to point to a view that has no form tags, as described in the xupload workflow wiki

Q2: You should use maxNumberOfFiles option in the widget config

It all should look like this:

 <?php
            $this->widget( 'xupload.XUpload', array(
                'url' => Yii::app( )->createUrl( "/fadepreciation/upload"),
                //our XUploadForm
                'model' => $photos,
                //We set this for the widget to be able to target our own form
                'htmlOptions' => array('id'=>'fa-depreciation-form'),
                'attribute' => 'file',
                'multiple' => true,
                //Note that we are using a custom view for our widget
                //Thats becase the default widget includes the 'form' 
                //which we don't want here
                'formView' => 'application.views.faDepreciation._form',
                'options' => array('maxNumberOfFiles' => 5)
                )    
            );
            ?>


回答4:

Just use 'showForm' parameter as follow:

<?php
$this->widget( 'xupload.XUpload', array(
  ...
  'showForm' => false,
  ...
));
?>

Maybe, this option been added in next versions of xupload.



回答5:

I know that it's an old post but maybe this answer will help someone to solve this issue.

I found out that it's caused by the last line in the file /xupload/views/form.php (with default settings). It looks like the if statement is somehow working opposite... in mining that for false value it's rendering the code. For example:

<?php 
echo $this->showForm; 
if($this->showForm) echo CHtml::endForm(); 
echo $this->showForm; 
?>

returns:

Maybe I'm missing something but it looks weird... isn't it?