formapro / JsFormValidatorBundle

The Javascript validation for Symfony 2, 3 and 4 forms
MIT License
128 stars 56 forks source link

Constraints 'propertyPath' is not respected #148

Open boubbin opened 6 years ago

boubbin commented 6 years ago

For example this annotation

    public $start_date;
     /**
     * @Assert\GreaterThan(propertyPath = "start_date", message = "End smaller than start")
     */
    public $end_date;

Will be processed with GreaterThan like it should be:

function SymfonyComponentValidatorConstraintsGreaterThan() {
    this.message = '';
    this.value = null;

    this.validate = function (value) {

        var f = FpJsFormValidator;
        if (f.isValueEmty(value) || value > this.value) {
            return [];
        } else {
            return [
                this.message
                    .replace('{{ value }}', FpJsBaseConstraint.formatValue(value))
                    .replace('{{ compared_value }}', FpJsBaseConstraint.formatValue(this.value))
            ];
        }
    }
}

but this.value is always null here and therefor the field is never valid.

I could not find any references in the code to propertyPath but it is parsed correctly to the client side, just never used. Anyway you will never know what is the actual element in this case

TomasLudvik commented 4 years ago

Hey @boubbin, I have just run into same problem - have you ever managed to get it working somehow?

boubbin commented 4 years ago

Hey @boubbin, I have just run into same problem - have you ever managed to get it working somehow?

No. I did not.

We are still using this library extensively and I haven't encountered this problem in a long time. My guess is that we only had one constraint using propertyPath.

I suggest that you look into transferring the constraint into callback constraint (if possible) since they work very nicely with the library. They are a bit more work but once you have some ground work in place they come easy. We have currently multiple dozen different callback constraints implemented client-side.

One option is also to disable the validation for this field in the client-side. You will sooner or later need this kind of mechanism anyway where you can easily just not validate some constraints.