Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:44:23+00:00 2026-06-07T03:44:23+00:00

In my form, I want user to pick a file OR enter it’s URL.

  • 0

In my form, I want user to pick a file OR enter it’s URL. One of two options.

I know how to write a validator for either one of two Zend_Form_Element_Text elements, but since data from Zend_Form_Element_File is not in $_POST but in $_FILES I don’t know where to begin – I can’t get the data from Zend_Form_Element_File to be in a $context in isValid($value, $context = null) method for my custom validator.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:44:24+00:00Added an answer on June 7, 2026 at 3:44 am

    A possible approach I can think of is to pass the information if the file has been uploaded as additional context to the form’s validation method:

    Form

    $file = new Zend_Form_Element_File('file');
    
    $text = new Zend_Form_Element_Text('text');
    $text->setAllowEmpty(false);
    $text->addValidator(new TextOrFileValidator());
    
    $this->addElement($file);
    $this->addElement($text);
    

    Controller

    $request = $this->getRequest();
    
    // provide additional context from the form's file upload status
    $context = array_merge(
        $this->getRequest()->getPost(),
        array("isUploaded" => $form->file->isUploaded())
    );
    
    if ($request->isPost() && $form->isValid($context)) {
    }   
    

    Validator

    class TextOrFileValidator extends Zend_Validate_Abstract
    {
    
        const ERROR = 'error';
    
        protected $_messageTemplates = array(
            self::ERROR      => "You either have to upload a file or enter a text",
        );
    
        function isValid( $value, $context = null ) {
    
            $hasText = !empty($context['text']);
            $hasFile = $context['isUploaded'];
    
            if (($hasText && !$hasFile)
                || (!$hasText && $hasFile)
            ) {
                return true;
            }
    
            $this->_error(self::ERROR);
            return false;
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a form and want user to only enter capital letters
I currently have multiple search fields within one form which the user can pick
Before the user closes the form I want to prompt with a confirmation box.
I have a form. Once the form is filled I don't want the user
I want a form where user can change password. I am able to encrypt
When a user clicks a submit button I want the form to be submitted.
I want that the user shouldn't be able to resize the window form. I
I've got a form with a date and I want prompt the user if
I want to make a user-registration form where I will have fields such first
I want to submit my form after a user clicked/touched the checkbox: THE HTML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.