Question

I am creating forms elements like this:

$element = $this->CreateElement('select', 'countries');
$element->setLabel('Countries');
$element->setAttrib( 'required', true );
$element->addMultiOptions( array( ''=>'Select', '1'=>'Aus','2'=>'UK',.... ) );

Following code is submitting form:

// form
$form = new SomeForm();

// get request
$request = $this->getRequest();

// isPost
if( $request->isPost() ) {

    // isValid
    if( $form->isValid( $request->getPost() ) ) {               
         $values = $form->getValues();    
         // Save values into database here                                      
    } else {            
        $this->view->msg = "Required Fields are missing.";
    } 

} else {        
    $this->view->msg = 'Form is not submitted properly';
}

When I fill form properly then it save record into database successfully. But when I does not select country, it also submit form. I think I should show "Required Fields are missing." message instead of inserting values into database when required fields is missing.

What am I missing ??

Thanks

Was it helpful?

Solution

This line:

$element->setAttrib( 'required', true );

doesn't really do what you think it does (it sets an HTML attribute).

You probably wanted

$element->setRequired(true);

(This is assuming you're using Zend_Form, it's not totally clear from the code you posted).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top