XF 2.2 Required form element error message via phrase

asprin

Active member
So I've an entity set up like this:

PHP:
$structure->columns = [
    'sys_id' => ['type' => self::UINT, 'autoIncrement' => true],
    'title' => ['type' => self::STR, 'maxLength' => 40, 'required' => 'asp_fb_req_title'],
    'catg' => ['type' => self::STR, 'required' => 'asp_fb_req_catg', 'allowedValues' => array_keys($validCategories)],             
];

To test it, I keep both the fields empty and hit the submit button. I get the following output:

1675574995702.png

A couple of things are puzzling me:

(a) The error messages are not listed in the order the fields are shown on the page (In the form, first is the title and then the category). This is not a biggie though.

(b) The title's phrase asp_fb_req_title is being picked up but not the category's phrase asp_fb_req_catg. I've checked and double checked again. Both the phrases are present under the "Phrase" section. So unsure why a generic message is displayed instead of the intended phrase value.
 
Last edited:
Solution
You can just write a custom verifier.

PHP:
/**
 * @param array{allowedValues: list<string>} $columnOptions
 */
protected function verifySomeColumn(
    string $value,
    string $key,
    int $type,
    array $columnOptions
): bool
{
    if (!\in_array($value, $columnOptions['allowedValues'], true))
    {
        $this->error(\XF::phrase('some_custom_phrase'), $key);
        return false;
    }

    return true;
}
Ok, looks like that generic phrase value might be coming from ValueFormatter class because of the presence of allowedValues.

Does this mean we won't be able to use our own phrase in this case? Or at least let user know which field we are referring to the in the error message?
 
Last edited:
Any suggestions for being able to use own phrase for a required field that has allowedValues attribute? Don't like generic "Please enter a valid value" message.
 
You can just write a custom verifier.

PHP:
/**
 * @param array{allowedValues: list<string>} $columnOptions
 */
protected function verifySomeColumn(
    string $value,
    string $key,
    int $type,
    array $columnOptions
): bool
{
    if (!\in_array($value, $columnOptions['allowedValues'], true))
    {
        $this->error(\XF::phrase('some_custom_phrase'), $key);
        return false;
    }

    return true;
}
 
Solution
You can just write a custom verifier.

PHP:
/**
 * @param array{allowedValues: list<string>} $columnOptions
 */
protected function verifySomeColumn(
    string $value,
    string $key,
    int $type,
    array $columnOptions
): bool
{
    if (!\in_array($value, $columnOptions['allowedValues'], true))
    {
        $this->error(\XF::phrase('some_custom_phrase'), $key);
        return false;
    }

    return true;
}
Thanks. Based on an earlier question of mine (where I learned about the verifyXXX() method), I was thinking of resorting to it as the final option. But I'm guessing there's no other way to achieve this so I'll go with this one only.

It's a little interesting that for standard str columns, we have the required=> 'xxxx' way of specifying the custom error message but not for those str columns that have allowedValues() key.
 
Back
Top Bottom