I am validating the form. Whenever the field length for the item exceeds a certain limit I do get the error but I do not get the error message, that is the messages array is empty. Please advise what could be going wrong there. Attached are the complete form and the corresponding validation files.
<?php
namespace Application\Form;
use Laminas\Form\Form;
use Laminas\Form\View\Helper\FormInput;
use Laminas\Form\Element;
use Application\Validator\ItemValidator;
/**
* This form is used to collect ItemForm data.
*/
class ItemForm extends Form
{
/**
* Entity manager.
* @var Doctrine\ORM\EntityManager
*/
private $entityManager = null;
/**
*
* @var Application\Entity\Item
*/
private $iItem = null;
/**
* Constructor.
*/
public function __construct($translator,$entityManager = null, $iItem = null)
{
// Define form name
parent::__construct('item-form');
// Set POST method for this form
$this->setAttribute('method', 'post');
// Save parameters for internal use.
$this->entityManager = $entityManager;
$this->iItem = $iItem;
$this->setAttribute('novalidate', true);
$this->addElements($translator);
$this->addInputFilter();
}
/**
* This method adds elements to form (input fields and submit button).
*/
protected function addElements($translator)
{
$this->add([
'type' => 'textarea',
'name' => 'item-description',
'attributes' => [
'id' => 'item-description',
'class' => "form-control",
'rows' => "15",
'placeholder' => $translator->translate('Enter Item Description'),
'required' => false
],
'options' => [
'label' => $translator->translate('Item Description'),
],
]);
// Add the CSRF field
$this->add([
'type' => 'csrf',
'name' => 'item_csrf',
'options' => [
'csrf_options' => [
'timeout' => 600
]
],
]);
// Add the submit button
$this->add([
'type' => 'submit',
'name' => 'submit-iItem',
'attributes' => [
'value' => $translator->translate('Submit'),
'id' => 'submit-iItem',
'class' => "btn btn-info",
],
]);
}
/**
* This method creates input filter (used for form filtering/validation).
*/
private function addInputFilter()
{
// Create main input filter
$inputFilter = $this->getInputFilter();
$inputFilter->add([
'name' => 'item-description',
'required' => false,
'filters' => [
['name' => 'StringTrim'],
],
'validators' => [
[
'name' => ItemValidator::class,
'options' => [
'entityManager' => $this->entityManager,
'itemType' => 'item-description',
'max' => 25,
],
],
],
]);
}
}
/******VALIDATOR/
<?php
namespace Application\Validator;
use Laminas\Validator\AbstractValidator;
use Laminas\Validator\InArray;
use Laminas\Validator\Uri;
/**
* ItemValidator
*/
class ItemValidator extends AbstractValidator
{
protected $maximum = 0;
protected $company = null;
protected $item_type = '';
protected $messageVariables = array(
'max' => 'maximum',
'item_type' => 'item_type',
);
/**
* Available validator options.
* @var array
*/
protected $options = [
'itemType' => null,
'entityManager' => null,
];
// Validation failure message IDs.
const NOT_SCALAR = 'notScalar';
const ITEM_DO_NOT_EXIST = 'itemDoNotExist';
const ITEM_DESCRIPTION_TOO_LONG = 'itemDescriptionTooLong';
/**
* Validation failure messages.
* @var array
*/
protected $messageTemplates = [
self::NOT_SCALAR => [
'errorType' => 'NOT_SCALAR',
'itemType' => '%item_type%',
],
self::ITEM_DO_NOT_EXIST => [
'errorType' => 'ITEM_DO_NOT_EXIST',
'itemType' => '%item_type%',
],
self::ITEM_DESCRIPTION_TOO_LONG => [
'errorType' => 'ITEM_DESCRIPTION_TOO_LONG',
'itemType' => '%item_type%',
'itemValue' => '%value%',
'maxValue' => '%max%'
]
];
/**
* Constructor.
*/
public function __construct($options = null)
{
// Set filter options (if provided).
if(is_array($options)) {
if(isset($options['itemType']))
$this->options['itemType'] = $options['itemType'];
if(isset($options['entityManager']))
$this->options['entityManager'] = $options['entityManager'];
if(isset($options['max']))
$this->maximum = $options['max'];
}
// Call the parent class constructor
parent::__construct($options);
}
/**
* Check if item length is less than max specified and there is no other
* item exists with the same name.
*/
public function isValid($value)
{
if(!is_scalar($value)) {
$this->error(self::NOT_SCALAR);
return false;
}
$this->setValue($value);
$isItemAlreadyExistValid = true;
if(($this->options['itemType'] == 'item-description'))
$isItemAlreadyExistValid = $this->checkIfItemAlreadyExist();
//Check if name length is not more than the specified max value
if(($this->options['itemType'] == 'item-description'))
$isItemDescriptionLengthValid = $this->checkItemDescriptionLength($value);
$isValid = true;
//if any of the above criteria is invalid, isValid is false
if(!$isItemAlreadyExistValid || !$isItemDescriptionLengthValid )
$isValid = false;
// Return validation result.
return $isValid;
}
protected function checkIfItemAlreadyExist(){
$isItemAlreadyExistValid = true;
$entityManager = $this->options['entityManager'];
$itemType = $this->options['itemType'];
$item = null;
if($itemType == 'item-description')
$item = $entityManager->getRepository(DataBreachItem::class)->findByCompany($company);
//invalid if item don't exist
if(is_null($item)){
$isItemAlreadyExistValid = false;
$this->error(self::ITEM_DO_NOT_EXIST);
}
return $isItemAlreadyExistValid;
}
//Check if name length is not more than the specified max value
protected function checkItemDescriptionLength($value) {
$isItemDescriptionLengthValid = true;
$itemLength = strlen($value);
if ($itemLength > $this->maximum) {
$isItemDescriptionLengthValid = false;
$this->error(self::ITEM_DESCRIPTION_TOO_LONG);
}
return $isItemDescriptionLengthValid;
}
}