Inline-editing failing validation due to other fields.
Inline-editing failing validation due to other fields.
Short version:
on this post
Allan says..
"The other option is to modify the validation options so that they are only applied when these data is actually submitted."
How can one do this?
Long version:
I am having inline-editing failing validation due to another field in the form.
If I edit 'pluralName' inline, i get a message back under the 'pluralName' field as "Must only contain a-z", which is the 'tableName' error message.
The edit works fine when using the normal edit form, so its passing validation, I assume its failing because the 'tableName' isnt passed along with the inline edit.
If i add
formOptions: {
inline: {
submit: 'allIfChanged'
}
},
to the editor, it all works, however its submitted all the fields, and causing unnecessary work, especially if extra logic is being done when those other fields are passed along.
Editor::inst($this->dtDatabase, $this->table)->fields(
Field::inst('id'),
Field::inst('tableName')
->validator(Validate::maxLen(60, ValidateOptions::inst()->message('Max length is 60')))
->validator(Validate::minLen(1, ValidateOptions::inst()->message('Min length is 1')))
->validator(Validate::notEmpty(ValidateOptions::inst()->message('Required')))
->validator(function ($val) {
return !preg_match('/^[a-z]+$/', $val) ? 'Must only contain a-z' : true;
}),
Field::inst('singularName'),
Field::inst('pluralName'),
Field::inst('iconName'),
Field::inst('storageEngine')
->validator(Validate::values(['InnoDB', 'MyISAM'], ValidateOptions::inst()->message('Storage engine invalid'))
)
)->on('postCreate', function ($editor, $id, &$values, &$row) {
$this->_createSectionTable($editor, $id, $values, $row);
})->on('preEdit', function ($editor, $id, &$values) {
$this->_editSectionTable($editor, $id, $values);
})
->process($postData)
//->debug(true)
->json();
is there a way to only validate if the fields are present?
This question has an accepted answers - jump to answer
Answers
Good question - yes it is possible, but a further question will become, is it worth doing (with how it is done currently)?
Let me explain - you can use
preEdit
/preCreate
to add the validation - e.g.:In the case of
preCreate
you'll want that always to be present (i.e. noisset
), so there would need to be some consideration for that as well. Probably putting the validation addition into a function would be best, indicating if it is apreCreate
orpreEdit
event that is happening.Coming back to my question, you can see now that we've traded work done (by the CPU) for code complexity and reduced maintainability.
Perhaps the server-side libraries should provide a method that would handle this for you, but I'm not actually sure what an intuitive API for it would be! It would need to handle the create / edit difference as well as allowing for fields which are allowed to be empty.
Regards,
Allan