\Zend\Form\Element\Collection validation as a whole - zend-framework2

I have form with collection. And i attach validation to whole collecion - i just want to check the existence of certain relations between elements of the collection.
And it works great. in the case of wrong data - form does not pass the "isValid()" test.
But there is one problem. formElementErrors / getMessages didnt return anything.
What i do wrong?
My form:
class Form implements InputFilterProviderInterface {
/**
* #return array
*/
public function getInputFilterSpecification()
{
return [
[
'name' => 'legend',
'required' => true,
'allowEmpty' => false,
'validators' => [
['name' => 'Callback', 'options' => [
'messages' => [
\Zend\Validator\Callback::INVALID_VALUE => 'Wrong',
],
'callback' => function ($values, $context=[]) {
return false;
},
]],
]
],
];
}
public function init()
{
$this->add(
[
'name' => 'legend',
'type' => 'Zend\Form\Element\Collection',
'options' => [
'label' => 'Legenda',
'count' => 2,
'should_create_template' => true,
'allow_add' => true,
'template_placeholder' => '__placeholder__',
'target_element' => [
'type' => 'Narzedzie\Form\Legenda\LegendyOpcjeFieldset',
],
],
]
);
}
}
And view:
$element = $NarzedzieForm->get('legend');
var_dump($element->getMessages()); // in case of error - empty array!
echo $this->formElementErrors($element); // in case of error - empty string
echo $this->formColleciton($element);

Maybe you need to add both messages?
'messages' => [
\Zend\Validator\Callback::INVALID_VALUE => 'Wrong VALUE',
\Zend\Validator\Callback::INVALID_CALLBACK => 'Wrong CALLBACK',
],
as perhaps the invalid callback message is being suppressed as you are only supplying one? I would hope it would fall back to the default. But then all this validator message stuff seems a bit stupid to me the way it is done.
Looks like you have an error in your callback, which might be throwing an exception and is being caught in the validator in the try catch statement maybe?
should be?
function ($values, $context=[]) {
foreach ($values as $value) {
if ($value['el'] == '1') return false;
}
return true;
},
as in $values not $value for the array in the foreach? Probably want to check that key is set too, with an isset($value['el'])?

Related

How to set selected option on choiceType sonata admin bundle

setting default/ selected option for choice field in symfony3 sonata admin bundle?
For example :
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
/**
* #inheritdoc
*/
public function configureFormFields(FormMapper $formMapper) {
parent::configureFormFields($formMapper);
$formMapper->add('type', ChoiceType::class, [
'label' => 'config.label_type',
'choices' => [
'config.label_permanent' => 'permanent',
'config.label_automatic' => 'automatic',
'config.label_temporary' => 'temporary'
],
'required' => false
]);
}
How to make the _permanent_ as selected value ?
This post doesn't help me out
setting default value in symfony2 sonata admin bundle
You can try with something like this:
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
/**
* #inheritdoc
*/
public function configureFormFields(FormMapper $formMapper) {
parent::configureFormFields($formMapper);
$subject = $this->getSubject();
if (null === $subject->getId()) {
$subject->setType('permanent');
}
$formMapper->add('type', ChoiceType::class, [
'label' => 'config.label_type',
'choices' => [
'config.label_permanent' => 'permanent',
'config.label_automatic' => 'automatic',
'config.label_temporary' => 'temporary'
],
'required' => false
]);
}

How to get the selected option of a radion button element in Zend Framework 2?

