gpt4 book ai didi

php - Zend framework 2 - 提交表单错误时使表单字段变粘

转载 作者:搜寻专家 更新时间:2023-10-31 21:27:36 25 4
gpt4 key购买 nike

我正在研究 Zend 框架 2。我有一种用于保存商店信息的表格。我已经使用 Zend 的输入过滤器验证了表单。我的问题是,当我在字段中输入错误数据或将必填字段留空时,它会正确显示错误,但整个表单再次变为空白。

我想要以前输入的值,因为它在表单显示错误时是原样。

以下是呈现表单的函数。

public function settingsAction()
{
try {

$message = '';
$error = '';
$id = 0;
try {
$shop = $this->_getShop();
} catch (\Exception $ex) {
AppLogger::log($ex);
$error = $ex->getMessage();
}
if ($shop) {
$id = $shop->id;
}
else {
//redirect to login page
return $this->redirect()->toUrl($this->_getDomainUrl());
}

$form = new ShopForm($this->serviceLocator);
$config = $this->serviceLocator->get('config');
$apiUrls = $config['apiUrls'];

$request = $this->getRequest();
if ($request->isPost())
{
if (!$shop)
$shop = new Shop();
$form->setInputFilter($shop->getInputFilter());
$form->setData($request->getPost());

if ($form->isValid()) {
$url = $shop->url;
$token = $shop->token;
$config_id = $shop->config_id;
$password = $shop->password;
$shop->exchangeArray($form->getData(),false);
$shop->url = $url;
$shop->token = $token;
$shop->config_id = $config_id;
DAL::getShopTable($this->serviceLocator)->saveShop($shop);
$message = "Your settings has been saved successfully.";
}
else {
$error = 'There are values that need to be completed before you can save/update your preferences.';

foreach($form->getMessages() as $msg) {
$error = $error . $msg;
}
}
}

if ($shop)
{
echo "<pre>";print_r($shop);
$shop->selected_countries = unserialize($shop->selected_countries);
$form->bind($shop);
$form->get('return_address_country')->setAttribute('value', $shop->return_address_country);
}

$form->get('submit')->setAttribute('value', 'Save');
return array(
'id' => $id,
'form' => $form,
'apiUrls' => $apiUrls,
'message' => $message,
'uri' => $this->_selfURL(),
"error" => $error

);
}
catch (\Exception $ex) {
AppLogger::log($ex);
throw $ex;
}
}

我已使用 $form->setInputFilter($shop->getInputFilter()); 进行验证。 getInputFilter() 的一个片段如下:

public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();

$inputFilter->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'ship_to_code',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 0,
'max' => 50,
),
),
),
));
$inputFilter->add(array(
'name' => 'default_phone',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 0,
'max' => 50,
),
),
),
));
$inputFilter->add(array(
'name' => 'max_records_per_file',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
}

表格是

$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'ship_to_code',
'type' => 'Text',
'options' => array(
'label' => 'Ship-To Code',
),
));
$this->add(array(
'name' => 'default_phone',
'type' => 'Text',
'options' => array(
'label' => 'Default Phone',
),
));

最佳答案

您可以在设置操作中使用表单的 setdata() 方法。这是更新的代码

 if ($shop) 
{
// echo "<pre>";print_r($shop);
// $shop->selected_countries = unserialize($shop->selected_countries);
$form->bind($shop);
$form->setData($request->getPost()); // set post data to form
}

关于php - Zend framework 2 - 提交表单错误时使表单字段变粘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33889705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com