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

No error displayed for checkbox #173

Closed
koxu1996 opened this issue Sep 10, 2019 · 15 comments
Closed

No error displayed for checkbox #173

koxu1996 opened this issue Sep 10, 2019 · 15 comments
Labels

Comments

@koxu1996
Copy link

koxu1996 commented Sep 10, 2019

What steps will reproduce the problem?

Create form that require accepting checkbox. Example:

use yii\base\DynamicModel;
use yii\bootstrap4\ActiveForm;
$model = new DynamicModel(['text_field' => '', 'checkbox_field' => '']);
$model->addRule('text_field', 'required', ['requiredValue' => '1'])
        ->addRule('checkbox_field', 'required', ['requiredValue' => '1'])
        ->validate();
print_r($model->getErrors());
$form = ActiveForm::begin();
echo $form->field($model, 'text_field');
echo $form->field($model, 'checkbox_field')->checkBox();
ActiveForm::end();

What's expected?

Error message for required checkbox should be displayed.

What do you get instead?

There is no error for checkbox field:
issue

However client-side validation works, so after filling form you get:
cs-ok
After un-filling form:
cs-error

Additional info

Name Version
yiisoft/yii2 2.0.25
yiisoft/yii2-bootstrap4 2.0.7
PHP 7.3.9
@ruskid
Copy link

ruskid commented Nov 15, 2019

Looks like is-invalid class doesn't apply to the checkbox

ActiveField's checkbox and radio function should look like this:

public function checkbox($options = [], $enclosedByLabel = true)
    {
        if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
            $this->addErrorClassIfNeeded($options);
        }

        $this->addAriaAttributes($options);
        $this->adjustLabelFor($options);
        
        if ($enclosedByLabel) {
            $this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options);
            $this->parts['{label}'] = '';
        } else {
            if (isset($options['label']) && !isset($this->parts['{label}'])) {
                $this->parts['{label}'] = $options['label'];
                if (!empty($options['labelOptions'])) {
                    $this->labelOptions = $options['labelOptions'];
                }
            }
            unset($options['labelOptions']);
            $options['label'] = null;
            $this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options);
        }

        return $this;
    }

Currently addErrorClassIfNeeded is called after printing the checkbox.

@samdark samdark added the type:bug Bug label Nov 18, 2019
@smigles
Copy link

smigles commented Feb 2, 2020

Also no error is for checkboxList with a custom validation rule.

The part of the model:

public function rules()
{
    return [
        ['categories', 'validateCategoryCount'],
    ];
}

public function validateCategoryCount($attribute, $params)
{
    if (!is_array($this->$attribute)) {
        $this->addError($attribute, 'Некорректное значение.');
    } elseif (count($this->$attribute) > 3) {
        $this->addError($attribute, 'Должно быть выбрано не более 3 категорий.');
    }
}

The part of the view:

<?= $form->field($model, 'categories')->checkboxList($categories) ?>

The screenshot from Firefox Page Inspector:

Gray color is for hidden elements.

@alexgivi
Copy link

alexgivi commented Feb 4, 2020

I've fixed this bug such way:

  1. create AcriveField class, inherit from \yii\bootstrap4\ActiveField.
  2. implement checkbox method such way:
    public function checkbox($options = [], $enclosedByLabel = false)
    {
        Html::addCssClass($options, 'custom-control-input');
        if ($this->form->validationStateOn === ActiveForm::VALIDATION_STATE_ON_INPUT) {
            $this->addErrorClassIfNeeded($options);
        }

        return parent::checkbox($options, $enclosedByLabel); // TODO: Change the autogenerated stub
    }
  1. when create ActiveForm instance - pass 'fieldClass' config option as your new ActiveField class name.
$form = ActiveForm::begin([
        'fieldClass' => \app\widgets\ActiveField::class,
        // ...
]);

@samdark
Copy link
Member

samdark commented Feb 13, 2020

@alexgivi would you like to submit a pull request?

@egrekov
Copy link
Contributor

egrekov commented Feb 5, 2021

Yesterday I spent a day to understand why the error message was not shown until I came across this problem. Already it turns out for a year and a half people suffer and make crutches, but the problem has not been solved. Hmm, very sad.

@samdark
Copy link
Member

samdark commented Feb 6, 2021

@egrekov that's OpenSource. If there's a pull request fixing it that has unit tests, we merge it right away after a short code review.

@egrekov
Copy link
Contributor

egrekov commented Feb 8, 2021

@samdark Well, it's fixed :)

@bizley
Copy link
Member

bizley commented Feb 8, 2021

And yet, without unit tests ;) Could you please add some?

