gpt4 book ai didi

forms - $form->isValid 说它无效,但我的表单没有显示任何错误消息 - Zend Framework 2

转载 作者:行者123 更新时间:2023-12-04 08:55:44 38 4
gpt4 key购买 nike

我的 CRUD 应用程序中的 addAction 有问题。在 Controller 上,逻辑没有通过 $form->isValid() 验证,但表单没有显示任何错误消息。

我试过这个(谢谢山姆):

foreach($form->get('product')->getElements() as $el)
{
echo $el->getName()." = ".$el->getValue()." > ".$el->getMessages()." <br/>";
}

那只显示字段的名称和值,但不显示错误消息。

我试过让表单完全空白,它会触发“值是必需的,不能为空”错误消息,但是,我一个接一个地填写每个字段,直到我没有收到更多错误消息但表单仍然无效.

我的表单有一个产品字段集作为基本字段集和一个提交按钮。在产品字段集中,我有一个 ID 字段、一个名称字段、一个价格字段和一个品牌字段集。在我的品牌字段集中,我有一个 id 字段。像这样:

产品形式:
class ProductForm extends Form
{

public function init()
{
// we want to ignore the name passed
parent::__construct('product');
$this->setName('product');
$this->setAttribute('method', 'post');

$this->add(array(
'name' => 'product',
'type' => 'Administrador\Form\ProductFieldset',
'options' => array(
'use_as_base_fieldset' => true
),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Add',
'id' => 'submitbutton',
),
));
}
}

产品字段集:
class ProductFieldset extends Fieldset implements ServiceLocatorAwareInterface
{

protected $serviceLocator;

function __construct($name = null)
{
parent::__construct('product_fieldset');

$this->setHydrator(new ArraySerializableHydrator());
$this->setObject(new Product());
}

public function init()
{

$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'name',
'type' => 'Text',
'options' => array(
'label' => 'Name',
),
));
$this->add(array(
'name' => 'price',
'type' => 'Text',
'options' => array(
'label' => 'Price',
),
));
$this->add(array(
'name' => 'brand',
'type' => 'BrandFieldset',
));
}


public function setServiceLocator(ServiceLocatorInterface $sl)
{
$this->serviceLocator = $sl;
}

public function getServiceLocator()
{
return $this->serviceLocator;
}
}

品牌字段集:
class BrandFieldset extends Fieldset
{
function __construct(BrandTable $brandTable)
{
parent::__construct('brand_fieldset');

//$this->setHydrator(new ClassMethodsHydrator(false))->setObject(new Brand());

$this->setHydrator(new ArraySerializableHydrator());
$this->setObject(new Brand());

$brandSelectOptionsArray = $brandTable->populateSelectBrand();
$this->add(array(
'name' => 'id',
'type' => 'Select',
'options' => array(
'label' => 'Brand',
'empty_option' => 'Please select a brand',
'value_options' => $brandSelectOptionsArray,
),
));
}
}

这是我在 addAction 中的新 Form 语句:
$formManager = $this->serviceLocator->get('FormElementManager');
$form = $formManager->get('Administrador\Form\ProductForm');

在我的模型“产品”中,我有 inputFilters、“id”字段所需的过滤器、“name”字段所需的过滤器。对于 Brand 字段,我创建了其他 inputFilter 并将其添加到主 inputFilter:
$brandFilter->add($factory->createInput(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));
$inputFilter->add($brandFilter, 'brand');

奇怪的行为是我的 editAction 工作正常并且具有相同的逻辑。

是否有任何形式从表单中回显内部错误消息,这有助于我理解为什么表单无效。

编辑 2013-06-01

这是我的完整 Controller :
class ProductController extends AbstractActionController
{

protected $productTable;
protected $brandTable;

public function indexAction()
{
return new ViewModel(array(
'products' => $this->getProductTable()->fetchAll(),
));
}

public function addAction()
{
$formManager = $this->serviceLocator->get('FormElementManager');
$form = $formManager->get('Administrador\Form\ProductForm');
$form->get('submit')->setValue('Add');

$request = $this->getRequest();
if ($request->isPost()) {
$product = new Product();
$product->brand = new Brand();
$form->setInputFilter($product->getInputFilter());
$form->setData($request->getPost());

if ($form->isValid()) {
$product->exchangeArray($form->getData());
$this->getProductTable()->saveProduct($product);

// Redirect to list of products
return $this->redirect()->toRoute('product');
}
}
return new ViewModel(array(
'form' => $form,
));
}

public function editAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('product', array(
'action' => 'add'
));
}

// Get the Product with the specified id. An exception is thrown
// if it cannot be found, in which case go to the index page.
try {
$product = $this->getProductTable()->getProduct($id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('product', array(
'action' => 'index'
));
}
$formManager = $this->serviceLocator->get('FormElementManager');
$form = $formManager->get('Administrador\Form\ProductForm');
$brand = $this->getBrandTable()->getBrand($product->brand);
$product->brand = $brand;
$form->bind($product);
$form->get('submit')->setAttribute('value', 'Edit');

$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter($product->getInputFilter());
$form->setData($request->getPost());

if ($form->isValid()) {
$this->getProductTable()->saveProduct($form->getData());

// Redirect to list of products
return $this->redirect()->toRoute('product');
}
}

return array(
'id' => $id,
'form' => $form,
);
}

public function deleteAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('product');
}

$request = $this->getRequest();
if ($request->isPost()) {
$del = $request->getPost('del', 'No');

if ($del == 'Yes') {
$id = (int) $request->getPost('id');
$this->getProductTable()->deleteProduct($id);
}

// Redirect to list of products
return $this->redirect()->toRoute('product');
}

return array(
'id' => $id,
'product' => $this->getProductTable()->getProduct($id)
);
}


public function getProductTable()
{
if (!$this->productTable) {
$sm = $this->getServiceLocator();
$this->productTable = $sm->get('Administrador\Model\ProductTable');
}
return $this->productTable;
}

public function getBrandTable()
{
if (!$this->brandTable) {
$sm = $this->getServiceLocator();
$this->brandTable = $sm->get('Administrador\Model\BrandTable');
}
return $this->brandTable;
}
}

最佳答案

我的情况是我通过了错误的输入过滤器。 isValid返回 false,但 $form->getMessages()是空的。表格 OrderForm有以下内容:

$form->setInputFilter(new \Application\Form\UserInputFilter($er));

当我更改 UserInputFilterOrderInputFilter有用。

关于forms - $form->isValid 说它无效,但我的表单没有显示任何错误消息 - Zend Framework 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16867533/

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