Quiet Molly
Quiet Molly

Reputation: 102

How to make function get particular error in JS

In form, there is <input type="hidden" name="forcesave" value="0" id="forces"> which lets me avoid validator:

        if (\Frame\Validator::cidr($data['ip']))
        {
            $ip_collision = $this->db->getField("SELECT GROUP_CONCAT(ip) FROM ".$this->_table." WHERE deleted=0 AND id!=".(int)$data['id'].$this->handle_ip_search($data['ip']));
            if ($ip_collision and !$data['forcesave'])
            {
                \Frame\Msg::err('Entered IP is in collision with: '.' '.$ip_collision.'. Press button Save Changes to proceed anyway');
            }
            $range = Tools::cidrToRange($data['ip']);
            $data['start_address'] = $range[0];
            $data['end_address'] = $range[1];
        }

In the event of name="forcesave" value="1" record is supposted to be added to database even when there is $ip_colission

What I would like to do is:

After first submit of my post form, in case there would occur this particular error: Entered IP is in collision with: '.' '.$ip_collision.'. Press button Save Changes to proceed anyway value of forcesave would be set to 1.

To achieve that I created function:

$("#subm").click(function() {
    console.error();
    if ($("#ipcidr").val()) 
  { 
    $("#forces").val(1);
    // alert('forces 1');
  }
  else 
  {
    $("#forces").val(0);
    // alert('forces 0');
  }
});

which basically checks if ip input isnt empty and then sets value of forcesave to 1. Additionaly, in case that when there is IP in input, immediately forcesave is set to 1 and whole form is submitted(It does all the action after first submit button push)

How can I put this particular error inside of IF and how can I make this function work only after second button push?

Edit:

I managed to get my function get all errors, now I need to get the particular one and then put it inside of if case. How can I get to the exact error from array?

js file if function, getting error in case it appears:

if (typeof res.msg !== "undefined" && Object.keys(res.msg.err).length) {
                        form.removeAttr('data-sent');
                        var cb = 'form_cb_error_' + form.attr('name');
                        console.log(cb);
                        if (typeof window[cb] === "function") window[cb](res, form);
                        submit.html(submitContent).attr('disabled', false);
                        $.each(res.msg.err, function (k, v) {
                            var input = form.find('[name=' + k + ']');
                            if (!input.length) {
                                var val = v.match(/.*?\:\ \"(.*)\"/);
                                if (val) input = form.find('[name="' + k + '[]"][value="' + val[1] + '"]');
                            }
                            labelText = input.parent().parent().find('label').html();
                            if (labelText) {
                                v = v.replace(k, labelText)
                            }
                            input.addClass('error').attr('title', v).focus();
                            if ((!input.length && $.isNumeric(k)) || (input.length && input.is(':visible'))) form.find('ul.errorList').append('<li>' + v + '</li>').fadeIn();
                            submit.html(submitContent).attr('disabled', false);
                        });

form_cb_error form code(getting error)

window.form_cb_error_capForm = function(res, form)
  {
    console.log(res.error);
    console.log(res,form);
  };

Console log: enter image description here

What can I do now to get out EXACT error to the variable(no metter if its key or message i think)

Upvotes: 1

Views: 86

Answers (1)

Quiet Molly
Quiet Molly

Reputation: 102

The answer was much easier than I thought

window.form_cb_error_capForm = function(res, form){
    let errIP = res.msg.err.filter(s => s.includes('Entered'));
    let errName = res.msg.err.filter(s => s.includes('Incorrect value for field "name'));
      if (!errName.length)
      {
        console.log(errIP);
        if (errIP.length)
        {
          $('input[name=force_save').val(1);
        }
      }
  };

Since my error messages were inside of a array, I looked for particular value inside of an error and started doing my forcesave from there

Upvotes: 1

Related Questions