yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

Validation - Compare - custom id

Open sreenadh opened this issue 8 years ago • 6 comments

Attribute 'total_amount' has a custom id 'total_amount'. Client-side validation is not taking this id.

Validation rule

['received_amount', 'compare', 'compareAttribute' => 'total_amount', 'operator' => '<=', 'type' => 'number'],

What steps will reproduce the problem?

$form->field($job_card, "total_amount")->textInput(['class'=>'form-control', 'id' => 'total_amount', 'readOnly'=>'readOnly']) ?>

What is the expected result?

client side JS.

{
    "id": "jobcard-received_amount",
    "name": "received_amount",
    "container": ".field-received_amount",
    "input": "#received_amount",
    "enableAjaxValidation": true,
    "validate": function(attribute, value, messages, deferred, $form) {
        yii.validation.compare(value, messages, {
            "operator": "<=",
            "type": "string",
            "compareAttribute": "**total_amount**",
            "skipOnEmpty": 1,
            "message": "Received Amount must be less than or equal to \"Total Amount\"."
        });
        yii.validation.required(value, messages, {
            "message": "Received Amount cannot be blank."
        });
        yii.validation.number(value, messages, {
            "pattern": /^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/,
            "message": "Received Amount must be a number.",
            "skipOnEmpty": 1
        });
    }
}

What do you get instead?

{
    "id": "jobcard-received_amount",
    "name": "received_amount",
    "container": ".field-received_amount",
    "input": "#received_amount",
    "enableAjaxValidation": true,
    "validate": function(attribute, value, messages, deferred, $form) {
        yii.validation.compare(value, messages, {
            "operator": "<=",
            "type": "string",
            "compareAttribute": "**jobcard-total_amount**",
            "skipOnEmpty": 1,
            "message": "Received Amount must be less than or equal to \"Total Amount\"."
        });
        yii.validation.required(value, messages, {
            "message": "Received Amount cannot be blank."
        });
        yii.validation.number(value, messages, {
            "pattern": /^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/,
            "message": "Received Amount must be a number.",
            "skipOnEmpty": 1
        });
    }
}

Additional info

Q A
Yii version 2.0.12
PHP version 7.0.23
Operating system Linux d6 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:08:14 UTC 2014 i686 i686 i686 GNU/Linux

sreenadh avatar Nov 29 '17 07:11 sreenadh

Thank you for your issue.

Unfortunately code that is not formatted as code blocks is extremely hard to read. Please check GitHub formatting guide and wrap code accordingly.

Thanks!

This is an automated comment, triggered by adding the label missing formatting.

yii-bot avatar Nov 29 '17 08:11 yii-bot

Reference to #7627 Yii 2.0.17 - the same problem

Gruven avatar May 14 '19 18:05 Gruven

We are facing the same issue with Yii 2.0.20. Can anyone provide an update on this?

omkrole avatar Jul 25 '19 07:07 omkrole

This is still happenin in 2.0.38. Has there been a fix?

developedsoftware avatar Oct 12 '20 15:10 developedsoftware

No. The issue is open.

samdark avatar Oct 12 '20 15:10 samdark

Currently, this does not disturb the validation process at all (unless there happens to be another html element with same id) right? since the js handles it via attribute-name: https://github.com/yiisoft/yii2/blob/4ed14bb738a7dde5f80a3cf2cb9987a892c9f7bc/framework/assets/yii.validation.js#L281-L284

0xffcourse avatar Feb 20 '21 06:02 0xffcourse