Incorporate custom validation error messages into form object by element - zend-framework2

I have the following code which creates a specific text element:
$this->add([
'type' => 'text',
'name' => 'newpassword',
'attributes' => [
'id' => 'newpassword',
'class' => 'form-control'
],
'options' => [
'label' => 'Enter New User Password',
],
]);
And I have the following code which produces my input filter definitions:
$inputFilter->add([
'name' => 'newpassword',
'required' => true,
'filters' => [
['name' => 'StringTrim'],
['name' => 'StripTags']
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 6,
'max' => 256
],
]
],
]);
What I want to accomplish is adding my custom messages. Here's the way they have it in the api documentation:
$validator = new Zend\Validator\StringLength(array('min' => 8, 'max' => 12));
$validator->setMessages( array(
Zend\Validator\StringLength::TOO_SHORT =>
'The string \'%value%\' is too short',
Zend\Validator\StringLength::TOO_LONG =>
'The string \'%value%\' is too long'
));
How do I incorporate my custom validation messages into my already programmed code?
UPDATE:
I think this is where i will find success, but not sure how to do it:
$inputFilter->get('newpassword')->getValidatorChain()->

Use this-: its messageTemplates to set custom message
$inputFilter->add([
'name' => 'newpassword',
'required' => true,
'filters' => [
['name' => 'StringTrim'],
['name' => 'StripTags']
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 6,
'max' => 256,
'messageTemplates'=>array(
\Zend\Validator\StringLength::TOO_SHORT =>
'The string \'%value%\' is too short',
\Zend\Validator\StringLength::TOO_LONG =>
'The string \'%value%\' is too long'
)
],
]
],
]);

Related

How to make Textarea not resizable in Zend Form

I have a Zend Form and one of it's fields is a Textarea. I have set it's default size using 'rows' and 'cols' but I want the user not to be able to change it.
I have tried adding 'resize' => false, but it did not work.
public function __construct()
{
parent::__construct('form');
$this->setAttribute('method', 'post');
$this->setAttribute('role', 'form');
$this->add([
'name' => 'feedback',
'type' => Textarea::class,
'attributes' => [
'id' => 'feedback',
'class' => 'mdc-text-field__input',
'rows' => 3,
'cols' => 4,
'required' => false,
],
'options' => [
'label' => 'Feedback',
],
]);
}
You have the choice between
attributes => [
...,
'style' => 'resize:none'
]
or use a css class
attributes => [
...,
'class' => 'notresizable'
]
and define this css class in your css file.

Zend Framework - routing same routes to different controller

I have two routes and want to match both routes when some parameter exists in request.
Route 1:
'companies' => [
'type' => Segment::class,
'options' => [
'route' => '/api/v1/companies[/:id]',
'defaults' => [
'controller' => V1\Rest\Controller\CompaniesController::class,
]
],
'priority' => 2,
'may_terminate' => true,
],
Route 2:
'company_members' => [
'type' => Segment::class,
'options' => [
'route' => '/api/v1/companies[/:id][/:members][/:member_id]',
'defaults' => [
'controller' => V1\Rest\Controller\CompanyMembersController::class,
]
],
'priority' => 2,
'may_terminate' => true,
],
I want to use CompanyMembersController when members exists in the request and CompaniesController when members doesnt exists .But it is not working.
Your issue is in the second route where you defined members as a parameter [/:members]. You should change this to a /members.
I also would recommend to use constraints for your route parameters. Your routes should look like:
'companies' => [
'type' => Segment::class,
'options' => [
'route' => '/api/v1/companies[/:id]',
'defaults' => [
'controller' => Controller\CompaniesController::class,
'action' => 'index',
],
'constraints' => [
'id' => '\d+'
]
],
'priority' => 2,
'may_terminate' => true,
],
'company_members' => [
'type' => Segment::class,
'options' => [
'route' => '/api/v1/companies[/:id]/members[/:member_id]',
'defaults' => [
'controller' => Controller\CompanyMembersController::class,
'action' => 'index',
],
'constraints' => [
'id' => '\d+',
'member_id' => '\d+',
]
],
'priority' => 2,
'may_terminate' => true,
],
Also you can see the constraints to parameters id & member_id to integers.
References
zend mvc routing
understanding routing
routing and controllers

ZF2 segment route with file extension

