Dropdown validation

Go To StackoverFlow.com


I am having one dropdown option in the form called town. Already fetched values will be available in drop down from the databasetable townId. Now I want to check if the user doesn't select any choice from the dropdown and directly goes to the save button, then it should display "please choose your choice in dropdown" like that. The form is named university

I tried this code here:

                {   alert("U Must Choose Town Name Here");

                    $submit = new Zend_Form_Element_Submit('Save');
                    ->setAttrib('class','button slategray');


Inside models->university.php there are some actions for dropdown I didn't get exactly:

public function setOptions(array $options)
    $methods = get_class_methods($this);
    foreach ($options as $key => $value) {
        $method = 'set' . ucfirst($key);
        if (in_array($method, $methods)) {
    return $this;

Before I edited form->university (the save code already exits)

$submit = new Zend_Form_Element_Submit('Save');
        ->setAttrib('class','button slategray');

Thanks in advance.

2012-04-04 06:09
by subhangi.s
Approach you are taking is pretty much wrong. You don't do something like if(Zend_Form_Element_Submit). There alredy are functions provided by zend called isValid() which allows you to check wether your form is valid or not. For that you will ofcourse have to add validators (something like isRequired() in your case) to form elements. No offense but I really think you should do some research on your own before you ask questions (this is based on your previous question as well). Please see the manual - ro ko 2012-04-05 10:31


To work properly with Zend Framework's forms I recommand to try this approach:

Create a form by extending the Zend_Form class

class Form_User extends Zend_Form

Which automatically give you access to a container to manage all your elements from this form and give you acces to a isValid() method which allow you to validate all your form elements at once and to a populate() method which allow you to feed data to your form for editing

In your new class (Form_User) you can define all your form properties and elements in the init() method.

public function init()
    ->setAttrib('id', 'user');

    $username = new Zend_Form_Element_Text('username');

    $town = new Zend_Form_Element_Select('town');

    $submit = new Zend_Form_Element_Select('submit');

    $this->addElements(array($username, $town, $submit));

Each of these elements can be customized with a label, a description, some validators, some filters, etc.

If you want to make an element mandatory you can set the Required property to true


To validate your form after the submit has been cliqued, you can do it as simply as (assuming you do this in the controller) :

$form = new Form_User();
$postData = $this->getRequest()->getPost();

This will check for the required fields as well as execute any validators that you have setted on these diverses elements.

It will return true if everything is ok, and false if there is an error. If you get an error a display the form again, error messages will be shown automatically beside each erroneous fields.

If you want to set options values of your form elements, when you initialize your form, you can pass a configuration array like this :

$form = new Form_User(array('townListOptions' => array(...));

The associated method setTownListOptions will be called automatically and will receive the array (or any other object) you assigned it with.

I could explain even further things about forms but as @Rohan stated in his comment, RTM

2012-04-05 15:02
by JF Dion


In asp.net

<asp:DropDownList ID="ddlusertype" runat="server" CssClass="dropdown"  ></asp:DropDownList>
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="ddlusertype" ErrorMessage="select" Font-Size="XX-Small" Operator="NotEqual" Type="Integer" ValueToCompare="0"></asp:CompareValidator>
2012-04-04 06:14
by Brijesh Pandey
Thank u for your code.i will try this in zend frame wor - subhangi.s 2012-04-05 12:39