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

  • Home
  • SEARCH
  • 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 8710721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:41:46+00:00 2026-06-13T04:41:46+00:00

I wanted to display a simple exception message in zend form which is thrown

  • 0

I wanted to display a simple exception message in zend form which is thrown by an exception. I check whether there is a duplicate record existing in the database and if exits then I want to throw an error saying the the record with that name already exists in the database. This I wanted to show in the add.phtml file exactly after the record name textfield.

This is how I am trying to do:

In my Controller:

public function addAction()
{
    try {
        $records->validateDuplicateRecords($recordTitle);

        if ($form->isValid()) {
            //doing all the stuff like saving data to database
        }
    } catch (\Exception $e) {
         echo $e->getMessage(); //Not sure with this part
    }
}

And the class where I am checking for duplicate records:

records.php

public function validateDuplicateRecords($recordTitle)
{
    //fetching all titles from database

   //comparing with $recordTitle using foreach and if
   //all these here in the loop works, I am giving the skeleton of my code
    foreach($records as $record)
    {
        if($record == $recordTitle) {
            throw new \Exception("Record with title '$recordTitle' already exists");
        }
        return true;
    }   
}

So that is basically how im doing, I know how this try and catch works with pure php stuff, but I don’t know how I can use the exceptions with Zend Framework 2 and zend forms. In case if anybody has a solution for this would be glad if it can be shared.

P.S. I followed the Album Module so basically my structure resembles more or less same from the official Module

EDIT: add.phtml has been addded

add.phtml

<?php
$title = "Add New Record Title";
$this->headTitle($title);
?>
<h2><?php echo $this->escapeHtml($title); ?></h2>

<?php
$form = $this->form;
$form->setAttribute("action", $this->url("addRecordTitle", array('controller' => "album", 'action' => "add")));
$form->prepare();

echo $this->form()->openTag($form);
echo $this->formRow($form->get('recordTitle'));
echo $this->formInput($form->get('submit')); 
echo $this->form()->closeTag($form); 
?>
  • 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-13T04:41:48+00:00Added an answer on June 13, 2026 at 4:41 am

    Given your example, a way to do it would be this. However i suggest you read yourself into the inbuilt validators Db\RecordExists and Db\RecordNoExists, as they may already do what you’re trying to do.

    public function addAction() 
    {
         $form = $this->getForm(); //theoretical
    
         try {
             $records->validateDuplicateRecords($recordTitle);
         } catch (\Exception $e) {
             $form->setMessages(array(
                 'recordTitle' => array($e->getMessage())
             ));
             return new ViewModel(array(
                 'form' => $form
             ));
         }
    
         if ($form->isValid()) { 
             //usual stuff
         }
    }
    

    With this code you’d attach the error message onto your title-FormElement, be sure to edit the name into the name of your title element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having about 200 icons ,which I wanted to display in a treeview on
I have an object of type IMAGE which holds image. I wanted to display
hi guys I wanted to know, is there a way to not display my
Consider this simple Relation MyRel(A, B) I wanted to display A based on value
I wanted to have a Text widget that could display a message in it
I use flash[:error] to display a simple message to users that they cannot do
I wanted to display 9 images one after the other. I have included the
Let's say I wanted to display a Button and a few RadioButtons . Based
I am sourcing a large mapping/widget javascript file (1.3 MB) and wanted to display
I wanted to parse the xml feed and display as links in my Blackberry

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.