1
我的Zend表單isValid()
方法返回false,即使在任何地方都沒有錯誤顯示。因此,我不能執行isValid()
方法並將我的表單值存儲到mysql中。我不知道爲什麼它會在沒有錯誤時返回false。Zend表單isValid()返回false
我的控制器:
class IndexController extends AbstractActionController
{
private $usersTable;
public function indexAction()
{
return new ViewModel();
}
public function addAction()
{
$form = new Add();
if($this->request->isPost())
{
$complaintPost = new Post();
$form->bind($complaintPost);
$form->setInputFilter(new AddPost());
$form->setData($this->request->getPost());
$a = 'apple';
$b = 'boy';
if($form->isValid()) // Check whether it is running or not
{
echo "<script>console.log($a);</script>";
$complaintPost = $form->getData();
//"<script>console.log($this->getUsersTable()->insert($data));</script>";
$this->getUsersTable()->insert($complaintPost); //checking whether is it null
}
else
{
echo "<script>console.log($form->isValid());</script>";
}
}
return new ViewModel(array(
'form' => $form,
));
}
public function getUsersTable()
{
if(!$this->usersTable)
{
$this->usersTable = new TableGateway(
'complaint',
$this->getServiceLocator()->get('Zend\Db\Adapter\Adapter')
);
}
return $this->usersTable;
}
}
我PHTML文件:
<h1>Add Complaint</h1>
<?php
/** @var \Zend\Form\Form $form */
$form = $this->form;
$form->prepare();
echo $this->form()->openTag($form);
?>
<div class="form-group">
<?php echo $this->formRow($form->get('name'));?>
</div>
<div class="form-group">
<?php echo $this->formRow($form->get('icno'));?>
</div>
<div class="form-group">
<?php echo $this->formRow($form->get('address'));?>
</div>
<div class="form-group">
<?php echo $this->formRow($form->get('phoneno'));?>
</div>
<div class="form-group">
<?php echo $this->formRow($form->get('complaintdetail'));?>
</div>
<div class="form-group">
<?php echo $this->formRow($form->get('datetime'));?>
</div>
<div class="form-group">
<?php echo $this->formRow($form->get('image'));?>
</div>
<div class="form-group">
<?php echo $this->formRow($form->get('location'));?>
</div>
<div class="form-group">
<?php echo $this->formRow($form->get('remarks'));?>
</div>
<?php echo $this->formSubmit($form->get('submit'));?>
<?php echo $this->form()->closeTag();?>
我的形式:
class Add extends Form
{
public function __construct()
{
parent::__construct('add');
$this->setHydrator(new ClassMethods());
//More to add
$name = new Element\Text('name');
$name->setLabel("Name");
$name->setAttribute('class', 'form-control');
$nric = new Element\Text('icno');
$nric->setLabel("NRIC");
$nric->setAttribute('class', 'form-control');
$address = new Element\Text('address');
$address->setLabel("Address");
$address->setAttribute('class', 'form-control');
$phonenum = new Element\Text('phoneno');
$phonenum->setLabel("Phone Number");
$phonenum->setAttribute('class', 'form-control');
$complaintdetail = new Element\Text('complaintdetail');
$complaintdetail->setLabel("Things to Complaint");
$complaintdetail->setAttribute('class', 'form-control');
$dateHappen = new Element\Text('datetime');
$dateHappen->setLabel("Date and time");
$dateHappen->setAttribute('class', 'form-control');
$picture = new Element\File('image');
$picture->setLabel('Photo');
$picture->setAttribute('id', 'image');
$location = new Element\Text('location');
$location->setLabel("Location");
$location->setAttribute('class', 'form-control');
$remarks = new Element\Text('remarks');
$remarks->setLabel("Remarks");
$remarks->setAttribute('class', 'form-control');
$submit = new Element\Submit('submit');
$submit->setValue('Sent Complaint');
$submit->setAttribute('class', 'btn btn-primary');
$this->add($name);
$this->add($nric);
$this->add($address);
$this->add($phonenum);
$this->add($complaintdetail);
$this->add($dateHappen);
$this->add($picture);
$this->add($location);
$this->add($remarks);
$this->add($submit);
}
}
我的表單驗證:
class AddPost extends InputFilter
{
public function __construct()
{
$name = new Input('name');
$name->setRequired(true);
$name->setValidatorChain($this->getNameValidatorChain());
$name->setFilterChain($this->getStringTrimFilterChain());
$nric = new Input('icno');
$nric->setRequired(true);
$nric->setValidatorChain($this->getICValidatorChain());
$nric->setFilterChain($this->getStringTrimFilterChain());
$address = new Input('address');
$address->setRequired(true);
$address->setValidatorChain($this->getAddressValidatorChain());
$address->setFilterChain($this->getStringTrimFilterChain());
$phonenum = new Input('phoneno');
$phonenum->setRequired(true);
$phonenum->setValidatorChain($this->getPhoneValidatorChain());
$phonenum->setFilterChain($this->getStringTrimFilterChain());
$complaintdetail = new Input('complaintdetail');
$complaintdetail->setRequired(true);
$complaintdetail->setValidatorChain($this->getComplaintValidatorChain());
$complaintdetail->setFilterChain($this->getStringTrimFilterChain());
$datetime = new Input('datetime');
$datetime->setRequired(true);
$datetime->setFilterChain($this->getStringTrimFilterChain());
$picture = new Input('image');
$picture->setRequired(true);
$location = new Input('location');
$location->setRequired(true);
$location->setValidatorChain($this->getLocationValidatorChain());
$location->setFilterChain($this->getStringTrimFilterChain());
$remark = new Input('remarks');
$remark->setRequired(true);
$remark->setValidatorChain($this->getICValidatorChain());
$remark->setFilterChain($this->getStringTrimFilterChain());
$this->add($name);
$this->add($nric);
$this->add($address);
$this->add($phonenum);
$this->add($complaintdetail);
$this->add($datetime);
$this->add($picture);
$this->add($location);
$this->add($remark);
}
protected function getStringTrimFilterChain()
{
$filterChain = new FilterChain();
$filterChain->attach(new StringTrim());
return $filterChain;
}
protected function getLocationValidatorChain()
{
$stringLength = new StringLength();
$stringLength->setMin(3);
$stringLength->setMax(100);
$validatorChain = new ValidatorChain();
$validatorChain->attach(new Alnum(true));
$validatorChain->attach($stringLength);
return $validatorChain;
}
protected function getComplaintValidatorChain()
{
$stringLength = new StringLength();
$stringLength->setMin(3);
$stringLength->setMax(100);
$validatorChain = new ValidatorChain();
$validatorChain->attach(new Alnum(true));
$validatorChain->attach($stringLength);
return $validatorChain;
}
protected function getPhoneValidatorChain()
{
$stringLength = new StringLength();
$stringLength->setMin(3);
$stringLength->setMax(15);
$validatorChain = new ValidatorChain();
$validatorChain->attach(new Alnum(true));
$validatorChain->attach($stringLength);
return $validatorChain;
}
protected function getAddressValidatorChain()
{
$stringLength = new StringLength();
$stringLength->setMin(3);
$stringLength->setMax(70);
$validatorChain = new ValidatorChain();
$validatorChain->attach(new Alnum(true));
$validatorChain->attach($stringLength);
return $validatorChain;
}
protected function getICValidatorChain()
{
$stringLength = new StringLength();
$stringLength->setMin(3);
$stringLength->setMax(50);
$validatorChain = new ValidatorChain();
$validatorChain->attach(new Alnum(true));
$validatorChain->attach($stringLength);
return $validatorChain;
}
protected function getNameValidatorChain()
{
$stringLength = new StringLength();
$stringLength->setMin(3);
$stringLength->setMax(50);
$validatorChain = new ValidatorChain();
$validatorChain->attach(new Alpha(true));
$validatorChain->attach($stringLength);
return $validatorChain;
}
}
錯誤信息應該顯示你的文件有什麼問題。它可能太大,錯誤的文件類型或上傳文件時出現其他問題。 – Wilt
我試着用上面的代碼來檢查表單是否有效。我插入表單中的所有值並單擊提交按鈕,但它仍然表明我的值是如下所示的空值: –
表單無效Array([isEmpty] =>值是必需的,不能爲空)Array([isEmpty Array([isEmpty] =>值是必需的,不能爲空)Array([isEmpty] =>值是必需的,不能爲空)Array([數組([isEmpty] =>值是必需的,不能爲空)數組([isEmpty] =>值是必需的,不能爲空)數組[isEmpty] =>值是必需的,不能爲空)Array([isEmpty] =>值是必需的,不能爲空) –