How to require numeric fields using AutoValidator?

Parsnip

Active member
When using the AutoValidator, is there any way to make a numeric field required?

I know I can set a minimum value like:

array('type' => self::TYPE_UINT, 'min' => 1);

Which then will display the message:

"Please enter a number that is at least 1"

However, without the 'min' setting adding 'required' => true on its own does nothing.

I was hoping to be able to set a custom 'requiredError' message for this field.
 
So I figured out I can do this to the above array:

array('type' => self::TYPE_UINT, 'default' => 0, 'max' => 1000 , 'verification' => array('$this', '_verifyMyField'));

then...

PHP:
protected function _verifyMyField(&$myValue)
{
   if (!$myValue)
    {
        $this->error('You must enter a maximum value.');
        return false;
    }

    return true;
}

Which will display my custom required message in the overlay after submission.

popup.webp

However for the life of me I can't figure out how to get my message into the formValidationInlineError label like so:

side.webp
Where could I be going wrong?
 
I'm pretty sure that's a part of the datawriter.

Code:
$datawriter->error('Please enter a number that is at least 1', 'your_field_name');
 
Thank you, the field name what I was missing! :)

Now, if only I can figure out why it doesn't want to work with a select element... does it work with all elements?

Again, I can get the pop-up message happening for that but even the basic validation without my function won't display any inline errors.
 
Can anyone please help? Do the inline validation errors work with select elements, or all elements for that matter?
 
Do the inline validation errors work with select elements
I don't think you can do it with select elements.
You could however add a hidden text input box next to the select drop-down and pass that element's name to the error thus having the message show where you want.
 
Back
Top Bottom