# |
May 30th 2021, 15:06 |
kevin.pfeifer |
ah wait |
# |
May 30th 2021, 15:04 |
kevin.pfeifer |
```if( !empty($frat->_joinData->score ) || $frat->_joinData->score > $frat->maximal || $frat->_joinData->score<1 ){``` |
# |
May 30th 2021, 15:03 |
kevin.pfeifer |
it checks each value "if-part" one after the other |
# |
May 30th 2021, 15:03 |
kevin.pfeifer |
put the !empty at first |
# |
May 30th 2021, 15:02 |
etibor |
if($frat->_joinData->score > $frat->maximal || $frat->_joinData->score<1 || !empty($frat->_joinData->score )){ |
# |
May 30th 2021, 15:01 |
etibor |
its shows the validation custom error message |
# |
May 30th 2021, 15:00 |
kevin.pfeifer |
what error do you get |
# |
May 30th 2021, 15:00 |
etibor |
this is what i used : !empty($frat->_joinData->result) |
# |
May 30th 2021, 14:57 |
kevin.pfeifer |
and the field the one which you want to check |
# |
May 30th 2021, 14:57 |
kevin.pfeifer |
well $entity should be your $frat |
# |
May 30th 2021, 14:57 |
etibor |
it shows everytime a validation error no matter field value |
# |
May 30th 2021, 14:56 |
etibor |
i dont know what happened but adding the `!empty($entity->field)` to the end of the if, makes crashed the validator |
# |
May 30th 2021, 14:41 |
kevin.pfeifer |
because rules are only checked after the validator above has succeeded |
# |
May 30th 2021, 14:41 |
kevin.pfeifer |
if you require to have something selected you need to add that to the valditors |
# |
May 30th 2021, 14:37 |
kevin.pfeifer |
which i like to do with `!empty($entity->field)` |
# |
May 30th 2021, 14:37 |
kevin.pfeifer |
then you first need to check if they are present |
# |
May 30th 2021, 14:37 |
kevin.pfeifer |
if you get warnings/errors because some properties are not set |
# |
May 30th 2021, 14:35 |
etibor |
Or if($frat->_joinData->score > $frat->maximal || $frat->_joinData->score<1 || ($frat->_joinData->result != null)){ does not works too |
# |
May 30th 2021, 14:34 |
etibor |
its just dont allow when the selected item is null i tried to add that exception: if($frat->_joinData->score > $frat->maximal || $frat->_joinData->score<1 || !is_null($frat->_joinData->result)){ |
# |
May 30th 2021, 14:33 |
etibor |
yes i did it, i added to the DocumentsTable |
# |
May 30th 2021, 14:12 |
kevin.pfeifer |
so you can sort of create custom flash messages in your model even though you don’t have access to the flash component inside your model |
# |
May 30th 2021, 14:12 |
kevin.pfeifer |
which can be extended in the model and later accessed in the controller |
# |
May 30th 2021, 14:11 |
kevin.pfeifer |
basically I added a property to that class |
# |
May 30th 2021, 14:04 |
kevin.pfeifer |
you added ```class ProjectsTable extends Table { public array $flash_errors = [];``` to your DocumentsTable? |
# |
May 30th 2021, 14:03 |
etibor |
thank you Kevin its almost good: Undefined property `flash_errors`. You have not defined the `flash_errors` association on `App\Model\Table\DocumentsTable`. |
# |
May 30th 2021, 13:59 |
kevin.pfeifer |
Projects is your Table of course |
# |
May 30th 2021, 13:59 |
slackebot |
$this->Flash->error( __d('alfred_projects', 'The project could not be saved. Please, try again.' ) ); } }``` |
# |
May 30th 2021, 13:59 |
kevin.pfeifer |
```if( $this->Projects->save( $project ) ) { $this->Flash->success( __d('alfred_projects', 'The project has been saved.' ) ); return $this->redirect( [ 'action' => 'edit', $project->id ] ); } else { if( sizeof( $this->Projects->flash_errors ) > 0 ) { foreach( $this->Projects->flash_errors as $error ) { $this->Flash->error( $error ); } } else { |
# |
May 30th 2021, 13:58 |
kevin.pfeifer |
and access these messages in your controller |
# |
May 30th 2021, 13:58 |
kevin.pfeifer |
``` $rules->add(function ($entity, $options) { array_push( $this->flash_errors, "my error message" ); return false; }, 'ruleName', [ 'errorField' => 'is_done', 'message' => 'My custom message' ]);``` |
# |
May 30th 2021, 13:57 |
kevin.pfeifer |
```class ProjectsTable extends Table { public array $flash_errors = [];``` |
# |
May 30th 2021, 13:57 |
kevin.pfeifer |
i guess it would be easier for you to set “custom error messages” like so |
# |
May 30th 2021, 13:56 |
etibor |
$this->Form->control('storage.'.$i.'._joinData.score',['label'=>(false), 'type'=>'select','options'=>$options,'empty'=>__('choose') ]); // i have actually 4 fields like this |
# |
May 30th 2021, 13:55 |
etibor |
if the error message is displayed on the top of the page it would be also good |
# |
May 30th 2021, 13:55 |
etibor |
actually i have a field for total score, echo $this->Form->control( 'score' ); and if there is the custom error, the message is displayed there |
# |
May 30th 2021, 13:51 |
slackebot |
<kevin.pfeifer> |
# |
May 30th 2021, 13:51 |
kevin.pfeifer |
which results in |
# |
May 30th 2021, 13:51 |
kevin.pfeifer |
``` echo $this->Form->control( 'is_done' );``` |
# |
May 30th 2021, 13:50 |
kevin.pfeifer |
and my form outputs |
# |
May 30th 2021, 13:50 |
kevin.pfeifer |
in my Table |
# |
May 30th 2021, 13:50 |
kevin.pfeifer |
``` public function buildRules( RulesChecker $rules ): RulesChecker { $rules->add(function ($entity, $options) { return false; }, 'ruleName', [ 'errorField' => 'is_done', 'message' => 'My custom message' ]); return $rules; }``` |