【问题标题】:zend_form isValid always falsezend_form isValid 总是 false
【发布时间】:2014-04-24 00:32:38
【问题描述】:

我为使用 Zend 1 的网站编写代码。 我想在zend中重写旧的管理界面,但是我的表单验证失败。

我将在此处发布表单、验证代码和调试输出。

表格:

class Form_Admin_Address_Neu extends Zend_Form {

public function init() {
$this->setMethod('post');

$this->addElement('text', '_street', array(
    'label' => 'Strasse:',
    'size' => 60,
    'required' => true,
    'filters' => array('StringTrim'),
));

$this->addElement('text', '_zip', array(
    'label' => 'PLZ:',
    'size' => 6,
    'required' => true,
    'filters' => array('StringTrim'),
));

$this->addElement('text', '_city', array(
    'label' => 'Stadt:',
    'required' => true,
    'size' => 30,
    'filters' => array('StringTrim'),
));

$this->addElement('text', '_lat', array(
    'label' => 'Latitude:',
    'required' => true,
    'size' => 30,
    'validators' => array('Float'),
    'filters' => array('Stringtrim'),
));

$this->addElement('text', '_lng', array(
    'label' => 'Longitude:',
    'required' => true,
    'size' => 30,
    'validators' => array('Float'),
    'filters' => array('Stringtrim'),
));

$this->addElement('checkbox', '_hidden', array(
    'label' => 'Hidden:',
    'size' => 1,
    'filters' => array('Int', 'Null'),
));

$this->addElement('submit', 'submit', array(
    'ignore' => true,
    'label' => 'Senden',
));

$this->addElement('hash', 'csrf', array(
    'ignore' => true,
));

$this->setAction('/admin/address/list');
}

}

附加字段(我这里只发了 1 条,第 2 条就这样):

class Form_Admin_Elements_CountrySelect extends Zend_Form_Element_Select {
public function init() {
$countrymapper = new Mapper_Country();
    $this->addMultiOption(0, 'Please select...');
    foreach ($countrymapper->fetchAll() as $country) {
        $this->addMultiOption($country->getId(), $country->getName());
    }
$this->setLabel("Land:");
}
}

代码:

$addForm = new Form_Admin_Address_Neu();
$regionselect = new Form_Admin_Elements_RegionSelect('region_id');
$regionselect->setRequired(true);
$addForm->addElement($regionselect);
$countryselect = new Form_Admin_Elements_CountrySelect('country_id');
$countryselect->setRequired(true);
$addForm->addElement($countryselect);

if ($addForm->isValid($_POST)) {
...
} else {
print_r($_POST);
print_r($addForm->getErrorMessages());
print_r($addForm->getCustomMessages());
print_r($addForm->getErrors());
}

输出:

Array
(
[_street] => sdvdsvsv
[_zip] => 111111
[_city] => sdfgsf
[_lat] => 1.0
[_lng] => 2.1
[_hidden] => 0
[country_id] => 1
[region_id] => 3
[submit] => Senden
[csrf] => d18dfed9d26e28d7a52aa4983b00667e
)
Array
(
)
Array
(
)
Array
(
[_street] => Array
    (
    )

[_zip] => Array
    (
    )

[_city] => Array
    (
    )

[_lat] => Array
    (
        [0] => notFloat
    )

[_lng] => Array
    (
        [0] => notFloat
    )

[_hidden] => Array
    (
    )

[submit] => Array
    (
    )

[csrf] => Array
    (
    )

[region_id] => Array
    (
    )

[country_id] => Array
    (
    )

)

在我看来,验证失败,但我不知道为什么。 这些值存在于 $_POST 中,但表单不验证。 我什至尝试使用 isValidPartial(),但结果相同。 我认为我在做一些根本错误的事情。 一个提示会很棒。

提前

【问题讨论】:

  • 解决方案:我把它放在我的引导程序中,所以它在我的表单中没有“LocalizedToNormalized”-Filters 等工作:$locale = new Zend_Locale(); $locale->setLocale('en_US');

标签: zend-framework zend-form


【解决方案1】:

尝试在LatitudeLongitude 中输入comma 而不是point
1,02,1 而不是1.02.1

我认为这是您的验证器的Locale 的问题

【讨论】:

  • 你是对的,问题是数据库有浮点作为字段类型,所以内容没有被保存。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
相关资源
最近更新 更多