Yii 1 File Validation Not Allowing Empty Field

205 Views Asked by At

Hopefully you can help. I have the validator in the model I believe correct however it still is requiring a file. The allowempty is set to true, it just doesn't seem to be working, not sure where the issue is coming from, but hopefully someone here can point my in the right direction

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('filename', 'file',  'allowEmpty'=>true,),
        array('vendor_id, vendor_service_id, project_id', 'numerical', 'integerOnly'=>true),
        array('id, vendor_id, amount, vendor_service_id, requierd_by, project_id, notes,url,filename', 'safe'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, vendor_id, amount, vendor_service_id, requierd_by, project_id, notes', 'safe', 'on'=>'search'),
    );
}

My Controller Action:

public function actionCreate()
{
    $model=new Checkrequests;
    $bucket = 'cesfiledata';
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    $live = 1;
    if(isset($_POST['Checkrequests']))
    {
        $prj="";
        $model->attributes=$_POST['Checkrequests'];
        

        if($model->save())
        




        $prj = Yii::app()->db->createCommand()
        ->select("project_no")
        ->from('projects')
        ->where('id='.$_GET['project_id'])
        ->queryScalar();
        $name = $_FILES['file']['name'];
        if(isset($name)){
        $size = $_FILES['file']['size'];

        $file = CUploadedFile::getInstanceByName('file');
        
        $tmp = $file->tempName;
        $actual_image_name = $file->name . "." . $this->getExtension($file->name);
        $model->filename = $actual_image_name;
        
        $model->url = $s3file = 'http://' . $bucket . '.s3.amazonaws.com/'.$prj.'/documents/' . $actual_image_name;
        if(strlen($name['file']) > 0){
        

                $s3 = new A2S3();
                //Rename image name. 
                if($size['file'] < (10024 * 10024)){
                    if($s3->putObject(array(
                        'SourceFile'          => $tmp,
                        'Bucket'              => $bucket,
                        'Key'                 => $prj.'/documents/'.$actual_image_name,
                        'ACL'                 => 'public-read',
                        'x-amz-storage-class' => 'REDUCED_REDUNDANCY',
                    ))
                    ){
                        Yii::app()->user->setFlash('notification', 'File has been successfully uploaded to Amazon!');
        }

                    
    

        
        $service = Yii::app()->db->createCommand()->select("description")->from('vendor_services')->where('id='.$model->vendor_service_id)->queryScalar();
        $vend = Yii::app()->db->createCommand()->select("name")->from('vendors')->where('id='.$model->vendor_id)->queryScalar();
        $vendoraddr = Yii::app()->db->createCommand()->select("address")->from('vendors')->where('id='.$model->vendor_id)->queryScalar();
        $projectmgr = Yii::app()->db->createCommand()->select("ad_email")->from("EMPLOYEE,projects")->where('EMPLOYEE.EMPLOYEE_ID = ces_project_manager and projects.id='.$model->project_id)->queryScalar();

        $headers = 'From: [email protected]' . "\r\n" .
               'Reply-To: [email protected]' . "\r\n" .
           "Content-type: text/html\r\n";

        if($live == 1) {
        $to = "[email protected]";
        $to.=",".$projectmgr;
        }
        else {
            $to = "[email protected]";
        }
        $prj = Yii::app()->db->createCommand()
        ->select("project_no")
        ->from('projects')
        ->where('id='.$_GET['project_id'])
        ->queryScalar();
        $subject = "Check Request, Project: " .$prj;
        $body = "A Check Request Has Been Submitted<br><br>

        Site Name: ".$prj."<br><br>
        Special Instructions: ".$model->notes."<br><br>
        <table border='1' style='border: 1px solid black; border-collapse:collapse;'>
        <tr>
            <th style='text-align:left'>Vendor Name:</th>&nbsp;  ".$vend."<td></td>
        </tr>
        <tr>
            <th style='text-align:left'>Vendor Address:</th><td>&nbsp;  ".$vendoraddr."</td>
        </tr>
        <tr>
            <th style='text-align:left'>Type of Service:</th><td>&nbsp;  ".$service."</td>
        </tr>
        <tr>
            <th style='text-align:left'>Check Needed By:</th><td>&nbsp; ".$model->requierd_by."</td>
        </tr>
        <tr>
            <th style='text-align:left'>Amount of Request:</th><td>&nbsp; ".money_format("$%(#10n",$model->amount)."</td>
        </tr>
        <tr>
            <th style='text-align:left'>Backup Documentation:</th><td>&nbsp; <a href=".$model->url.">".$model->filename."</a></td>
        </tr>
        </table>";
        #Send Notification
        $model->save(false);
        mail($to, $subject, $body, $headers);
        Yii::app()->user->setFlash('notification', 'Request Submitted!');
        $this->redirect(array('checkrequests/manage/','id'=>$_GET['project_id']));
        }
                    else {
                        Yii::app()->user->setFlash('error', 'File upload failed!');
                    }

                }


            }
        }
1

There are 1 best solutions below

0
On

The record was posting the issue was related to a misplaced if() else statement