@egrekov
Copy link
Contributor

egrekov commented Feb 8, 2021

@bizley, some problem in the assembly of tests

# docker login
Authenticating with existing credentials...
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

# make test
test -d tests/docker || git clone https://github.com/cebe/jenkins-test-docker tests/docker
cd tests/docker && git checkout -- . && git pull
Уже обновлено.
mkdir -p tests/dockerids
cd tests/docker/php && sh build.sh
building base docker image for php.
Sending build context to Docker daemon  6.144kB
Step 1/10 : FROM debian:wheezy
 ---> 10fcec6d95c4
Step 2/10 : RUN   DEBIAN_FRONTEND=noninteractive apt-get update &&   DEBIAN_FRONTEND=noninteractive apt-get upgrade -y &&   DEBIAN_FRONTEND=noninteractive apt-get install -y git curl vim build-essential pkg-config autoconf bison re2c libxml2-dev
 ---> Running in 20c0c5134467
Ign http://security.debian.org wheezy/updates Release.gpg
Ign http://deb.debian.org wheezy Release.gpg
Ign http://security.debian.org wheezy/updates Release
Ign http://deb.debian.org wheezy-updates Release.gpg
Err http://security.debian.org wheezy/updates/main amd64 Packages

Err http://security.debian.org wheezy/updates/main amd64 Packages

Ign http://deb.debian.org wheezy Release
Err http://security.debian.org wheezy/updates/main amd64 Packages

Ign http://deb.debian.org wheezy-updates Release
Err http://security.debian.org wheezy/updates/main amd64 Packages

Err http://security.debian.org wheezy/updates/main amd64 Packages
  404  Not Found [IP: 151.101.2.132 80]
Err http://deb.debian.org wheezy/main amd64 Packages
  404  Not Found
Err http://deb.debian.org wheezy-updates/main amd64 Packages
  404  Not Found
W: Failed to fetch http://security.debian.org/debian-security/dists/wheezy/updates/main/binary-amd64/Packages  404  Not Found [IP: 151.101.2.132 80]

W: Failed to fetch http://deb.debian.org/debian/dists/wheezy/main/binary-amd64/Packages  404  Not Found

W: Failed to fetch http://deb.debian.org/debian/dists/wheezy-updates/main/binary-amd64/Packages  404  Not Found

E: Some index files failed to download. They have been ignored, or old ones used instead.
The command '/bin/sh -c DEBIAN_FRONTEND=noninteractive apt-get update &&   DEBIAN_FRONTEND=noninteractive apt-get upgrade -y &&   DEBIAN_FRONTEND=noninteractive apt-get install -y git curl vim build-essential pkg-config autoconf bison re2c libxml2-dev' returned a non-zero code: 100
building docker for php php-5.6.8.
Sending build context to Docker daemon  12.29kB
Step 1/8 : FROM yiitest/php-base:latest
pull access denied for yiitest/php-base, repository does not exist or may require 'docker login'
Makefile:20: ошибка выполнения рецепта для цели «docker-php»
make: *** [docker-php] Ошибка 1```

@bizley
Copy link
Member

bizley commented Feb 8, 2021

Is this local docker? I'm not sure how to help you here. This part of code should be ok to test with php only, without any external services anyway.

@egrekov
Copy link
Contributor

egrekov commented Feb 8, 2021

@bizley Is this local docker?

Yes local docker, looked 7 debians are used in tests, it is already deprecated. You tried to run tests on your own, it seems to me that they won't start either.

@egrekov
Copy link
Contributor

egrekov commented Feb 16, 2021

@bizley Once again I looked at why the test is not being collected, it turns out that the repository is missing yiitest/php-base

@egrekov
Copy link
Contributor

egrekov commented Feb 16, 2021

Are the tests passed?
image

@bizley
Copy link
Member

bizley commented Feb 16, 2021

Yes, tests are passing which means nothing is broken but is there a test checking what you just added?

@egrekov
Copy link
Contributor

egrekov commented Mar 4, 2021

Yes, tests are passing which means nothing is broken but is there a test checking what you just added?

root@516debc04af8:/opt/app# vendor/phpunit/phpunit/phpunit --filter ActiveFieldTest
PHPUnit 4.8.34 by Sebastian Bergmann and contributors.

Runtime:        PHP 7.3.27
Configuration:  /opt/app/phpunit.xml.dist

.............

Time: 2.62 seconds, Memory: 8.00MB

OK (13 tests, 13 assertions)

egrekov added a commit to egrekov/yii2-bootstrap4 that referenced this issue Mar 4, 2021
@samdark samdark closed this as completed in d1af1e2 Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants