0

我有一个两步表单(花式名称),但是,当我提交此表单时,验证始终返回 false,我对其进行了调试,并收到了我想要的所有参数,但由于某种原因 isValid 为 false 并得到RadioButton 元素中的一些错误,这是代码:

这是表单/Users.php

public function signup()
    {   

        $contryModel = new Application_Model_Country();
        $countries = $contryModel->getCountries();
        foreach ($countries as $array)
        {
            if(isset($array['id_country'])){
                $countryArr[$array['id_country']] = $array['country']; 
            }
        } 

        array_unshift($countryArr, '-- Select Your Country --');

        $name = new Zend_Form_Element_Text('Name');
        $name->setLabel('Name')
                ->setRequired(true)
                ->addValidator('NotEmpty');

        $lastname = new Zend_Form_Element_Text('LastName');
        $lastname->setLabel('Last Name')
                    ->setRequired(true)
                    ->addValidator('NotEmpty');

        $email = new Zend_Form_Element_Text('Email');
        $email->setLabel('Email')
                ->setRequired(true)
                ->addValidator('NotEmpty');

        $website = new Zend_Form_Element_Text('Website');
        $website->setLabel('Website')
                ->setRequired(true)
                ->addValidator('NotEmpty');

        $country = new Zend_Form_Element_Select('Country');
        $country->setLabel('Country')
                ->setRequired(true)
                ->addValidator('NotEmpty')
                ->addMultiOptions($countryArr);

        $city = new Zend_Form_Element_Select('City');
        $city->setLabel('City')
                ->setRequired(true)
                ->addValidator('NotEmpty')
                ->addMultiOptions(array('-- Select Your City --'))
                ->setRegisterInArrayValidator(false)
                ->setAttrib('disabled',true);


        $password = new Zend_Form_Element_Password('Password');
        $password->setLabel('Password')
                ->setRequired(true)
                ->addValidator('NotEmpty');

        $rpassword = new Zend_Form_Element_Password('RepeatPassword');
        $rpassword->setLabel('Repeat Password')
                ->setRequired(true)
                ->addValidator('NotEmpty');

        $why = new Zend_Form_Element_Textarea('Why');
        $why->setRequired(true)
            ->addValidator('NotEmpty')
            ->setAttrib('cols', '50')
            ->setAttrib('rows', '4');

        $job = new Zend_Form_Element_Text('Job');
        $job->setLabel("Job")
            ->setRequired(true)
            ->addValidator('NotEmpty');


        $boxes = new Application_Model_Colors();
        $res = $boxes->getColors();

        foreach($res as $colors){
            $colorArr[$colors['id']]['color'] = $colors['color'];
            $colorArr[$colors['id']]['overColor'] = $colors['overColor'];

        }

        $color = new Zend_Form_Element_Radio('color');
        $color->setLabel('Color')
                ->addMultiOptions($colorArr)
                ->addValidator('NotEmpty');



        $save = new Zend_Form_Element_Submit('Submit');
        $save->setLabel('Save');


        $this->addElements(array($name, $lastname, $email, $website, $password, $rpassword, $job, $country, $city, $color, $why, $save));

    }

*这是视图的一部分 *我格式化收音机的地方

<?php
foreach($this->form->color->options as $key=>$colors){

            echo "<label><input type='radio' value='$key' name='color'><div class='registrationBoxes' style='background-color:#".$colors['color']."' data-color='".$colors['color']."' data-overColor='".$colors['overColor']."'  value='".$colors['color']."'></div></label>";

}
?> 

这是控制器

public function indexAction()
    {





        $form = new Application_Form_Users();
        $form->signup();
        $this->view->form = $form;

        if($this->getRequest()->isPost())
        {   
            $formData = $this->getRequest()->getPost();
            if($form->isValid($formData))
            {

                $formData = $this->getRequest()->getPost();
                $name = $formData['Name'];
                $lastName = $formData['LastName'];
                $email = $formData['Email'];
                $website = $formData['Website'];
                $password = $formData['Password'];
                $rpassword = $formData['RepeatPassword'];
                $job = $formData['Job'];
                $country = $formData['Country'];
                $city = $formData['City'];
                $color = $formData['color'];
                $why = $formData['Why'];
                $date_create = date('Y-m-d H:i:s');
                $position = new Application_Model_Country();
                $latLong = $position->getLatLong($city);
                if($password == $rpassword){
                    $data = array(  'name'=>$name,
                                    'last_name'=>$lastName,
                                    'email'=>$email,
                                    'website'=>$website,
                                    'password'=>md5($password),
                                    'date_create'=>$date_create,
                                    'job'=>$job,
                                    'country'=>$country,
                                    'city'=>$city,
                                    'color'=>$color,
                                    'why'=>$why,
                                    'latitude'=>$latLong['latitude'],
                                    'longitude'=>$latLong['longitude']

                                );
                    $add = new Application_Model_Users();
                    $res = $add->insertUser($data);

                    if($res){
                        $this->view->message = "Registration Successfuly.";
                        $this->_redirect("/contact");

                    }else{
                        $this->view->errorMessage = "We have a problem with your registration, please try again later.";
                    }

                }else{
                    $this->view->errorMessage = "Password and confirm password don't match.";
                    $form->populate($formData);
                }
            } else {
                $this->view->errorMessage = "There is an error on your registration, all fields are requried";
                $form->populate($formData);
            }

        }
    }

