Giter Club home page Giter Club logo

Comments (8)

weierophinney avatar weierophinney commented on August 16, 2024 1

@autowp Deprecated, but not yet removed, and we do not have a replacement lined up. We'll be keeping them for the foreseeable future, and will have alternatives in place and documented when we're ready to remove them for a future v3 revision (if we end up doing one at all).

from zend-inputfilter.

weierophinney avatar weierophinney commented on August 16, 2024

Why are you passing an array of data to setData()? That should be $filter->setData(false), shouldn't it?

from zend-inputfilter.

froschdesign avatar froschdesign commented on August 16, 2024

@weierophinney
This code example is correct, but the unit test in #139 is wrong.

@autowp
No need for the manager. Much simpler: πŸ˜‰

$inputFilter = new Zend\InputFilter\InputFilter();
$inputFilter->add(
    [
        'name'     => 'foo',
        'required' => false,
    ]
);
$inputFilter->setData(
    [
        'foo' => false,
    ]
);
var_dump($inputFilter->isValid());
var_dump($inputFilter->getMessages());

Output:

boolean false

array (size=1)
  'foo' => 
    array (size=1)
      'isEmpty' => string 'Value is required and can't be empty' (length=36)

from zend-inputfilter.

weierophinney avatar weierophinney commented on August 16, 2024

I figured out the issue with the test. ArrayInputTest extends from InputTest, which results in an exception being raised if the value is not within an array. The appropriate solution is to override that test case within ArrayInputTest in order to pass an array with the false value.

What's interesting is that if I merge both #138 and #157, and adapt the tests as described above (simple false value in the InputTest, overriding the test within ArrayInputTest to pass an array), the test from #157 passes, but the ArrayInputTest variant fails with the same message.

This is due to the fact that the fix for #138 does not also patch ArrayInput line 92, which does the equivalent $empty definition. However, if I make that change, I now get 158 errors, all against ArrayInputTest.

From what I can see, this is due to the various allow_empty and continue_if_empty flag defaults in that input, and how they interact to determine if a false value is valid.

I'll see if I can find a solution, but it's looking pretty complex, which leads me back to my initial inclination that we may not want to support this.

from zend-inputfilter.

froschdesign avatar froschdesign commented on August 16, 2024

This can be used:

$inputFilter = new Zend\InputFilter\InputFilter();
$inputFilter->add(
    [
        'name'              => 'foo',
        'required'          => false,
        'allow_empty'       => true, // important!
        'continue_if_empty' => true, // important!
    ]
);
$inputFilter->setData(
    [
        'foo' => false,
    ]
);
var_dump($inputFilter->isValid()); // true
var_dump($inputFilter->getMessages()); // empty

If an empty string is not allowed, but the value false (like above):

$inputFilter->get('foo')->getValidatorChain()->prependValidator(
    new Zend\Validator\NotEmpty(
        [
            'type' => [
                Zend\Validator\NotEmpty::STRING,
                Zend\Validator\NotEmpty::SPACE,
                Zend\Validator\NotEmpty::NULL,
                Zend\Validator\NotEmpty::EMPTY_ARRAY,
            ],
        ]
    )
);
$inputFilter->setData(
    [
        'foo' => '',
    ]
);
var_dump($inputFilter->isValid()); // false
var_dump($inputFilter->getMessages()); // 'isEmpty' => string 'Value is required and can't be empty'

from zend-inputfilter.

weierophinney avatar weierophinney commented on August 16, 2024

@autowp See the explanation from @froschdesign above; this is already possible, but requires a little configuration on your part: the input must allow_empty to allow a false value to validate.

from zend-inputfilter.

froschdesign avatar froschdesign commented on August 16, 2024

@weierophinney
…and continue_if_empty!

from zend-inputfilter.

autowp avatar autowp commented on August 16, 2024

But both allow_empty and continue_if_empty deprecated since 2.4.8

from zend-inputfilter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.