In a Fieldset I have an Element\Radio foo and Element\Text bar.
public function init()
{
$this->add(
[
'type' => 'radio',
'name' => 'foo',
'options' => [
'label' => _('foo'),
'value_options' => [
[
'value' => 'a',
'label' => 'a',
'selected' => true
],
[
'value' => 'b',
'label' => 'b'
]
]
]
...
]);
$this->add(
[
'name' => 'bar',
'type' => 'text',
'options' => [
'label' => 'bar',
...
],
...
]);
}
The validation of the field bar is depending on the selected foo option. It's easy to implement, if I can get the selected value of foo:
public function getInputFilterSpecification()
{
return [
'bar' => [
'required' => $this->get('foo')->getCheckedValue() === 'a',
...
],
];
}
But there is no method Radio#getCheckedValue(). Well, I can iterate over the $this->get('foo')->getOptions()['value_options'], but is it really the only way?
How to get (in the Fieldset#getInputFilterSpecification()) the selected option of a Zend\Form\Element\Radio?
The selected option gets POSTed to the server along with everything else from the HTML form and is all of this is available in validators through the $context array.
You can create a conditionally required field by using a callback validator and the $context array like this:
public function getInputFilterSpecification() {
return [
'bar' => [
'required' => false,
'allow_empty' => true,
'continue_if_empty' => true,
'required' => true,
'validators' => [
[
'name' => 'Callback',
'options' => [
'callback' => function ($value, $context) {
return $context['foo'] === 'a'
},
'messages' => [
\Zend\Validator\Callback::INVALID_VALUE => 'This value is required when selecting "a".'
]
]
]
]
],
];
}
That would check if 'foo' is equal to 'a', i.e. option 'a' is selected and return true when it is, which marks the input as valid, and false when it's not, marking the input invalid.

How to enable displaying the global label unsing FormMultiCheckbox in ZF2?

I'm using Zend\Form\Element\MultiCheckbox with Zend\Form\View\Helper\FormMultiCheckbox:
MyFieldset.php
// namespace ...;
// use ....;
class MyFieldset extends Fieldset
{
// ...
public function init()
{
parent::init();
$this->add(
[
'type' => 'multi_checkbox',
'name' => 'mymulticheckbox',
'options' => [
'label' => _('global label'),
'label_attributes' => [
'class' => 'col-md-3',
],
'value_options' => [
[
'value' => 'foo',
'label' => 'FOO',
],
[
'value' => 'bar',
'label' => 'BAR',
],
[
'value' => 'buz',
'label' => 'BUZ',
],
]
],
]
);
}
// ...
}
myform.phml
use Zend\Form\View\Helper\FormMultiCheckbox;
echo $this->formMultiCheckbox($myFieldset->get('mymulticheckbox'), FormMultiCheckbox::LABEL_PREPEND);
It works, but the "global label" is not displayed. It gets displayed, when I'm using Zend\Form\View\Helper\FormElement, but the FormMultiCheckbox seems to ignore the "global label".
How to make FormMultiCheckbox display the label of the checkbox list?
Have you tried with formRow(). For me it works. This does not appear to be managed in formMultiCheckbox(). See lines 182-193, file zend-form/src/View/Helper/FormRow.php.
// Multicheckbox elements have to be handled differently as the HTML standard does not allow nested
// labels. The semantic way is to group them inside a fieldset
if ($type === 'multi_checkbox'
|| $type === 'radio'
|| $element instanceof MonthSelect
|| $element instanceof Captcha
) {
$markup = sprintf(
'<fieldset><legend>%s</legend>%s</fieldset>',
$label,
$elementString
);

How to validate empty input in Zend Framework 2

I'm doing a registration form in ZF2, but I don't get how to validate. Validation seems not working.
I'm adding the validators array, but it doesn't work anyways. I don't know how can I fix that.
This is my code of controller:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Form\Formularios;
use Zend\Db\Adapter\Adapter;
use Application\Modelo\Entity\Usuarios;
class FormularioController extends AbstractActionController
{
public $dbAdapter;
public function indexAction()
{
return new ViewModel();
}
public function registroAction()
{
if($this->getRequest()->isPost())
{
$this->dbAdapter=$this->getServiceLocator()->get('Zend\Db\Adapter');
$u=new Usuarios($this->dbAdapter);
//echo "se recibiĆ³ el post";exit;
$data = $this->request->getPost();
$u->addUsuario($data);
return $this->redirect()->toUrl($this->getRequest()->getBaseUrl().'/application/formulario/registro/1');
}else
{
//zona del formulario
$form=new Formularios("form");
$id = (int) $this->params()->fromRoute('id', 0);
$valores=array
(
"titulo"=>"Registro de Usuario",
"form"=>$form,
'url'=>$this->getRequest()->getBaseUrl(),
'id'=>$id
);
return new ViewModel($valores);
}
}
}
this is my form code with validator
class Formularios extends Form
{
public function __construct($name = null)
{
parent::__construct($name);
$this->add(array(
'name' => 'name',
'required' => true,
'allow_empty' => false,
'options' => array(
'label' => 'Nombre Completo',
),
'attributes' => array(
'type' => 'text',
'class' => 'input'
),
'filters' => [ ['name' => 'StringTrim'], ],
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Ingrese Nombres.',
))))
));
$this->add(array(
'name' => 'lastname',
'required' => true,
'options' => array(
'label' => 'Apellido',
),
'attributes' => array(
'type' => 'text',
'class' => 'input'
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Ingrese Apellidos.',
))))
));
Thanks in advance
First problem.
$data = $this->request->getPost(); should be $data = $this->getRequest()->getPost();
Second problem is that you call your validators direclty when you build your form in the view, which is wrong. The right way to do is via an inputFilter. Now, there are many ways to to this, for example: with or without a factory called from your model or via the for class with a form element manager
I will show you the model way with a factory since it's easier for new comers.
namespace MyModule\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
class MyModel implements InputFilterAwareInterface
{
/**
* #var null $_inputFilter inputFilter
*/
private $_inputFilter = null;
// some more code like exhnageArray get/set method
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add(
$factory->createInput([
'name' => 'id',
'required' => false,
'filters' => [
['name' => 'Int'],
],
])
);
$inputFilter->add(
$factory->createInput([
"name"=>"title",
"required" => true,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
['name' => 'NotEmpty'],
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
'max' => 200,
],
],
],
])
);
$inputFilter->add(
$factory->createInput([
"name"=>"text",
"required" => true,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
['name' => 'NotEmpty'],
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 1,
],
],
],
])
);
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
Third proble. DO NOT EVER use serviceManager in controller. It's a really really really bad practice. Instead use a factory.

