ZF2 how to set message of required field when it's empty? - php

I have an input filter...
$this->inputFilter->add($factory->createInput([
'name' => 'reason',
'required' => true,
'filters' => [
[
'name' => 'StripTags'
]
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 10,
'max' => 150
]
]
]
]));
When the length is 0, the 'required' error kicks in (appears to use NotEmpty validator.) This gives a very generic message "Value is required and can't be empty."
Since I am showing all errors in list above the form and not next to their input, this is not specific enough.
I am assuming there is a 'messages' key like in the validators array, but I cannot find any documentation on it.
How can I set the message for an empty input?

I dug through the InputFactory code and found a few things...
continue_if_empty will allow an empty field and still run the validators.
$this->inputFilter->add($factory->createInput([
'name' => 'reason',
'continue_if_empty' => true,
'filters' => [
[
'name' => 'StripTags'
]
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 10,
'max' => 150,
'messages' => [
\Zend\Validator\StringLength::TOO_SHORT => 'The reason must be greater than %min% characters.',
\Zend\Validator\StringLength::TOO_LONG => 'The reason must be less than %max% characters.'
]
]
]
]
]));
You could also add the error_message config. It appears it will always show this error and no others regardless of what happens. This is fine since the only validator is StringLength.
$this->inputFilter->add($factory->createInput([
'name' => 'reason',
'error_message' => 'The reason must be between 10 and 150 characters in length.',
'filters' => [
[
'name' => 'StripTags'
]
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 10,
'max' => 150,
]
]
]
]));
If you still require a "not empty" message, you should be able to add a NotEmpty validator, customize its message, and use continue_if_empty => true config.

Related

How to validate an array inside an array payload in laravel

How to validate a request like for this example i want to create a custom validation for qualities based on a group type. I know how to create a custom validation for laravel but for the example below i want to create a validation for quality type based on its group type.
The payload below it just for demonstration.
$payload = [
'groups' => [
[
'type' => 'human',
'qualities' => [
[
'type' => 'hair',
'value' => 'blue'
],
[
'type' => 'height',
'value' => '188cm'
],
]
],
[
'type' => 'cat',
'qualities' => [
[
'type' => 'hair',
'value' => 'yellow'
]
]
]
]
];
You can use the wildcard, for example:
$request->validate([
'payload.*' => 'required|array',
'payload.*.type' => 'required',
'payload.*.qualities' => 'required|array',
'payload.*.qualities.*' => 'required'
]);

Implement suggest function of elasticsearch-php client in my API

I am trying to implement the suggest function of elasticsearch-php client in my API to suggest people some already existing problems.
I have made index for my problems
'index' => 'newproblemindex',
'body' => [
'settings' => [
'number_of_shards' => 3,
'number_of_replicas' => 2
],
'mappings' => [
'newproblems' => [ // type of index
'_source' => [
'enabled' => true
],
'properties' => [
'title' => [
'type' => 'text',
'analyzer' => 'standard'
],
'description' => [
'type' => 'text',
'analyzer' => 'standard'
], 'suggest' => [
'type' => 'completion'
]
]
]
]
]
But I am unable to find which param fields to use to implement suggest function
'index' => 'newproblemindex',
'body' => [
'try' => [
'text' => $request->search_key,
'completion' => [ 'text' => 'suggest' ]
]
],
I am using laravel and taking search_key as request param but I am getting "invalid_type_name_exception" and when I tried to give the type name, it is again giving me some error.
"suggest" => [
"song-suggest" => [
"prefix" => $request->search_key,
"completion" => ["field" => "suggest"]
]
]
I am getting error "suggest is not a valid param ". Please help
and Thanks in advance.

How to validate uploaded images with zend-inputfilter

I have a form that accepts a text field and an image, and I would like to be able to validate or filter the images that are uploaded.
I try to follow this tutorial on GitHub (https://github.com/lowtower/zend-expressive2-tutorial)
I am using zend expressive 2. I have this approach:
<?php
namespace Admin\Model\InputFilter;
use Zend\InputFilter\InputFilter;
class EventoInputFilter extends InputFilter
{
public function init()
{
$this->add([
'name' => 'eventoNombre',
'required' => true,
'filters' => [
['name' => 'StripTags'],
['name' => 'StringTrim'],
],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 5,
'max' => 100,
],
],
],
]);
$this->add([
'name' => 'imagenes[]',
'required' => true,
'validators' => [
[
'name' => 'FileUploadFile'
],
[
'name' => 'FileMimeType',
'options' => [
'mimeType' => ['image/jpeg', 'image/png']
]
],
[
'name' => 'FileIsImage'
],
[
'name' => 'FileImageSize',
'options' => [
'minWidth' => 128,
'minHeight' => 128,
'maxWidth' => 4096,
'maxHeight' => 4096
]
],
],
'filters' => [
[
'name' => 'FileRenameUpload',
'options' => [
'target' => 'public/eventos/',
'useUploadName' => true,
'useUploadExtension' => true,
'overwrite' => true,
'randomize' => false
]
]
],
]);
}
}
but something is wrong and I do not validate it correctly when I upload a correct image.

