Voyager: Validation problem

Created on 10 Nov 2017  路  13Comments  路  Source: the-control-group/voyager

Laravel Version: 5.5.20
Voyager Version: 1.0.7
PHP Version: 7.1.10
Database Driver & Version: MySQL 5.7.20

Description:

Validation not working when trying to add a new record of a type which have some validation rules.

Steps To Reproduce:

  1. Add validation rule to any BREAD like this:
{
    "validation": {
        "rule": "required",
        "messages": {
            "required": "This :attribute field is a must."
        }
    }
}
  1. trying to add a record from this type.
  2. No thing happen if the field is empty so no error message and can't add the record.
  3. Checking JS console give the error:
    Cannot read property 'top' of undefined at Array. (app.js:24) ...., actually this line is related to displaying errors as I think and trying to fix the error manually I've changed the code:
$.each(e.errors,function(t,n){0===Object.keys(e.errors).indexOf(t)&&$("html, body").animate({scrollTop:$("[name='"+t+"']").parent().offset().top-$("nav.navbar").height()+"px"},"fast"),$("[name='"+t+"']").parent().addClass("has-error"),$("[name='"+t+"']").parent().append("<span class='help-block' style='color:#f96868'>"+n+"</span>")})},error:function(){$(n).unbind("submit").submit()}})})})},,,function(e,t,n){var i,r;"function"==typeof Symbol&&Symbol.iterator;

to this:

$.each(e.errors,function(t,n){0===Object.keys(e.errors).indexOf(t)&&$("html, body").animate({scrollTop:$("[data-name='"+t+"']").parent().offset().top-$("nav.navbar").height()+"px"},"fast"),$("[data-name='"+t+"']").parent().addClass("has-error"),$("[data-name='"+t+"']").parent().append("<span class='help-block' style='color:#f96868'>"+n+"</span>")})},error:function(){$(n).unbind("submit").submit()}})})})},,,function(e,t,n){var i,r;"function"==typeof Symbol&&Symbol.iterator;

and the problem solved partially but I get another problem that the error message shown and still shown despite of the field is not empty.

Most helpful comment

you can use this as work around

public function validateBread($request,$data) {

        $rules = [];
        $messages = [];

        foreach ($data as $row) {
            $options = json_decode($row->details);
            if (isset($options->validation)) {
                if (isset($options->validation->rule)) {
                    if (!is_array($options->validation->rule)) {
                        $rules[$row->field] = explode('|', $options->validation->rule);
                    } else {
                        $rules[$row->field] = $options->validation->rule;
                    }
                }

                if (isset($options->validation->messages)) {
                    foreach ($options->validation->messages as $key => $msg) {
                        $messages[$row->field.'.'.$key] = $msg;
                    }
                }
            }
        }

        return Validator::make($request, $rules, $messages);

    }

paste it to your controller to override the base Controller.

All 13 comments

I have the same problem

I have the same problem extented bread

Same problem I encountered here. Any solution?

Same problem after migrate the database with self generated tables or columns, So i think the problem is that voyager didn't accept migration from outside Voyager UI.

Related to #2236

Same issue. Please mention a workaround until we get fix, if anyone has.

@dattz check out #2236. I mention a dirty workaround. Basically you just have to lowercase display names (Assuming they are identical) like name/Name order/Order.

@Victor-emil Oh! Thanks! Great temporary fix. 馃憤

Go toline number 129 of vendor/tcg/voyager/src/Http/Controllers/Controller.php and replace display_name to field as below :

if (!is_array($options->validation->rule)) {
                        // $rules[$row->display_name] = explode('|', $options->validation->rule);
                        $rules[$row->field] = explode('|', $options->validation->rule);
                    } else {
                        // $rules[$row->display_name] = $options->validation->rule;
                        $rules[$row->field] = $options->validation->rule;
                    }

I hope this will help you

@krishna-bhatta, please don't suggest overwriting code under vendor/. At least not without providing a VERY good reason. I also recommend providing the warning that the change will be overwritten with any updates.

you can use this as work around

public function validateBread($request,$data) {

        $rules = [];
        $messages = [];

        foreach ($data as $row) {
            $options = json_decode($row->details);
            if (isset($options->validation)) {
                if (isset($options->validation->rule)) {
                    if (!is_array($options->validation->rule)) {
                        $rules[$row->field] = explode('|', $options->validation->rule);
                    } else {
                        $rules[$row->field] = $options->validation->rule;
                    }
                }

                if (isset($options->validation->messages)) {
                    foreach ($options->validation->messages as $key => $msg) {
                        $messages[$row->field.'.'.$key] = $msg;
                    }
                }
            }
        }

        return Validator::make($request, $rules, $messages);

    }

paste it to your controller to override the base Controller.

This was also fixed in #2660

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

winex01 picture winex01  路  3Comments

IvanBohonosiuk picture IvanBohonosiuk  路  4Comments

duongsieu picture duongsieu  路  3Comments

wp-src picture wp-src  路  3Comments

popica80 picture popica80  路  3Comments