PHPUnit ZF2 InputFilter with Custom Validator

I have the following InputFilter:
<?php
namespace Login\InputFilter;
use Zend\InputFilter\InputFilter;
/**
* Class Login
*
* #package Login\InputFilter
*/
class Login extends InputFilter
{
/**
* Construct
*/
public function __construct()
{
/**
* Password
*/
$this->add(
[
'name' => 'password',
'required' => true,
'filters' => [
[
'name' => 'stringtrim'
]
],
'validators' => [
[
'name' => 'stringlength',
'options' => [
'min' => '5',
'max' => '128'
],
'break_chain_on_failure' => true
],
[
'name' => 'regex',
'options' => [
'pattern' => '/^[^\\\' ]+$/'
],
'break_chain_on_failure' => true
]
]
]
);
}
/**
* Init
*/
public function init()
{
/**
* Employee ID
*/
$this->add(
[
'name' => 'employeeId',
'required' => true,
'filters' => [
[
'name' => 'stringtrim'
]
],
'validators' => [
[
'name' => 'stringlength',
'options' => [
'min' => '1',
'max' => '20'
],
'break_chain_on_failure' => true
],
[
'name' => 'digits',
'break_chain_on_failure' => true
],
[
'name' => 'Login\Validator\EmployeeId',
'break_chain_on_failure' => true
]
]
]
);
}
}
Attached to the employeeId is a custom validator I've created to check if the Employee ID actually exists in a database. It has a constructor for Doctrine Entity Manager. This works fine when testing via the web, so no worries there.
However now I would like to test via PHPUnit and I've created the following test:
<?php
namespace LoginTest\InputFilter;
use Login\InputFilter\Login;
/**
* Class LoginTest
*
* #package LoginTest\InputFilter
*/
class LoginTest extends \PHPUnit_Framework_TestCase
{
/**
* #var Login $inputFilter
*/
protected $inputFilter;
public function setUp()
{
$this->inputFilter = new Login();
$this->inputFilter->init();
parent::setUp();
}
public function testFormHasElements()
{
$inputs = $this->inputFilter->getInputs();
$this->assertArrayHasKey(
'employeeId',
$inputs
);
$this->assertArrayHasKey(
'password',
$inputs
);
}
}
When the test runs the following error is produced:
1) LoginTest\InputFilter\LoginTest::testFormHasElements
Argument 1 passed to Login\Validator\EmployeeId::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called in /vhosts/admin-application/vendor/zendframework/zendframework/library/Zend/ServiceManager/AbstractPluginManager.php on line 180 and defined
I'm not certain how I can get passed this particular error. I assume I need to use Mockery but I'm not certain.
The validator has a Factory which supplies the Doctrine Entity Manager from the Service Locator.
I am still very new to PHPUnit but I've been trying to do my research before asking here.
Any ideas?
You're getting this error because you directly instantiate you input filter and it isn't then aware of your custom validator factory.
In real application InputFilter is using Zend\Validator\ValidatorPluginManager for getting validators from service manager.
I see two ways how to solve this problem:
1.) You can setup real service manager from application configuration, like it's described in documentation and then pull the input filter from service manager:
$inputFilter = Bootstrap::getServiceManager()->get(\Login\InputFilter\Login::class); // change the service name if you have another
This solution is good if you want to write some kind of integration tests.
2.) You can mock your custom validator and inject into ValidatorPluginManager in setup method:
protected function setUp()
{
$validator = $this->getMockBuilder(\Login\Validator\EmployeeId::class)->getMock();
$inputFilter = new Login();
$inputFilter->getFactory()
->getDefaultValidatorChain()
->getPluginManager()
->setService(\Login\Validator\EmployeeId::class, $validator);
$inputFilter->init();
$this->inputFilter = $inputFilter;
parent::setUp();
}
This solution is good if you want to write unit tests for Login input filter.

Resources