XF 2.2 Entity field "required"=true does not work when type if UINT

asprin

Active member
In my custom entity, I have a field such as:

PHP:
'foo' => ['type' => self::UINT, 'required' => true]

The form is something like this:
HTML:
<xf:selectrow label="Foo" value="{$entity.foo}" name="foo">
   <xf:option value="">Select One</xf:option>
   <option value="1">Abc</option>
    <option value="2">Ghi</option>
    <option value="3">Def</option>
</xf:selectrow>

Of course, there are other fields as well, but if nothing in selected for "Foo" and submit button is pressed, the form submits successfully and in the database, the value gets set as "0".

Controller action is like this:
PHP:
protected function formSaveProcess(\Asprin\FB\Entity\Bar $bar)
{
    $data = array();

    $data['first'] = 'str';
    $data['last'] = 'str';
    $data['foo'] = 'int';

    $input = $this->filter($data);

    $form = $this->formAction();
    $form->basicEntitySave($bar, $input);

    return $form;
}

Any way to validate empty UINT values without having to write code in the controller?
 
Solution
The value is still required, but 0 is a valid value for an unsigned integer and your controller code will set 0 when filtering.

You can do what's suggested above, or add a verifyFoo method to your entity for more comprehensive validation.
The value is still required, but 0 is a valid value for an unsigned integer and your controller code will set 0 when filtering.

You can do what's suggested above, or add a verifyFoo method to your entity for more comprehensive validation.
 
Solution
verifyFoo
Is the "verify" part of the function name a predefined one? What I mean by that is whether XF, as a system, will look for any functions in the entity class file that starts with "verify" and run it during save/update (just like how _preSave() and _postSave() work)

Or is it a custom function (that I can name it it any way) and call it manually in the public controller to perform the validation?
 
It's a convention. If it exists, XF will call it automatically. You can set a custom function name by setting the 'verify' key on the column definition.

It's called when a column is set. It should return a boolean indicating if the value was valid. You can set error messages inside the method as usual using $this->error(...).
 
Back
Top Bottom