Zend Framework 2 - Use custom validators in input filter

I want to use a custom validator with the input filter add method. I am using the latest version of the zend-validator package.
In my module.config.php I have this:
'validators' => [
'invokables' => [
'DoctrineRecordExists' => 'Utils\Validator\Doctrine\RecordExists',
'NoDoctrineRecordExists' => 'Utils\Validator\Doctrine\NoRecordExists',
],
]
And I use it here:
$this->add([
'name' => 'name',
'required' => true,
'validators' => [
[
'name' => 'StringLength',
'options' => [
'min' => 3,
'max' => 20,
],
],
[
'name' => 'NoDoctrineRecordExists',
'options' => [
'repository' => 'Account\Entity\OAuthUser',
'field' => 'name',
'entityManager' => $this->getEntityManager(),
],
],
],
]);
But the constructor of NoRecordExists is never called. I get a 500 Error with no Exceptions available. If I remove the validator from the input filter, it works as expected.
I also tried this in my Module.php
public function getValidatorConfig() {
return [
'invokables' => [
'DoctrineRecordExists' => 'Utils\Validator\Doctrine\RecordExists',
'NoDoctrineRecordExists' => 'Utils\Validator\Doctrine\NoRecordExists',
],
];
}
But there was no difference.

How do you define a Cassandra CollectionMap nested in a UDT with duoshuo's PHP client library?

I have a CollectionSet<UDT> where UDT contains a CollectionMap<int,boolean>. I have not been able to find any documentation or example of how to define this when creating a new Cassandra\Type\CollectionSet for inserting into the table. There is a great example with a CollectionList (found here) which is like this:
// CollectionSet<UDT>, where UDT contains: Int, Text, Boolean,
// CollectionList<Text>, CollectionList<UDT>
new Cassandra\Type\CollectionSet([
[
'id' => 1,
'name' => 'string',
'active' => true,
'friends' => ['string1', 'string2', 'string3'],
'drinks' => [['qty' => 5, 'brand' => 'Pepsi'], ['qty' => 3, 'brand' => 'Coke']]
],[
'id' => 2,
'name' => 'string',
'active' => false,
'friends' => ['string4', 'string5', 'string6'],
'drinks' => []
]
], [
[
'type' => Cassandra\Type\Base::UDT,
'definition' => [
'id' => Cassandra\Type\Base::INT,
'name' => Cassandra\Type\Base::VARCHAR,
'active' => Cassandra\Type\Base::BOOLEAN,
'friends' => [
'type' => Cassandra\Type\Base::COLLECTION_LIST,
'value' => Cassandra\Type\Base::VARCHAR
],
'drinks' => [
'type' => Cassandra\Type\Base::COLLECTION_LIST,
'value' => [
'type' => Cassandra\Type\Base::UDT,
'typeMap' => [
'qty' => Cassandra\Type\Base::INT,
'brand' => Cassandra\Type\Base::VARCHAR
]
]
]
]
]
]);
I've tried using the above example with several variations to accommodate the CollectionMap but nothing is working. My last attempt was this
new Cassandra\Type\CollectionSet($udt_array, [[
'type'=>Cassandra\Type\Base::UDT,
'definition' => [
'map_name' => [
'type' => Cassandra\Type\Base::COLLECTION_MAP,
'value' => [
Cassandra\Type\Base::INT,
Cassandra\Type\Base::BOOLEAN
]
]
]
]])
which gives the error Caught exception: Since v0.7, collection types should have \"definition\" directive. I've also tried using 'definition' instead of 'value'. I'm running out of ideas, any help would be greatly appreciated.
Use "definition" instead of "value". I tried this before but apparently I was doing something else wrong because this worked.
new Cassandra\Type\CollectionSet($udt_array, [[
'type'=>Cassandra\Type\Base::UDT,
'definition' => [
'map_name' => [
'type' => Cassandra\Type\Base::COLLECTION_MAP,
'definition' => [
Cassandra\Type\Base::INT,
Cassandra\Type\Base::BOOLEAN
]
]
]
]])

Categories