0

I'm working on Zend Framework 1.11.12 version. In my add form where we've to put file upload (for photo upload) field and it has few validations as required, file extensions and size specific criteria mentioned as below, which is declared under my zend form:

    // part of my form class (Default_Form_Photo::init)
    $photo = new Zend_Form_Element_File('photo', '', array('required'   => true));
    $photo->setLabel('Photo')
          ->setDestination('/var/www/zendframework/public/uploads/');
    // ensure its mandatory
    #$photo->addValidator('required', true);
    // ensure only one file
    $photo->addValidator('Count', false, array('min' => 1));
    // max 2MB
    $photo->addValidator('Size', false, 2097152)
          ->setMaxFileSize(2097152);
    // only JPEG, PNG, or GIF
    $photo->addValidator('Extension', false, 'jpg,jpeg,png,gif');
    $photo->setValueDisabled(true);
    $this->addElement($photo, 'photo');

Now I have taken a hidden field, for edit page which will return me current image name, if no new image uploaded:

    // hidden element for edit time photo save
    $this->addElement('hidden', 'photo_uploaded', array( ));

I set this hidden element value from controller by $form->photo_uploaded->setValue($result['photo']).

Now the problem is: when user edit information, it does not allow saving because the file upload field is blank as no new image uploaded, but in my case have to use existing current image, which I am passing from hidden element to my controller.

So validation causes problem here, any idea how can i remove validation for my edit page where users do not have to upload new image every time, they are editing information.

Earliest response will be appreciated.

Thanks !

1 Answer 1

2

The answer to this requires you to add two functions to your form: isValid and getValues. First of all, remove required from the photo element as we are going to check for this in isValid:

/**
 * Check if a photo has been submitted. If not, set the form element as required.
 * @param $data Array of values
 * @return Boolean true if all form element requirements are mets
 */
public function isValid($data)
{
 //A photo has not been uploaded this time, nor previously.
 if(!$data['photo'] AND !$data['photo_uploaded'])
 {
  $this->photo->isRequired(TRUE);
 }
 return parent::isValid($data);
}

/**
 * Return an array of form element values
 * @return Array of values
 */
public function getValues()
{
 $values = $this->getValues();

 //if photo has been uploaded, but no previous photos have been uploaded, then set photo_uploaded value.
 if($values['photo'] AND !$values['photo_uploaded'])
 {
  $values['photo_uploaded'] = $values['photo'];
 }

 //if previous photo has been uploaded, but photo has not been uploaded this time, set photo value
 if($values['photo_uploaded'] AND !$values['photo'])
 {
  $values['photo'] = $values['photo_uploaded'];
 }

 return $values;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.