I have a route to an action which returns a pdf file. Now it would be fine if the url would contain the file extension .pdf. If the last part of my route is not a segment, it should work but not in my case.
Works
'my_route' => [
'type' => 'segment',
'may_terminate' => true,
'options' => [
'route' => '/my-file.pdf',
'defaults' => [
'action' => 'file'
]
]
],
Does not work
'my_route' => [
'type' => 'segment',
'may_terminate' => true,
'options' => [
'route' => '/my-file/:year.pdf',
'constraints' => [
'year' => '\d{4}'
],
'defaults' => [
'action' => 'file',
'year' => date('Y')
]
]
],
This is an example using Regex route (not fully tested):
'my_route' => [
'type' => 'regex',
'may_terminate' => true,
'options' => [
'route' => '/my-file/(?<year>\d{4}).pdf?',
'constraints' => [
'defaults' => [
'action' => 'file',
'year' => date('Y')
]
]
],
See the docs : https://framework.zend.com/manual/2.4/en/modules/zend.mvc.routing.html#zend-mvc-router-http-regex
You can adjust it to this:
'route' => '/my-file/:year:.pdf',
Note the extra : after the parameter

How to make MultiCheckBox Label bold or strong?

I have the following multicheckbox:
$this->add(array(
'type' => 'DoctrineModule\Form\Element\ObjectMultiCheckbox',
'name' => 'directPractice',
'options' => array(
'label' => 'A. Check all direct practice field education assignments',
**EDIT 1:**
'label_attributes' => array(
'class' => 'label-multicheckbox-group'
),
'object_manager' => $this->getObjectManager(),
'target_class' => 'OnlineFieldEvaluation\Entity\FieldEducationAssignments', //'YOUR ENTITY NAMESPACE'
'property' => 'directPractice', //'your db collumn name'
'value_options' => array(
'1' => 'Adults',
'2' => 'Individuals',
'3' => 'Information and Referral',
'4' => 'Families',
),
),
'attributes' => array(
'value' => '1', //set checked to '1'
'multiple' => true,
)
));
How to make the following part bold?
Using label_attributes makes all labels bold, and I want just the main label for the multibox be bold.
'label' => 'A. Check all direct practice field education assignments',
EDIT 1: add label_attributes to "options"
When I add label_attributes as #itrascastro suggested, all labels become bold and I want only the top one to become bold: multicheckbox
Try to add this to your options:
'options' => array(
'label_attributes' => array(
'class' => 'myCssBoldClass'
),
// more options
),
Each value option accepts an array of options.
For example
'value_options' => [
[
'label' => 'Adults',
'label_attributes' => [
'class' => 'my-bold-class',
],
'attributes' => [],
'value' => 1,
'selected' => false,
'disabled' => false,
],
'2' => 'Individuals',
'3' => 'Information and Referral',
'4' => 'Families',
],
I've added in some other values you can define; they are obviously optional.
Since there is no build in option to do this just for the top label, I used jQuery to accomplish this:
$("label[for]").each(function(){
$(this).addClass("label-bold");
});

Zend Framework 2 inputfilter incorrect behaviour

there is a more than strange behavior of filterInput, the getting filter function itself is:
public function getInputFilter($id = null){
if (!$this->inputFilter){
$inputFilter = new InputFilter();
$factory = new InputFactory();
$id = intval($id);
$inputFilter->add($factory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
array(
'name' => 'Db\NoRecordExists',
'options' => array(
'field' => 'name',
'table' => 'table',
'adapter' => $this->dbAdapter,
'message' => 'record exists',
'exclude' => array(
'field' => 'id',
'value' => $id
)
),
)
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
setting the filter like:
$form->setInputFilter($model->getInputFilter($id));
Now, when we fire $form->isValid(), the validation error will rise about duplication in the database, if I will remove Db\NoRecordExists validator, the database will contain 2 records! more interesting, if I will set 'required' => false, there will be no double inserting, the same with adding second validation field. Working settings are:
public function getInputFilter($id = null){
if (!$this->inputFilter){
$inputFilter = new InputFilter();
$factory = new InputFactory();
$id = intval($id);
$inputFilter->add($factory->createInput(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
array(
'name' => 'Db\NoRecordExists',
'options' => array(
'field' => 'name',
'table' => 'table',
'adapter' => $this->dbAdapter,
'message' => 'record exists',
'exclude' => array(
'field' => 'id',
'value' => $id
)
),
)
),
)));
//test field
$inputFilter->add($factory->createInput(array(
'name' => 'id',
'required' => false,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
array('name' => 'Int')
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
So it fails to work correctly with filter config for only one field.. Does anybody knows the reason?

Resources