Trouble with validation function in plugins on custom fields

Discussion in 'Customization & add-ons' started by zakta, Jul 5, 2013.

  1. zakta

    zakta New Member

    Joined:
    Jul 1, 2013
    Messages:
    2
    So, I have this plugin I'm working on, right now just adding custom fields to products.

    function init()
    {
    $this->getDi()->productTable->customFields()->add(new Am_CustomFieldText('searches_per_bill_period', 'Searches Each Billing Period', 'maximum number of searches user can do in the Billing Period'));


    }

    That works fine, no problems. Now, according to the API, you can also specify a validation function in the Am_CustomField constructor. Couldn't find any real documentation on it, but dug into the source. All the constructor does is just save the validation argument in validateFunc. Here is where the interesting stuff happens in Am_CustomField

    public function addToQF2(HTML_QuickForm2_Container $container, $params = array())
    {
    $el = $container->addElement($this->qfType, $this->name, $params)
    ->setLabel(!empty($this->description) ? ___($this->title)."\n".___($this->description) : ___($this->title));
    if (!empty($this->size))
    $el->setAttribute('size', $this->size);
    if (!empty($this->default))
    $el->setValue($this->default);
    if(!(defined('AM_ADMIN') && AM_ADMIN)) $this->addValidateFunction($el);
    return $el;
    }

    function addValidateFunction(HTML_QuickForm2_Node $el)
    {
    foreach ((array)$this->validateFunc as $f)
    {
    switch ($f)
    {
    case 'required' :
    $el->addRule('required');
    break;
    case 'integer':
    $el->addRule('regex', ___("Integer value required"), '/^\d+$/');
    break;
    case 'number':
    $el->addRule('regex', ___("Numeric value required"), '/^\d+(|\.\d+)$/');
    break;
    case 'email':
    $el->addRule('callback', ___("Please enter a valid e-mail address"), array('Am_Validate', 'empty_or_email'));
    break;
    default:
    if (is_callable($f)) $el->addRule('callback2', '--error--', $f);
    break;
    };
    }
    }

    So looking at the addValidateFunction, it appears that the validate argument to the Am_CustomField constructor can be an array of validators, which can either be a callback (so custom written in the plugin) or a few premade ones that you can use by just specifying the string ('required', 'integer', etc).

    Great. And the addToQF2 gets called when the field is added to customFields. So the validator doesn't get assigned to the field until it's actually being added to a form. Also fine.

    But, there is this little check
    if(!(defined('AM_ADMIN') && AM_ADMIN))
    which basically means that if the admin (or if you're in the admin section, still not sure on that one) is entering data in the fields, all the validators are ignored (well, really they never get added to the field in the form, but the effect is the same).

    So, if I comment out that part and just have the add validator line

    //if(!(defined('AM_ADMIN') && AM_ADMIN)) $this->addValidateFunction($el);
    $this->addValidateFunction($el);

    Bingo, works fine.

    I guess the assumption is that admin's are too epic to be constrained by field validation?

    Is there some really critical reason to have that check there?

    I hate to modify the source code of amember itself, so should I just try to call addValidateFunction myself from inside the init function in my plugin?

Share This Page