This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hello, I'm new to October. I tried many things with Builder plugin to manage validations in back-end during 'create' or 'update' forms. I would like, depending on a field value to STOP the create or update process and UPDATE the field with a default value WITHOUT submission.
It's not clear for me where I have to put this code ? I understood that it was in the Model .php but I can't figure on which event I should code this validation... ... and how to STAY on the form with an ERROR MESSAGE, until the field is good. Thanks to put me on the way !
In your model, you can add this:
public function beforeValidate()
{
if ($this->field == 'VALUE') {
$this->field = 'DEFAULT_VALUE';
throw new \October\Rain\Exception\ValidationException(['field' => 'Invalid Value']);
}
}
Thanks for your answers. I was trying this since a long time. Now I found that beforeValidate() is only called during CREATE. For updating an existing record, I tried beforeUpdate() but it doesn't go inside. What's wrong ?
public function beforeUpdate() { \Flash::error('beforeUpdate()'); // doesn't display with SAVE }
Sorry : Markdown not working ;-(
Last updated
beforeValidate() is called for both creating/updating a record.
Flash won't work here to prevent saving the record, use throw() as below:
throw new \October\Rain\Exception\ValidationException(['field' => 'Invalid value']);
Replace "field" with your field name
Also, don't forget to add this in your model class definition:
class MyClass extends Model
{
use \October\Rain\Database\Traits\Validation;
...
Thanks it works !
Sorry, but as the documentation is quite light, should I understand that this instruction invalidates the form submission, creating a validation error on the specified field and stopping the code ?
And what is the best way to display debug messages without perturbations ?
Last updated
Incremental said:
Thanks it works !
Sorry, but as the documentation is quite light, should I understand that this instruction invalidates the form submission, creating a validation error on the specified field and stopping the code ?
Yes, that is correct.
And what is the best way to display debug messages without perturbations ?
Not sure what you mean.
OK so, ValidationException is the last executed of beforeValidate()
I was wondering how to display debug messages, as echo() can't be used and Flash seems not working everywhere...?
I tried throw new \SystemException('beforeValidate()='.$var);
but I get a popup displaying the message AND : on line 121 of C:\Dev\WinNMP\WWW\site\plugins\lb\events\models\Event.php
As only the last message is displayed with Flash, I would like some popup with a OK button to continue the code step by step... Is it possible ???
Thanks for your kind help
Last updated
As only the last message is displayed with Flash, I would like some popup with a OK button to continue the code step by step... Is it possible ???
I don't know how to do that.
and how do you put your traces, as throw stops everything ?
I experienced this popup without an indication of the code line :
throw new \ApplicationException('beforeValidate() : '.$called); // Popup with OK & STOP
but it stops the code...
By the way, you helped me a lot to understand the validation with beforeValidate()
But is there a way to correct a field with a default value ???
Your instruction :
$this->field = 'DEFAULT_VALUE';
has no effect.
I'm searching a way to do this since months. Is it impossible in October ???
1-13 of 13