2

I have a front end form based on the entry form example provided by craft documentation which allows logged in users to submit an entry to a specific section. Form validation is not working in that required fields are not being picked up on submission. So if the field is empty it still submits the form.

I have the user group set up so users are not able to use the CP or submit live entries so the entry is sent through as disabled so that the admin users can monitor entries before enabling them.

Am I correct in thinking that because the entry is being saved as disabled that craft does not validate the post data? If this is correct what is the work around as I do not want users to be able to post live entries.

Cheers

1 Answer 1

2

You're correct. Craft will not run validation upon saving disabled elements.

This is not a bug – it's just designed that way (as far as I can tell from looking at the relevant source code in the ElementsService).

As for a workaround – you'll need a plugin.

This is the easiest approach I can think of:

  1. Listen for the entries.onBeforeSaveEntry event
  2. In the event handler, validate the entry model manually, if the entry is disabled
  3. If validation fails, add error messages to the model and set the $event object's performAction attribute to false

This should stop the entry from going through, and should also display any error messages in your form:

craft()->on('entries.beforeSaveEntry',function(){
    $entry = $event->params['entry'];
    if(!$entry->enabled && !craft()->content->validateContent($entry))
    {
        $entry->addErrors($entry->getContent()->getErrors());
        $event->performAction = false;
    }
    return $event;
});

Anyway, since I wanted to test the above before responding here, I wrote the plugin. It seems to do the job, so hopefully it'll help you out!

https://github.com/mmikkel/AlwaysValidate-Craft

7
  • thanks for the advice and the plugin.Quick question what are you pointing the action to on the frontend form to as currently I'm getting a page not found error. Cheers
    – Lettie
    May 20, 2015 at 8:30
  • In testing, I just copy pasted the Entry Form example from the official docs, so the action is entries/saveEntry. Do you get a 404 without the plugin? What do you have for your redirect value? May 20, 2015 at 8:33
  • sorry yep that sorted it was pointing at the plugin rather than entries. Thanks so much for your help. Cheers
    – Lettie
    May 20, 2015 at 8:42
  • No worries, @Lettie – happy you got it sorted! May 20, 2015 at 8:43
  • Ahh I spoke too soon. It is validating now but it is also marking the entry as enabled. This is not what I require I need it to remain disabled until an admin has reviewed it. Is there a code snippet to add to make sure it does not enable the entry?
    – Lettie
    May 20, 2015 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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