a die(var_dump()) 返回:

array(12) {
  ["Name"]=>
  string(9) "Some name"
  ["LastName"]=>
  string(13) "Some lastName"
  ["Email"]=>
  string(21) "Someemail@hotmail.com"
  ["Password"]=>
  string(5) "Some "
  ["RepeatPassword"]=>
  string(5) "Some "
  ["Website"]=>
  string(12) "Some website"
  ["Job"]=>
  string(8) "Some job"
  ["Country"]=>
  string(1) "7"
  ["City"]=>
  string(3) "434"
  ["color"]=>
  string(1) "3"
  ["Why"]=>
  string(10) "Emotional!"
  ["Submit"]=>
  string(4) "Save"
}

如您所见,所有参数都带有值,但是 isValid 无缘无故返回 false,有些想法?

谢谢。-

编辑

这是 Tonunu 所需的 Zend/form.php 代码的一部分

public function __construct ($options = null)
{
    if (is_array($options)) {
        $this->setOptions($options);
    } elseif ($options instanceof Zend_Config) {
        $this->setConfig($options);
    }
    // Extensions...
    $this->init();
    $this->loadDefaultDecorators();
}



public function __clone ()
{
    $elements = array();
    foreach ($this->getElements() as $name => $element) {
        $elements[] = clone $element;
    }
    $this->setElements($elements);
    $subForms = array();
    foreach ($this->getSubForms() as $name => $subForm) {
        $subForms[$name] = clone $subForm;
    }
    $this->setSubForms($subForms);
    $displayGroups = array();
    foreach ($this->_displayGroups as $group) {
        $clone = clone $group;
        $elements = array();
        foreach ($clone->getElements() as $name => $e) {
            $elements[] = $this->getElement($name);
        }
        $clone->setElements($elements);
        $displayGroups[] = $clone;
    }
    $this->setDisplayGroups($displayGroups);
}

public function setOptions (array $options)
{
    if (isset($options['prefixPath'])) {
        $this->addPrefixPaths($options['prefixPath']);
        unset($options['prefixPath']);
    }
    if (isset($options['elementPrefixPath'])) {
        $this->addElementPrefixPaths($options['elementPrefixPath']);
        unset($options['elementPrefixPath']);
    }
    if (isset($options['displayGroupPrefixPath'])) {
        $this->addDisplayGroupPrefixPaths(
        $options['displayGroupPrefixPath']);
        unset($options['displayGroupPrefixPath']);
    }
    if (isset($options['elementDecorators'])) {
        $this->_elementDecorators = $options['elementDecorators'];
        unset($options['elementDecorators']);
    }
    if (isset($options['elements'])) {
        $this->setElements($options['elements']);
        unset($options['elements']);
    }
    if (isset($options['defaultDisplayGroupClass'])) {
        $this->setDefaultDisplayGroupClass(
        $options['defaultDisplayGroupClass']);
        unset($options['defaultDisplayGroupClass']);
    }
    if (isset($options['displayGroupDecorators'])) {
        $displayGroupDecorators = $options['displayGroupDecorators'];
        unset($options['displayGroupDecorators']);
    }
    if (isset($options['elementsBelongTo'])) {
        $elementsBelongTo = $options['elementsBelongTo'];
        unset($options['elementsBelongTo']);
    }
    if (isset($options['attribs'])) {
        $this->addAttribs($options['attribs']);
        unset($options['attribs']);
    }
    $forbidden = array('Options', 'Config', 'PluginLoader', 'SubForms', 
    'View', 'Translator', 'Attrib', 'Default');
    foreach ($options as $key => $value) {
        $normalized = ucfirst($key);
        if (in_array($normalized, $forbidden)) {
            continue;
        }
        $method = 'set' . $normalized;
        if (method_exists($this, $method)) {
            $this->$method($value);
        } else {
            $this->setAttrib($key, $value);
        }
    }
    if (isset($displayGroupDecorators)) {
        $this->setDisplayGroupDecorators($displayGroupDecorators);
    }
    if (isset($elementsBelongTo)) {
        $this->setElementsBelongTo($elementsBelongTo);
    }
    return $this;
}
4

1 回答 1

0

为了检查您的表格是否有效,您必须使用此方法将数据放入其中!

$form->setData($formData)

然后你可以要求验证

$form->isValid()

所以你需要的是:

$formData = $this->getRequest()->getPost();
$form->setData($formData);
if($form->isValid()){
  //Blablablabla
}
于 2013-05-30T16:34:19.280 回答