Amit K.
Amit K.

Reputation: 3754

zend form validation error

I was trying to validate a zend form. But problem is that in form there are three fields named country, state and city. I am sending valid data for these fields but its giving me validation error. for country, state and city only. Error messages are :

Please enter country name. Please enter state name. Please enter city name

Here is my form fields :

    $country = new Zend_Form_Element_Select('country');
    $country->setRequired(true)
            ->setAttrib('placeholder', 'Country')
            ->removeDecorator('DtDdWrapper')
            ->addErrorMessage('Please enter country name.')
            ->removeDecorator('HtmlTag')
            ->removeDecorator('Label');

    $state = new Zend_Form_Element_Select('state');
    $state->setRequired(true)
            ->setAttrib('placeholder', 'State')
            ->removeDecorator('DtDdWrapper')
            ->addErrorMessage('Please enter state name.')
            ->removeDecorator('HtmlTag')
            ->addMultiOptions(array("" => "Select State"))
            ->removeDecorator('Label');

    $city = new Zend_Form_Element_Select('city');
    $city->setRequired(true)
            ->setAttrib('placeholder', 'City')
            ->removeDecorator('DtDdWrapper')
            ->addErrorMessage('Please enter city name.')
            ->removeDecorator('HtmlTag')
            ->addMultiOptions(array("" => "Select City"))
            ->removeDecorator('Label');

here is posted Data :

Array ( 
[full_name] => Test User 
[dob] => 2015-01-15 
[gender] => 1 
[address] => ddewewe 
[country] => DZK 
[state] => 26 
[city] => 403564 
[mobile_number] => 4535345345 
[submit] => Save )

Can any one help me to spot this issue?

Thanks,

M.

Upvotes: 1

Views: 409

Answers (1)

Rupali
Rupali

Reputation: 1078

First you haven't set options for your Country, State and City drop-down list in your Zend_form. You have set only one option whose value is balnk "" and you have applied validation for required field hence it is giving mentioned error.

You will get another error ("value was not found in the haystack" ) because your Zend Form will match the posted value in the pre specified list of a select box. Since validator will not find any such option which you're sending in POST data hence it will throw error.

You have following ways to resolve these issues.

  1. Deactivate the validator to validate posted values in pre specified array as shown below and remove setRequired(true) validation:

     $country = new Zend_Form_Element_Select('country');
     $country->setAttrib('placeholder', 'Country')
     $country->setAttrib('placeholder', 'Country')
        ->removeDecorator('DtDdWrapper')
        ->removeDecorator('HtmlTag')
        ->removeDecorator('Label')
        ->setRegisterInArrayValidator(false); //This line will not check posted data in list
    
  2. Set options array in your zend_form itself.

    class RegistrationForm extends Zend_Form {
      public function __construct($options = null) {
        $country = new Zend_Form_Element_Select('country');
        $country->setRequired(true)
            ->setAttrib('placeholder', 'Country')
            ->removeDecorator('DtDdWrapper')
            ->addErrorMessage('Please enter country name.')
            ->removeDecorator('HtmlTag')
            ->removeDecorator('Label');
        foreach($options['countries'] as $option)
            $country->addMultiOption(trim($option->code),trim($option->country_name));
       }
    }
    

    and while creating object of Zend form in your controller pass array of countries in it.

Upvotes: 1

Related Questions