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 6135907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:32:13+00:00 2026-05-23T17:32:13+00:00

For my school project, I am working on a database management application. It is

  • 0

For my school project, I am working on a database management application.
It is my first real Zend Framework application.

Now, right now, I have set 3 values as required, postalcode, email and telephone.
They are required like this(Example):

        $mail = $this->createElement('text', 'mail');
    $mail->setLabel('E-mail:')
            ->setAttrib('size', 50)->addValidator('StringLength', false, array(6, 40))->addValidator('EmailAddress', true)
            ->setRequired(true);
        $telephone = $this->createElement('text', 'telephone');
    $telephone->setLabel('Telephone:')
            ->setAttrib('size', 50)->addValidator('StringLength', false, array(10, 10))
            ->setRequired(true);

How can I make only one of them required?

I am working with Zend_Form, and also working with Displaygroups.

Is there someone who knows a solution for my program? Maybe use an array?

Thanks in advance,

JorritK

UPDATE

@brady.vitrano

Ok, the first part of my code looks like this now:

<?php

class Application_Form_Validate_ContactMethodSelected
extends Zend_Validate_Abstract
{
const INVALID = 'invalid';

protected $_messageTemplates = array(
    self::INVALID => 'Must select at least one contact method'
);

public function isValid($value, $context = array())
{
     // You need to use your element names, consider making these dynamic
    $checkFields = array('telefoon','mobiel','mail');
    // Check if all are empty
    foreach ( $checkFields as $field ) {
        if (isset($context[$field]) && !empty($context[$field])) {
            // Only one value needs to return true..skip the rest
            return true;
        }
    }

    // All were empty, set your own error message
    $this->_error(self::INVALID);
    return false;
}

}

class Application_Form_Nieuwkandidaat extends Zend_Form
{

public function init()
{
     $this->setMethod('post');
    $DB = Zend_Db_Table::getDefaultAdapter();


    $telefoon = $this->createElement('text', 'telefoon');
    $telefoon->setLabel('Telefoon:')
            ->setAttrib('size', 50)->addValidator('StringLength', false,array(10,10));
    $telefoon->addValidator(new Application_Form_Validate_ContactMethodSelected());
    $mobiel = $this->createElement('text', 'mobiel');
    $mobiel->setLabel('Mobiel:')
            ->setAttrib('size', 50)->addValidator('StringLength', false,array(10,10));
    $mobiel->addValidator(new Application_Form_Validate_ContactMethodSelected());
    $mail = $this->createElement('text', 'mail');
    $mail->setLabel('E-mail:')
            ->setAttrib('size', 50)->addValidator('StringLength', false,array(6,40))->addValidator('EmailAddress', true);
    $mail->addValidator(new Application_Form_Validate_ContactMethodSelected());

It won’t show the message after hitting the submit button. What more should I change? Thanks for helping!

  • 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-05-23T17:32:13+00:00Added an answer on May 23, 2026 at 5:32 pm

    A good solution to consider is to write a custom validator for the elements.

    In your, isValid method you will have to check based on $context of other values. Something like:

    EDIT

    /** /library/Application/Form/Validate/ContactMethodSelected.php **/
    
    class Application_Form_Validate_ContactMethodSelected 
        extends Zend_Validate_Abstract
    {
        const INVALID = 'invalid';
    
        protected $_messageTemplates = array(
            self::INVALID => 'Must select at least one contact method'
        ); 
    
        public function isValid($value, $context = array())
        {
             // You need to use your element names, consider making these dynamic
            $checkFields = array('phone','email','address');
            // Check if all are empty
            foreach ( $checkFields as $field ) {
                if (isset($context[$field]) && !empty($context[$field])) {
                    // Only one value needs to return true..skip the rest
                    return true;
                }
            }
    
            // All were empty, set your own error message
            $this->_error(self::INVALID);
            return false;   
        }
    
    }
    

    Now, you have to tell the elements to use that validator. So, make changes in your forms init() method.

     $mail->addValidator(new Application_Form_Validate_ContactMethodSelected());
     $telephone->addValidator(new Application_Form_Validate_ContactMethodSelected());
    

    Don’t forget: Once you have your own custom validator, you will have to remove the isRequired() from each element.

    EDIT2

    You must set the custom validator as the first validator in the chain and break on failure. Also, you have to setAllowEmpty() to false.

    $telefoon = $this->createElement('text', 'telefoon');
    $telefoon->setLabel('Telefoon:')
            ->setAttrib('size', 50)->setAllowEmpty(false)
            ->addValidator(new Application_Form_Validate_ContactMethodSelected(),true)
            ->addValidator('StringLength', false,array(10,10));
    $mobiel = $this->createElement('text', 'mobiel');
    $mobiel->setLabel('Mobiel:')
            ->setAttrib('size', 50)->setAllowEmpty(false)
            ->addValidator(new Application_Form_Validate_ContactMethodSelected(),true)
            ->addValidator('StringLength', false,array(10,10));
    $mail = $this->createElement('text', 'mail');
    $mail->setLabel('E-mail:')
            ->setAttrib('size', 50)->setAllowEmpty(false)
            ->addValidator(new Application_Form_Validate_ContactMethodSelected(),true)
            ->addValidator('StringLength', false,array(6,40))->addValidator('EmailAddress', true);
    

    Next, you will have to update the isValid method with the following:

    public function isValid($value, $context = array())
    {
        // You need to use your element names, consider making these dynamic
        $checkFields = array('telefoon','mobiel','mail');
        // Check if all are empty
        foreach ( $checkFields as $field ) {
        if (isset($context[$field]) && !empty($context[$field])) {
    
            if (!empty($value)) {
                // This is the element with content... validate as true
                return true;
            }
            // we are going to return false and no error
            // to break validation chain on other empty values
            // This is a quick hack, don't have time to invest in this
            return false;
            }
        }
    
        // All were empty, set your own error message
        $this->_error(self::INVALID);
        return false;
    }
    

    Now, you will have to add another condition to your code that uses this validator. We have to require that the form does not have any error messages.

    if ($form->isValid($_POST) || !count($form->getMessages())) {
        /** Valid, now you can process **/
    } else {
        /** Not valid **/
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.