PHP Validation on mjoin
PHP Validation on mjoin
rlangille2
Posts: 10Questions: 3Answers: 0
Hello,
Can somebody please show me how to write a validation to check if no option was selected for a multi-select drop down, and return a message on the form? I can't seem to do it with the mjoin. The value I want to check is Field::inst('DataSourceID').
->Join(
Mjoin::inst( 'DataSources' )
->link( 'Templates.TemplateID', 'TemplateDataSources.TemplateID' )
->link( 'DataSources.DataSourceID', 'TemplateDataSources.DataSourceID' )
->order( 'DataSourceID asc' )
->fields(
Field::inst( 'DataSourceID' )
->validator( Validate::required() )
->options( Options::inst()
->table( 'DataSources' )
->value( 'DataSourceID' )
->label( 'DataSource' )
->where( function ($r) {
$r->where( 'Enabled', '1', '=' );
})
->order( 'DataSource' )
),
Field::inst( 'DataSource' ),
Field::inst( 'Enabled' )
)
)
This discussion has been closed.
Replies
Hi,
You'd need to use a global validator for that. The field validators in the Mjoin just run on the individual inputs.
Allan
Thank you,
I checked out the global validator documentation but I am so far unable to get it working. Where in my php should I place this example..
There is no mjoin in the example provided, so I am unsure where in the code it should go.
It would go just before the
->process()
method normally. It could go anywhere in theEditor
chain before that method, but that's typically where I've put it myself.If you do:
You'll be able to see the data structure for your configuration. It will cause an invalid JSON error (system error in Editor), but using the network inspector you'll be able to see the data and use that to form the validator needed.
If you need a hand with that, let me know - if you could show the printed debug statement that would be useful.
Allan
Thanks Allan,
I am struggling with using the validator function. I just want the validation rule to not allow the form to be submitted if no DataSource is selected (you can find the field at the very bottom of the output below.)
This should do it I believe:
Allan
Hi Allan,
Yes that worked. Here is the complete code so others can benefit if they have a similar question.
Thank you!