All Low add-ons are now owned by EEHarbor. Read the blog post.

Support archive

Setting validation error on var_save not working

TJ Draper 30 Apr 2016 19:29 problem, active

Hey Low,

I’m having some trouble with a field type I’m developing setting an error message for validation failure on the var_save method. I’m setting:

$this->error_msg = $error;

return false;

But no luck.

I looked through the code a little bit and I can see in mcp.low_variables where the error_msg is set if the return is false, but the object it's trying to get the property off of (Low_variables_type) has error_msg set to null. in the private _ft property I can see my error_msg property on the field type class.

Any ideas?

Replies

  1. Low 2 May 2016 07:33

    I see what you mean. The var is skipped correctly, but the error message is not shown. As a workaround, change the save method in type.low_variables.php to this:

    public function save($var_data) 
    {
    if ($fn = $this->get_bridge_method(__FUNCTION__))
    {
    $var_data = $this->call_ft($fn, $var_data);
    }

    if ($var_data === FALSE && is_object($this->_ft))
    {
    $this->error_msg = $this->_ft->error_msg;
    }

    return $var_data;
    }


    I'll incorporate that fix (or something similar) in the next version. Probably nicer to use a function call at some point...

  2. TJ Draper 2 May 2016 15:06

    Thanks! That works in that the error is shown, but my validation method is returning some HTML which is being converted to entities so that it looks like this on the front end:

  3. Low 2 May 2016 15:26

    That would be correct. Right now, it doesn't accept markup. What does EE do natively in the publish form, here?

    Low 2 May 2016 15:32

    That said, you can overcome it by opening javascript/low_variables.js and look up line 278.

    Then change $target.append($('<em/>').text(error));

    to $target.append($('<em/>').html(error));

    Low 2 May 2016 15:33

    Not sure i'd recommend it tho. I think EE uses an <em> element to display error messages in the native form, and nesting a lot of other stuff in there might break things...

    TJ Draper 2 May 2016 15:50

    EE does render the HTML. I‘ll have to look to see if it’s inside of an element.