Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InArray Validator meta data not complete #37

Open
michalbundyra opened this issue Jan 10, 2020 · 0 comments
Open

InArray Validator meta data not complete #37

michalbundyra opened this issue Jan 10, 2020 · 0 comments
Labels
Enhancement New feature or request

Comments

@michalbundyra
Copy link
Member

The metadata for the InArray validator is not complete. It is missing the haystack key. However, this might have been intentional as there is no way to set an array to the haystack via the admin. But then you could argue why even have this validator in Apigility admin at all.

I solved this in a project by extending the InArray validator and adding a haystack and haystack_delimiter key to the validator metadata which allows me to pass a comma separated string to the setHaystack() method, exploding it to an array based on the haystack_delimiter key. Not sure if this is the best way to do it, but it is working for my needs.

namespace Validator;

use Zend\Validator\InArray as ZendInArray;

/**
 * Class InArray
 * @package Validator
 */
class InArray extends ZendInArray {

    /**
     * The delimiter to be used to explode the string to an array
     * @var string
     */
    protected $haystackDelimiter = ',';

    /**
     * @return string
     */
    public function getHaystackDelimiter()
    {
        return $this->haystackDelimiter;
    }

    /**
     * @param string $haystackDelimiter
     * @return InArray
     */
    public function setHaystackDelimiter($haystackDelimiter)
    {
        $this->haystackDelimiter = $haystackDelimiter;
        return $this;
    }

    /**
     * @param array|string $haystack
     * @return ZendInArray
     */
    public function setHaystack($haystack)
    {
        if( is_string($haystack) ) {
            if( ! strstr($haystack, $this->getHaystackDelimiter()) ) {
                $haystack = array($haystack);
            } else {
                $haystack = explode($this->getHaystackDelimiter(), $haystack);
            }
        }

        return parent::setHaystack($haystack);
    }
} 

And the config of:

return array(
    /**
     * Validators
     */
    'validators'=>array(
        'invokables'=>array(
            'Validator\InArray'            =>'Validator\InArray',
        ),
    ),

    /**
     * Validator MetaData
     */
    'validator_metadata' => array(
        'Validator\InArray'=> array(
            'strict' => 'bool',
            'recursive' => 'bool',
            'haystack'=>'string',
            'haystack_delimiter'=>'string',
        ),
    ),
);

Originally posted by @spectravp at zfcampus/zf-apigility-admin#242

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant