Yii Framework Forum: can not update a record without uploading an image - Yii Framework Forum

Jump to content

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

can not update a record without uploading an image Rate Topic: -----

#1 User is offline   yiinighter2000 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 08-January 17

Posted 05 April 2017 - 09:47 AM

I been trying to deal with a website that was written by another programmer. the website is built with yii 1.1.4

it seems that i can not update a "store" record without uploading a new picture every time. here is the message i get when i try to update without uploading an image: "Please fix the following input errors:

Picture cannot be blank."

the picture field (aka "icon_filename") is not even required. also, most of the records do not have any picture uploaded yet.

anybody know what is the problem and could please help?

here is the code:

models:

public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('name, stream_id, full_address,about, country_id', 'required', 'on' => array('create')),
        array('status, stream_id, lock_version', 'numerical', 'integerOnly' => true),
        array('name, email', 'length', 'max' => 255),
        array('about, full_address, website_url', 'length', 'max' => 500),
        array('website_url, maps_service_url', 'url'),
        array('phone, fax', 'length', 'max' => 30),
        array('phone, fax', 'PcSimplePhoneValidator'),
        array('country_id, region_id, assigned_user, created_by, last_updated_by_user_id', 'length', 'max' => 10),
        array('assigned_user', 'validateAssignedUser', 'except' => 'search'),
        array('city_id', 'length', 'max' => 6),
        array('icon_filename', 'file',
            'types' => "jpg,jpeg",
            'wrongType' => Yii::t('StoresModule.forms', "Invalid file type. These are the supported file types: {extensions}"),
            'maxSize' => 1048576,
        ),
        array('icon_filename', 'file', 'allowEmpty' =>true, 'on' => 'update'),
        array('updated_on', 'safe'),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, name, email, stream_id, searchText, status, about, full_address, phone, fax, website_url, country_id, region_id, city_id, icon_filename, assigned_user', 'safe', 'on' => 'search'),
    );
}


controller:


enter/**
 * Updates a particular model.
 * If update is successful, the browser will be redirected to the 'view' page.
 *
 * @param integer $id the ID of the model to be updated
 * @throws CHttpException
 */
public function actionUpdate($id) {
    /* @var Store $model */
    $model = $this->loadModel($id);
    // set scenario to update to allow empty image - meaning no replacement of Store image
    $model->scenario = 'update';

    if ($model === null) {
        Yii::log("Store update requested with id $id but no such record found!", CLogger::LEVEL_INFO, __METHOD__);
        throw new CHttpException(404, Yii::t("StoresModule.general", 'The requested page does not exist.'));
    }

    if (Yii::app()->user->checkAccess('edit Store')) {
        // do nothing. edit is allowed.
    }
    else if (!is_null($model->assignedUser)) {
        // some user is assigned to this Store
        if (Yii::app()->user->checkAccess('edit assigned Store', array('assigned_user' => $model->assignedUser->id, 'user_id' => Yii::app()->user->id))) {
            // do nothing. this user is the assigned user and therefore is allowed to edit.
        }
    }
    else {
        Yii::log("User (id=" . Yii::app()->user->id . ") tried to edit Store with id=$id but no such record. Giving him a **404** error", CLogger::LEVEL_WARNING, "SECURITY " . __METHOD__);
        throw new CHttpException(404, Yii::t("StoresModule.general", 'The requested page does not exist.'));
    }

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

        // use aux variable for manipulating the image file.
        $image = CUploadedFile::getInstance($model, 'icon_filename');
        // check if a new image weas submitted or not:
        if ($image) {
            /* the only thing that might have changed in the update is the extension name of the image. therefore,
                                 * if something was submitted, and since we already know the ID of the Store, we can determine the full
                                 * updated icon_filename attribute of the model prior to its save() (unlike in create action - see there...).
                                 */
            $model->icon_filename = $model->getImageFsFilename($image);
        }

        if ($model->save()) {
            // save the updated image, if any
            if ($image) {
                $image->saveAs($model->getImageFsFilename($image));
                // create the thumbnail image file:
                /* @var simple_image $thumbnail */
                $thumbnail = Yii::app()->imageResizer->load($model->icon_filename);
                $thumbnail->resizeToWidth(Store::THUMBNAIL_WIDTH_LIMIT);
                $thumbnail->save($model->getImageFsThumbFilename($image));
            }
            $this->redirect(array('view', 'slug' => $model->generateUniqueSlug()));
        }
    }

    $this->render('update', array(
        'model' => $model,
    ));
}

0

#2 User is offline   reykel 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 67
  • Joined: 21-June 15
  • Location:Camaguey, Cuba

Posted 07 April 2017 - 07:34 PM

You've got it as empty allowed but on the controller there are instructions can not work with out a value... Try that part and tell me please...
0

#3 User is offline   yiinighter2000 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 08-January 17

Posted 17 April 2017 - 01:11 PM

View Postreykel, on 07 April 2017 - 07:34 PM, said:

You've got it as empty allowed but on the controller there are instructions can not work with out a value... Try that part and tell me please...


thanks! needed to add a "'allowEmpty'=>true" to each appearance of the 'icon_filename' rule.
0

#4 User is offline   reykel 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 67
  • Joined: 21-June 15
  • Location:Camaguey, Cuba

Posted 18 April 2017 - 09:39 PM

Ok, any time bro, good luck
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