Editor - Chained select boxes and multiple query inserts on update.
Editor - Chained select boxes and multiple query inserts on update.
Hi. I wasn't able to find this in the documentation, or I might have searched for the wrong wording and such.
I will like to know if the following scenarios are possible at all if not, where should I start adding my own functions to make it all work.
1) Chaining Select boxes and validation.
On client side I have the following fields:
{label: "Subcontracted",
name:"subcontracted.to_c",
type:"select"},
{label:"Operative",
name:"jobs_assigned.id",
type:"select"}
I need to enable the one only if the other is set to null, so for example the field labeled Operatives should be active and validated only if the field labeled Subcontracted is set to an empty value. However if the field Subcontracted is set to anything but an empty value than, the field Operatives must be set to an empty value. I can write my own javascript function to get around this, that's the easy part, however rule of thumb, never trust the user data, so I need to validate this on the server side.
Currently I have both of the fields set to use: ->validator( 'Validate::dbValues' )
but this fails when the submitted value is empty. So the question here is, I guess I need to create my own validation function, which will match agains db values + empty right?
2) If all validation have passed, before updating the tables (multiple joined tables) which is nicely handled by editor, I need to run some additional queries. I need to do some loop's and modification in other tables which are not included in the join at all, and some operations which requires me to loop the same query a few times, where should I include this? Also how should I handle it if the user edits multiple rows? Is the validation done for all of the rows then it moves on to executing the query or is it done for each row seperately in a loop function?
This is how my server-side script looks at the moment:
$editor=Editor::inst( $db, 'jobs' )
->fields(
Field::inst( 'company.Name' ),
Field::inst( 'stores.short_add' )->validator( 'Validate::notEmpty' ),
Field::inst( 'jobs.description' )->options( 'job_descriptions', 'id', 'short_description', function ($q) {
global $owner;
$q->where('owner',$owner);})
->validator( 'Validate::dbValues' ),
Field::inst( 'job_descriptions.short_description' ),
Field::inst( 'jobs.startdate' ),
Field::inst( 'clients.Company_Name' ),
Field::inst( 'operatives.name' ),
Field::inst( 'jobs.freq' )->validator( 'Validate::numeric' ),
Field::inst( 'jobs.freq_type' )->validator( 'Validate::numeric' )->options( function () {
return array(
array( 'value' => '0', 'label' => 'Interval' ),
array( 'value' => '1', 'label' => 'Set Date' )
);
} ),
// Field::inst( 'subcontracted.to_c' )->options( 'clients', 'id', 'Company_Name')->validator( 'Validate::dbValues' ),
Field::inst( 'subcontracted.to_c' )->options( function () use ( $db ) {
$out[] = array('value' => '', 'label' => 'Select from list');
$attrList=$db->selectDistinct ('clients',['id','company_Name']);
while ( $row = $attrList->fetch() )
{
$out[] = array(
"value" => $row['id'],
"label" => $row['company_Name']
);}
return $out;
})->validator( 'Validate::dbValues' ),
Field::inst( 'jobs_assigned.id' )->options( 'operatives', 'id', 'name', function ($q) {
global $owner;
$q->where('owner',$owner);})
->validator( 'Validate::dbValues' ),
Field::inst( 'jobs.owner' )->setValue($owner)
)
->leftJoin( 'stores', 'stores.id', '=', 'jobs.store_id' )
->leftJoin( 'company', 'stores.company_id','=','company.id')
->leftJoin( 'subcontracted', 'subcontracted.jobid','=','jobs.id')
->leftJoin( 'clients','clients.id','=','subcontracted.to_c')
->leftJoin( 'jobs_assigned','jobs_assigned.jobid','=','jobs.id')
->leftJoin( 'operatives','jobs_assigned.operativeid','=','operatives.id')
->leftJoin( 'job_descriptions','job_descriptions.id','=','jobs.description');
$editor->where('jobs.owner',$owner);
$editor->process( $_POST );
$editor->json();
Hope my questions aren't too much all over the place....
This question has an accepted answers - jump to answer
Answers
1) Yes that is possible, but it isn't something that is built into Editor. You would need your own custom validator to check that the value is something that is allowable based on the other values in the form.
2) Sounds like a perfect use case for server-side events.
Regards,
Allan
Hi Allan.
Thank you again... I went trough the documentation a few times, but never read the Events section... wonder why