2

I use BootstrapValidator as my validation component. I need to give options which are gathered from PHP into its constructor like this:

PHP Code to generate fields:

$fields = "";
foreach ($form["questions"] as $key => $value) {
    if (!empty($value["validators"])) {
        $fields .= "\"" . $key . "\":" . json_encode($value["validators"]) . ",";
    }
}
$new_fields = "{" . chop($fields, ",") . "}";

Javascript part is:

$('#my_form').bootstrapValidator({
    message: 'This value is not valid',
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: <?php echo $new_fields; ?>
});

It prints something like that:

     $('#fc_register_form').bootstrapValidator({
            message: 'This value is not valid',
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                "field_1": {"notEmpty": {"message": "The ****** cannot be empty"}},
                "field_1_confirm": {
                    "notEmpty": {"message": "The ******* must be same with first input for ******."},
                    "identical": {
                        "field": "field_1",
                        "message": "The given value is not same with first input for ****"}
                },
                "field_2": {"notEmpty": {"message": "The ******* empty"}},
                "field_3": {"notEmpty": {"message": "The ******* empty"}},
                "field_4": {"notEmpty": {"message": "The ****** is required and cannot be left empty"}}
            }
        });

So, may double quotes cause it not to work? Or what is wrong with that? It should not be that much though, i guess...

Sonny
  • 8,204
  • 7
  • 63
  • 134
fth
  • 2,478
  • 2
  • 30
  • 44
  • 1
    What is `$fields`? Is that the result of a `json_encode()`? P.S. Your example is missing a `"` in `"message": "...` – gen_Eric Nov 03 '14 at 21:48
  • 1
    Either `$fields` is missing it, or your copy/paste is missing the closing `"` for `message` under the `field_1_confirm` option. – Kyle Nov 03 '14 at 21:48
  • Guys, I've added PHP part and fix missing ". Thanks for your warning. – fth Nov 03 '14 at 21:51
  • 1
    @FatihKaratana: *Do **not*** build JSON yourself! Use PHP's `json_encode()` function. What you do is you create an array formatted how you want it, then you call `json_encode()` on that. You only ever want to call `json_encode()` *once* on the final result. – gen_Eric Nov 03 '14 at 21:53
  • 1
    Replace your loop with: `$new_fields = json_encode($form["questions"]);` – Sonny Nov 03 '14 at 21:55
  • @RocketHazmat `$value["validators"]` is an array, to how i can concat this array into the string? `implode` or `join` does not work because i have something like this:`"field_name" => array("validators" => array("notEmpty" => array("message" => 'The field is required')))` – fth Nov 03 '14 at 21:59
  • I believe it is not about PHP, i write fields values as a `JSON` object, and it did not work again. – fth Nov 03 '14 at 22:24

2 Answers2

1

I just realized that my comment wasn't quite complete.

PHP Code

$fields = array();
foreach ($form["questions"] as $key => $value) {
    if (!empty($value["validators"])) {
        $fields[$key] = $value["validators"];
    }
}

JavaScript Code

$('#my_form').bootstrapValidator({
    message: 'This value is not valid',
    feedbackIcons: {
        valid: 'glyphicon glyphicon-ok',
        invalid: 'glyphicon glyphicon-remove',
        validating: 'glyphicon glyphicon-refresh'
    },
    fields: <?php echo json_encode($fields); ?>
});
Sonny
  • 8,204
  • 7
  • 63
  • 134
  • thanks for your answer, but you made the logic in a better way. Fields still have the same stuff which i showed in my question and validation does not still work. – fth Nov 03 '14 at 22:04
  • I've corrected the issue you had with JSON encoding. I'm not clear on what the problem is beyond that. – Sonny Nov 03 '14 at 22:06
  • I would venture to guess that the data in PHP has an issue. Are you getting any JavaScript errors in your browser console? – Sonny Nov 03 '14 at 22:21
  • The output as a JSON in my dirty code anyway. You made it better. And nope. There is no error in the inspector, just validator does not work. – fth Nov 03 '14 at 22:23
  • Try a `print_r()` or `var_dump()` on your `$fields` array, and make sure the values and structures are correct. I'm not very familiar with BootstrapValidator, but a lot of jQuery functions and plugins will fail silently. – Sonny Nov 04 '14 at 15:16
1

Yeap, i figured it out what i was doing wrong.

It is not about how i encoded my array $fields, it is about missing a child validatorswhich is required by bootstrapValidator.

I just changed this:

........ $fields[$key] = $value["validators"]; ........

into this:

........ $fields[$key]["validators"] = $value["validators"]; ........

And it converts my entire object to what bootstrapValidator required. Also thanks for owner of bootstrapValidator to make me aware of that I missed the validatorskey.

Here is the complete library link: https://github.com/nghuuphuoc/bootstrapvalidator

Hope you guys find it useful.

fth
  • 2,478
  • 2
  • 30
  • 44