Help with isset($_POST['action']) && $_POST['action'] === 'edit'
Help with isset($_POST['action']) && $_POST['action'] === 'edit'
I have an editor instance where I want to update another table on edit.
All was working ok, until I added validation to some of my editor fields.
If everything validates ok, no problem, but the $_POST['action'] === 'edit' block of code will run regardless of whether my editor validates, throwing an error
How can I check to see if the editor input validates before running the edit block ?
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Use the
Editor->validate()
method - which is just what the Editor class calls internally to perform the validation. Obviously you need to set up the list of fields etc first.Allan
Sorry, I very well may be missing the point here.
The validation bit works, so for example
an invalid entry will prevent the editor form from posting
but if i add the following code after ->process
}
this block runs regardless of the form data being valid.
if it IS valid, all is well, but if it isn't, it throws an error.
I would like the second block to run only if the entry is valid
Add it before the data is processed :-). You want to ensure that it is valid before the data is acted upon presumably.
You want to set the
Editor
class up fully, and then execute the validation. If it passes go on to do whatever is needed in your custom actions and call theprocess()
method.Allan
In my own mind, i thought that running the second block after the process method would mean that if the editor class had invalid data, then it wouldn't process, hence the second block wouldn't execute.
I tried moving the code before the data is processed, but that throws an error every time
If it helps clarify, here is the full editor class
The
process()
method will callvalidate()
itself, and if validation fails it will not continue to process the data - but it does not stop the rest of the script executing.I don't see a call to
$editor->validate()
above. If you wanted to validate the data before doing something, I would expect it to be placed around like 106.However, if you are happy to have the additional actions after the
process()
call, then you could just check what$data
contains and see if there are any field errors. If so, then validation failed.Allan
Thanks for your PM - I hope you don't mind continuing the discussion here (just as this is where the code is, so I can easily refer to it!).
Does your additional logic need to run before or after the
->process()
method is called? The solution will depend directly on that.Regards,
Allan
I dont think it matters where it runs, but here is my scenario
Basically all I want to do is validate my editor fields, and at the point the table is updated, I (may) need to update another table, dependent upon the values returned.
If any of the editor fields do not validate, i want to see a validation error, so the form is not submitted.
eg if I am changing the contacttypeid ($_POST['data']['contacts']['ContactTypeID']).
If the value returned is '2', do nothing, otherwise delete a row in the other table
At the moment, if i try and run the delete, i get an error, but if I remove the validation, it processes ok, (but obviously the data entered into the main table is not what i want)
Hope this makes sense
Got it! Thanks for the clarification.
Try this:
I've cut down the blocks of code as I've not made any changes there and so that the change is clearer.
The only thing I've done is to add
! isset( $data['fieldErrors'] )
into your condition for doing the additional work. I think this is the easiest and most straight forward option.So basically all it does is to check and see if Editor found any validation errors (we know that if
fieldErrors
is set, then there were errors - if it is not, then there were no errors).Does that make sense? And more importantly, when you update your code, does that work for you?
Regards,
Allan
Looking good
$data['fieldErrors'] is the check I needed.
My scenario is currently simpler than it will be, I need to make checks at create too, but now this logic is in place, I can get on with it.
Thanks again