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

allowEmpty can be populated with the list of what we consider 'empty'. The validation only will stop if the empty validation returns true. #12519

Merged
merged 1 commit into from Jan 10, 2017

Conversation

Projects
None yet
3 participants
@gguridi
Contributor

gguridi commented Dec 30, 2016

Hello!

  • Type: bug fix | new feature
  • Link to issue: #12106

In raising this pull request, I confirm the following (please check boxes):

  • I have read and understood the Contributing Guidelines?
  • I have checked that another pull request for this purpose does not exist.
  • I wrote some tests for this PR.

Small description of change:

Added the possibility to specify in allowEmpty option what we consider 'empty'. Fixed the behaviour so only in case it returns true we move to the next validation. Added recursivity in the preChecking method so we don't duplicate code. Updated changelog.

  • allowEmtpy = true (this means that everything that evaluates to empty is ok)
  • allowEmpty = ['', null] (this means that empty only means empty string and null, false and 0 won't evaluate as empty).

I've added a pull request with this change. The benefits are:

  • Backwards compatible.
  • Flexible, we can specify 'unknown', 'empty' or -1 as empty values in case we are using a shitty api.

An improvement also added in the pull request is that only if the allowEmpty validation is true the validation will stop there, in case of false it will continue with the next validators.

Thanks


This change is Reviewable

Gorka Guridi
Added the possibility to specify in allowEmpty option what we conside…
…r 'empty'. Fixed the behaviour so only in case it returns true we move to the next validation. Added recursivity in the preChecking method so we don't duplicate code. Updated changelog.

@sergeyklay sergeyklay added this to the 3.1.x milestone Jan 3, 2017

@sergeyklay sergeyklay merged commit 27eb58f into phalcon:3.1.x Jan 10, 2017

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@sergeyklay

This comment has been minimized.

Member

sergeyklay commented Jan 10, 2017

Thanks

@sergeyklay sergeyklay modified the milestones: 3.2.x, 3.2.0 Apr 8, 2017

@someson

This comment has been minimized.

Contributor

someson commented Oct 22, 2018

what if

    // ...
    allowEmpty => ['', null, new RawValue('NULL')],

?

new RawValue('NULL') === new RawValue('NULL') → false

I have to prefill the entity already on form-level (save/update), not model-level.
Extended form handler:

    private function _nullableValue(Model $entry, string $fieldName, $value)
    {
        $metaData = $entry->getModelsMetaData();
        $attributes = $metaData->getNotNullAttributes($entry);
        return \in_array($fieldName, $attributes, true) ? $value : new RawValue('NULL');
    }

Model:

    public function validation(): bool
    {
        // ...
        $validation->add('vat', new Numericality(['allowEmpty' => [new RawValue('NULL')]]));
@sergeyklay

This comment has been minimized.

Member

sergeyklay commented Oct 22, 2018

@someson could you please create a PR with test which fails?

@someson

This comment has been minimized.

Contributor

someson commented Oct 22, 2018

@sergeyklay, yeah, I'll try :)

someson added a commit to someson/cphalcon that referenced this pull request Oct 22, 2018

allowEmpty list cannot handle Objects
tests updated for phalcon#12519
see phalcon#12519#issuecomment-431848960

@someson someson referenced this pull request Oct 22, 2018

Merged

Validator's "allowEmpty" list cannot handle Objects #13549

3 of 3 tasks complete

@sergeyklay sergeyklay referenced this pull request Nov 3, 2018

Merged

Fixed Phalcon\Validation::preChecking #13575

3 of 3 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment