0

I have a hook handler for safecracker_submit_entry_start that gets some value from a form field and processes it.

I want to validate the output of that process.

  • If the output is good, then continue form submission
  • If the output is not valid or bad, I want to cancel form submission and if possible, return some sort of error code or message

Is it possible for the hook handler of safecracker_submit_entry_start to cancel form submission?

1

As you are checking the Safecracker form submitted data and would like to show error and cancel submission on incorrect data.

Here is the code example which you have to write same like into the hooked function:

function function_name($obj)
{
$custom_error = 'XYZ'; // need to check the data if that error exist 
if($custom_error)
{
array_push($obj->errors, $custom_error);
}
}

Now that error message will be shown also. If you are using error_handling="inline" parameter then this error message will be shown within:

{global_errors}{error}{/global_errors}

In the same way, you can output the custom error message for any field with following example code:

function function_name($obj)
{
$custom_error = 'XYZ'; // need to check the data if that error exist 
if($custom_error)
{
$obj->field_errors['field_name'] = $custom_error;
}
}

This error message can be outputted within safecracker for with the tag:

{error:my_field_name}

I hope, it would help you.

6
  • This only works with global errors. Can you specifically set something to inline errors? I can't seem to find anything for that in the documentation. – developarvin Jul 8 '13 at 8:23
  • I think, this is already inline error message if you are passing parameter error_handling="inline" with {exp:safecracker} tag and putting {global_errors}{error}{/global_errors} into safecracker tag. May be I 'm not getting what exactly do you want. Could you share more info. – Bhashkar Yadav Jul 8 '13 at 8:35
  • I am trying to understand how Expression Engine knows that X error is associated with X field. In your example, we are passing only strings. – developarvin Jul 8 '13 at 8:49
  • Well, my example throwing the global error i.e its not specific to any field of safecracker. There are some global errors like "Numbers cannot be used as URL Titles" which can be shown while submitting an entry. For the field error, you need to have $obj->field_errors['field_name'] = "Error Message"; and this field error can be outputted with {error:my_field_name} – Bhashkar Yadav Jul 8 '13 at 9:03
  • Yup, it worked. It is exactly what I was looking for. – developarvin Jul 8 '13 at 9:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.