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

Error in Multiple columns example #17

Closed
mikbox74 opened this issue Jul 24, 2015 · 13 comments
Closed

Error in Multiple columns example #17

mikbox74 opened this issue Jul 24, 2015 · 13 comments

Comments

@mikbox74
Copy link

https://github.com/unclead/yii2-multiple-input/blob/master/docs/multiple_columns.md
This validation example not works

$key = $attribute . '[' . $index . '][label]';
$this->addError($key, $error);

The $error not displays.
I guess it is possible but requires impruvement within the widget code.

@unclead
Copy link
Owner

unclead commented Jul 24, 2015

i'll check the example documentation but here is screen from my test app: http://prntscr.com/7wi131
As you can see validation works fine. Please provide your code, so I checked it

Or you mean that message is not appeared?

As workaround you can use errorSummary

@unclead unclead closed this as completed Jul 24, 2015
@unclead unclead reopened this Jul 24, 2015
@mikbox74
Copy link
Author

Eugene, thank you for answer. Here is my code.
Model:

namespace frontend\widgets\menu;

use Yii;
use yii\validators\RequiredValidator;

class Configuration extends \common\models\WidgetsConfig
{
    public $items;
    //public $title;
    public function rules()
    {
        return [
            ['items', 'validateItems'],
        ];
    }
    public function init()
    {
        parent::init();
    }

    public function attributeLabels()
    {
        return [
            'items' => 'Пункты меню',
        ];
    }

    public function validateItems($attribute)
    {
        $requiredValidator = new RequiredValidator();
        $requiredValidator->message = 'Необходимо заполнить поле.';

        foreach($this->$attribute as $index => $row) {
            $error = null;
            $requiredValidator->validate($row['label'], $error);
            if (!empty($error)) {
                $key = $attribute . '[' . $index . '][label]';
                $this->addError($key, $error);
            }
        }
    }
    public function rolesList()
    {
        return Yii::$app->authManager->getRoles();
    }
}

form:

use unclead\widgets\MultipleInput;
use yii\helpers\ArrayHelper;

$roles = Yii::$app->authManager->getRoles();

echo $form->field($model, 'items')->widget(MultipleInput::className(), [
    'columns' => [
        [
            'name'  => 'label',
            'title' => 'Название',
        ],
        [
            'name'  => 'url',
            'title' => 'Ссылка',
            'defaultValue' => '/',
        ],
        [
            'name'  => 'access',
            'type'  => 'checkboxList',
            'title' => 'Доступ',
            'items' => ArrayHelper::map($roles, 'name', 'description'),
            'options' => [
                'item' => function ($index, $label, $name, $checked, $value) {
                   $render = '<div class="checkbox">
                      <label>
                        <input type="checkbox"'.($checked?' checked':'').' name="'.$name.'" value="'.$value.'">
                         '.$label.'
                      </label>
                    </div>';
                    return $render;
                }
            ],
        ]
    ]
 ])->hint('Если ни одна роль не отмечена, то пункт меню смогут увидеть все пользователи');

May be it not works because the model extends base\Model, not ActiveRecord?

@unclead
Copy link
Owner

unclead commented Jul 24, 2015

it not works because widget uses Html helper for rendering the inputs.
I'll try to use your solution with some modification for fixing this bug

@unclead
Copy link
Owner

unclead commented Jul 24, 2015

@mikbox74 Try to update to latest revision. I adapted your code. Let me know if all is correct

@mikbox74
Copy link
Author

I try it with example above. Div.help-block.help-block-error appears but it is empty and field not highlighted. Obviously you forget to put errors from model into the div and add class has-error to div.form-control.

@unclead
Copy link
Owner

unclead commented Jul 25, 2015

You use form without Ajax validation. Am I right? I forgot about this use case

Отправлено со смартфона Sony Xperia™

Пользователь Michail notifications@github.com писал:

I try it with example above. Div.help-block.help-block-error appears but it is empty and field not highlighted. Obviously you forget to put errors from model into the div and add class has-error to div.form-control.


Reply to this email directly or view it on GitHub:
#17 (comment)

@mikbox74
Copy link
Author

Yes, right.

2015-07-25 16:23 GMT+04:00, Eugene Tupikov notifications@github.com:

You use form without Ajax validation. Am I right? I forgot about this use
case

Отправлено со смартфона Sony Xperia™

Пользователь Michail notifications@github.com писал:

I try it with example above. Div.help-block.help-block-error appears but it
is empty and field not highlighted. Obviously you forget to put errors
from model into the div and add class has-error to div.form-control.


Reply to this email directly or view it on GitHub:
#17 (comment)


Reply to this email directly or view it on GitHub:
#17 (comment)


С уважением, Михаил

@unclead unclead reopened this Jul 26, 2015
@unclead
Copy link
Owner

unclead commented Jul 27, 2015

Fixed via 8ac7f9b
Please check it again and let me know if the issue reproduced
@mikbox74 please check it again. You can get more detail in multiple columns example

@unclead unclead closed this as completed Jul 27, 2015
@mikbox74
Copy link
Author

All works fine. Thank you for this incredible widget!

@daniroyo
Copy link

daniroyo commented Nov 1, 2020

Sorry for reopen issue, close if you think it is better to create new one.

First of all, thanks a lot for such as incredible widget, it fits my need perfectly.

I notice errors works fine but error messages are not show. Is it that for missconfiguration of mine or is it an expected behaviour?

Ressume: Errors are highlighted, but error messages are not shown

@unclead
Copy link
Owner

unclead commented Nov 2, 2020

@daniroyo create a new issue with the code example to reproduce your case

@daniroyo
Copy link

daniroyo commented Nov 2, 2020

Sorry, but I finally found the way to show the error, using attribute "enableError" on Column definition like follows.

$form->field($model, 'users')->widget(MultipleInput::class, [
                // 'max' => 4,
                'addButtonPosition' => [
                    MultipleInput::POS_FOOTER,
                    MultipleInput::POS_HEADER,
                ],
                'columns' => [
                    [
                        'name'  => 'username',
                        'title' => Yii::t('app', 'Username'),
                        **'enableError' => true,**
                        'options' => [
                            'class' => '',
                        ],
                    ],
                ]
            ]);

@unclead
Copy link
Owner

unclead commented Nov 3, 2020

this option is covered in the documentation 😄

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

No branches or pull requests

3 participants