Generate Error on field in BE
How do I simply generate an error on a field in the Backend?
I have a field schoolnzqaid, and a routine in a onsubmit_callback to check whether this number exists in a database.
If it doesn't I simply want to generate an error message "NZQAID does not exist" within an else{} block.
I generate a log entry like this
Code:
$this->log('School Member ID "'.$dc->id.'" added as New Member in Backend, but nzqaid "'.$dc->activeRecord->schoolnzqaid.'" is not in SchoolSearch database, so Login prevented.', 'MemberAlterSchool resetAfterMemberAdd', TL_ERROR);
but I can't figure the syntax for the field error.
Re: Generate Error on field in BE
You'd better user the hook validateFormField.
To add the error message, use $objWidget->addError("the error message") in the hook function.
Re: Generate Error on field in BE
Thanks acenes, but is there no other way???
Do I necessarily have to use a hook?
Already this routine is in an onsubmit_callback added into tl_member, and only specific to a certain number range and a specific member group.
I was hoping for a simple statement similar to $this->log(), that simply prints the error.
Re: Generate Error on field in BE
$this->log(....);
$this->redirect('typolight/main.php?act=error');
Re: Generate Error on field in BE
Re: Generate Error on field in BE
Sorry, by mistake I edited the previous post instead of reply
Code:
$this->log(....);
$this->redirect('typolight/main.php?act=error');
Re: Generate Error on field in BE
Hmm, interesting to know that, but not what I'm after unfortunately.
Really I do need to print an error message ("not in database") beside a specific field (tl_member.schoolnzqaid).
My routine allows the New Member to be created by an Admin in the BE, but prevents homeDirectory creation and Login permission, and resets the schoolnzqaid to its default 0 if the inputted field value is not present in another table.
I know the hook is designed to output the error to the field, but I'm fishing for any alternative ways.
If there isn't i'll investigate the hook you've suggested.
Cheers :)
Re: Generate Error on field in BE
I'm trying to do this with a custom rgxp instead, and I have a question about "instanceof".
How do I say "if the call is from the Backend Members Module" (tl_member)???
Code:
$objWidget instanceof FrontendUser
doesn't work
Re: Generate Error on field in BE
Hi, ramjet
Have you figured out how to write error message for the field, in same way as validation do this?
Is "custom regex" is the only one way do do this?
I've tried to add some check inside save_callback, but looks like this is wrong place.
I also tried to do this with a hooks - but nothing happened to me.
P.s. maybe it is too late to write something into this topic .... last message was 2 years ago
Re: Generate Error on field in BE
From my FormAuto module, I test the field to see if anything is wrong, before I allow it to modify the database. This is how you do it inside the field's save_callback, you simply use the throw new Exception() syntax.
Code:
public function renameColumn($varValue, DataContainer $dc)
...
if (in_array($varValue, $this->systemColumns))
{
throw new Exception(sprintf($GLOBALS['TL_LANG']['ERR']['systemColumn'], $varValue));
}
...
return $varValue;
}
Re: Generate Error on field in BE
Hi Thyon,
Thank you for your post
if I do throw new Exception - it will return me a php error. In my case it looks like blank page without any styles.
What I'm trying to do, is to show nice error for the field, similar to validation error.
i think I manage to do something with the addCustomRegexp hook, I need to make some tests to understand if it cover all my needs.
I can't believe that this is only one way to do field value check ...
Re: Generate Error on field in BE
Your under the impression that a HOOK and a callback is the same thing. Acenes was not correct, in that the “validateFormField” is only used in the FE Form Generator, so that cannot be use in the BE.
The only way is to throw Exception in the save_callback, this is exactly how leo does this in the DC_Table.php driver just above where the save_callback is initiated. He tests for DCA parameters, e.g. if the item is “unique” and then he does a throw Exception(). Maybe you are not using the throw Exeptions with valid parameters. What you can do is to do a print_r($varValue); die(); at that point just to test if it even reaches your validation code.
However, in other hooks, it depends. The addCustomRegexp HOOK is completely different as that is called inside the widget, so you simply call the widget->addError(). Why did you not look at the sample from leo’s website, so you can copy that?
Code:
// config.php
$GLOBALS['TL_HOOKS']['addCustomRegexp'][] = array('MyClass', 'myAddCustomRegexp');
// MyClass.php
public function myAddCustomRegexp($strRegexp, $varValue, Widget $objWidget)
{
if ($strRegexp == 'postal')
{
if (!preg_match('/^0-9{4,6}$/', $varValue))
{
$objWidget->addError('Field ' . $objWidget->label . ' should be a postal code.');
}
return true;
}
return false;
}