• REGISTRATION REQUIREMENTS:

    Your username here MUST MATCH your XenForo username (connected to your XF license).

    Once you have registered here, then you need to start a conversation at xenforo.com w/Bob and provide the following:
    1. Your XenForo License Validation Token
    2. The Domain Name associated with the License
    NOTE: Your account will be validated once ALL requirements are verified/met. Thank you for your patience.

Custom Field Validation

Bob

Developer
Staff member
The only other important thing relating to this which I think is needed is a better data validator which prompts to say which field has been entered incorrectly.

There is, however, it evaluates 1 at a time (in a loop) and when it finds the first one, it throws the error (so there may be more, but it won't know until the item is submitted again.

Selection_473.png

Selection_474.png
 
Nice one, what happens when you enter data in the wrong format? ie. doesn't pass Regex validation?

There is already built in validation. It used what MIKE and KIER use in Core XF.

PHP:
                $valid = $fieldModel->verifyCustomFieldValue($field, $value, $error);
                if (!$valid)
                {
                    $this->error($error, "sc_custom_field_$fieldId");
                    continue;
                }

PHP:
    /**
     * Verifies that the value for the specified field is valid.
     *
     * @param array $field
     * @param mixed $value
     * @param mixed $error Returned error message
     *
     * @return boolean
     */
    public function verifyCustomFieldValue(array $field, &$value, &$error = '')
    {
        $error = false;

        switch ($field['field_type'])
        {
            case 'textbox':
                $value = preg_replace('/\r?\n/', ' ', strval($value));
                // break missing intentionally

            case 'textarea':
            case 'bbcode':               
                $value = trim(strval($value));
               
                if ($field['field_type'] == 'bbcode')
                {
                    $value = XenForo_Helper_String::autoLinkBbCode($value);
                }
               
                if ($field['max_length'] && utf8_strlen($value) > $field['max_length'])
                {
                    $error = new XenForo_Phrase('please_enter_value_using_x_characters_or_fewer', array('count' => $field['max_length']));
                    return false;
                }

                $matched = true;

                if ($value !== '')
                {
                    switch ($field['match_type'])
                    {
                        case 'number':
                            $matched = preg_match('/^[0-9]+(\.[0-9]+)?$/', $value);
                            break;

                        case 'alphanumeric':
                            $matched = preg_match('/^[a-z0-9_]+$/i', $value);
                            break;

                        case 'email':
                            $matched = Zend_Validate::is($value, 'EmailAddress');
                            break;

                        case 'url':
                            if ($value === 'http://')
                            {
                                $value = '';
                                break;
                            }
                            if (substr(strtolower($value), 0, 4) == 'www.')
                            {
                                $value = 'http://' . $value;
                            }
                            $matched = Zend_Uri::check($value);
                            break;

                        case 'regex':
                            $matched = preg_match('#' . str_replace('#', '\#', $field['match_regex']) . '#sU', $value);
                            break;

                        case 'callback':
                            $matched = call_user_func_array(
                                array($field['match_callback_class'], $field['match_callback_method']),
                                array($field, &$value, &$error)
                            );

                        default:
                            // no matching
                    }
                }

                if (!$matched)
                {
                    if (!$error)
                    {
                        $error = new XenForo_Phrase('please_enter_value_that_matches_required_format');
                    }
                    return false;
                }
                break;

            case 'radio':
            case 'select':
                $choices = unserialize($field['field_choices']);
                $value = strval($value);

                if (!isset($choices[$value]))
                {
                    $value = '';
                }
                break;

            case 'checkbox':
            case 'multiselect':
                $choices = unserialize($field['field_choices']);
                if (!is_array($value))
                {
                    $value = array();
                }

                $newValue = array();

                foreach ($value AS $key => $choice)
                {
                    $choice = strval($choice);
                    if (isset($choices[$choice]))
                    {
                        $newValue[$choice] = $choice;
                    }
                }

                $value = $newValue;
                break;
        }

        return true;
    }
 
From my understanding it just outputs the XF phrase "please_enter_value_that_matches_required_format" then right? I'm going to have to update all the fields with examples to show what is acceptable and what isn't, problem is I still have that bug with the field descriptions not updating.
 
I've moved this into its own thread as it has nothing to do with the LAYOUT of the EDIT page.

Yes, that is what the core xenforo method does (which is the standard that I follow).

I can not re produce (on any instance) the Description Bug you are referring to.
 
Back
Top