Correct way to use validator(Validate::required()) -> crashes
Correct way to use validator(Validate::required()) -> crashes
When I use->validator(Validate::required())
my page crashes. It seems it complains about an empty array. Strange because this works:
Field::inst( 'publikasjon.fylke' )
->options(
Options::inst()
->table('fylke')
->value('fylke')
->label('fylke')
)
->validator(Validate::required())
->validator(Validate::dbValues()),
When I change the validator to
Field::inst( 'publikasjon.fylke' )
->options(
Options::inst()
->table('fylke')
->value('fylke')
->label('fylke')
)
->validator(Validate::required())
the page crashes.
Am I writing the code wrong?
This question has accepted answers - jump to:
Answers
Can you show me the error message please?
Thanks,
Allan
DataTables warning: table id=example - Invalid JSON response. For more information about this error, please see https://datatables.net/tn/1
and I turned on debug and can see this
Fatal error: Uncaught Error: Call to a member function dbField() on array in /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php:1142 Stack trace: #0 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(1034): DataTables\Editor->_get(NULL, Array) #1 /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php(716): DataTables\Editor->_process(Array) #2 /home/1/m/mfu/www/code/js/Editor-PHP/controllers/upload-mfu.php(68): DataTables\Editor->process(Array) #3 {main} thrown in /home/1/m/mfu/www/code/js/Editor-PHP/lib/Editor.php on line 1142
I am sorry but I did not see that the code I wrote was not correct but just two equal code blocks!. The problem is when I use
validator(Validate::optional())
So this code crashes the page and gives the error I wrote in the last postThat's not where I would have expected such an error to show! This is the code in question it is in the
get
routine, which is what surprises me.Can you show me the full PHP for the Editor instance please?
Also to confirm, you are saying using
optional
makes it crash, butrequired
doesn't?Allan
Yes
optional
makes it crash whilerequired
doesn´t. I don´t want the value to be required.. I am using the dropdown "fylke" with the text value as both value and label.With debug I see this in Chrome inspector:
Her is the full code for the php:
Thank you! And apologies for having not seen the error before - there is no
Validate::optional()
method. There is anoptional()
on theValidateOptions
class though, which can be passed as the second parameter toField->validator()
.I think this is what you want:
Allan
Thank you Allan! This works. I could not find out where to put "optional" from reading the manual page about validation . So I need the
Validate::dbValues()
line also? What I want is to allow for empty(no) values for the field.Would be what you need to allow an empty string. The
optional
defines if the field must be submitted or not.allowEmpty
defines that if it is submitted is it allowed to be empty.Allan
Thanks Allan! I tested and it worked fine with empty value with
ValidateOptions::inst()->optional(true)
Did not complain at least. But I will use theallowEmpty(true)
as you suggest!