4 users online. Create an account or sign in to join them.Users
Advanced Field Validation
This is an open discussion with 8 replies, filed under General.
Search
Hmm tricky one. It could perhaps be solved by adding another delegate further down the processing chain, but another delegate means more bloat.
You could potentially write a new field which doesn’t render anything, but performs validation. This field would contain your rules, and return an error if they fail, along with other field errors.
Nick, that’s a rather cool idea! I’ll give it a try.
This field would contain your rules, and return an error if they fail, along with other field errors.
The first step is done: I managed to return an error for the “dummy” field itself (and render an error message on the frontend) by using s.th. like this in my field:
public function checkPostFieldData($data, &$message, $entry_id = NULL)
{
$message = NULL;
## get post data
if(isset($_POST['fields'])) $fields = $_POST['fields'];
## sanitize values
foreach ($fields as $item => $value) {
$fields[$item] = General::sanitize($value);
}
if(($fields['lalala'] == 'yes') && ($fields['lululu'] == ''))
{
$message = 'We need lululu because you are lalala.';
return self::__INVALID_FIELDS__;
}
return self::__OK__;
}
But how could I manage to get errors for other fields than the “dummy” field? (I am using the form-controls utility, so it would be really great to have these fields as errors in my XML.)
But how could I manage to get errors for other fields than the “dummy” field?
Do you mean that other field errors are no longer displayed in the Event XML output, or that you want to access these errors within checkPostFieldData?
I’d like to access these errors, i.e. change a field to be “invalid” or “missing”.
Hmm I’m not sure you can since there are no other delegates that fire when errors occur. The only remaining delegates are called if the data passes validation and the entry is saved.
The only way I can think of doing this, and it’s quite horrible, is to subscribe to a delegate that provides you with the page XML once it has been built, which you can perform XPath on, modify, and return before XSLT is applied.
Hmmm, maybe I can add additional logic in my XSL, applying class names for those fields based on the messages I get for the invalid dummy field. Well, the whole logic won’t be straight-forward in the end… A maintenance nightmare.
So just for the records, here is what I did (my “dummy” validation field is called “extended validation”):
<xsl:variable name="class-field-1">
<xsl:choose>
<xsl:when test="//events/myfunnyevent/extended-validation/@message = 'Field 1'">
<xsl:text>missing</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:variable>
...
<xsl:call-template name="form:label">
<xsl:with-param name="for" select="'field-1'"/>
<xsl:with-param name="text" select="'Field 1'"/>
</xsl:call-template>
<xsl:call-template name="form:input">
<xsl:with-param name="handle" select="'field-1'"/>
<xsl:with-param name="maxlength" select="'100'"/>
<xsl:with-param name="class" select="$class-field-1"/>
</xsl:call-template>
Create an account or sign in to comment.
I need to perform some advanced validation which is not possible out-of-the-box, like “if field1 is set to yes, then field2 must not be emtpy”.
I would love to find the results in the save event’s XML. I played around with the
EventPreSaveFilterdelegate, but I only managed to populate the message (error) array with these “advanced validation errors”. In this case the save event will be aborted, so I won’t find any “standard validation errors” at the same time.Is there any chance to perform additional validation and write to the event’s XML result without aborting the standard validation of the save event?