Validation of Fields?
Validation of Fields?
The Problem to which I am going through is Validation. I want to validate my editable fields . What I've tried is:
<?php
// DataTables PHP library
include( "../php/DataTables.php");
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Upload,
DataTables\Editor\Validate;
Editor::inst( $db, 'materials' )
->fields(
Field::inst( 'id' ),
Field::inst( 'name' ),
Field::inst( 'description' ),
Field::inst( 'specs' ),
Field::inst( 'isCode' )->validator( 'Validate::numeric' ),
Field::inst( 'unit' )
)
->process( $_POST )
->json();
<?php
>
```
?>
which is not working. Initially Ive implemented a client side validation using:
```js
editor.on( 'preSubmit', function ( e, o, action ) {
if ( action !== 'remove' ) {
var materialName = editor.field( 'name' );
// Only validate user input values - different values indicate that
// the end user has not entered a value
if ( ! materialName.isMultiValue() ) {
if ( ! materialName.val() ) {
materialName.error( 'A material name must be given' );
}
if ( materialName.val().length >= 20 ) {
materialName.error( 'The first name length must be less that 20 characters' );
}
}
// ... additional validation rules
// If any error was reported, cancel the submission so it can be corrected
if ( this.inError() ) {
return false;
}
}
} );
which now have been removed. But still the message on putting this field empty is "This field is Required" which is the default message of validator method in field options which was temporally set by me to check.
HOW TO IMPLEMENT THE VALIDATIONS IN FIELDS OTHER THAN MATERIAL NAME?
HOW TO IMPLEMENT MORE THAN ONE VALIDATION LIKE BASIC AND NUMBERS,ETC. FOR A SINGLE FIELD?
Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.
Replies
Each of the
Field
instances that are created in your code above can have itsvalidator()
method called (indeed, you can call it multiple times if you want to add multiple validators).So you might have something like:
You would simply call the
validate
method multiple times.The read to use built in validators are documented. That page also details how you can write your own validation methods if the built in ones don't suit your needs.
Allan
Thanks for your response Allan. I got your viewpoint, but when I am Implementing numeric validation in material IS code, there is no response. Also, having removed the validation from name, it still displays 'this field ids required'. Does it signifies any internal problem of overriding the changes?
Absolutely no response at all? That suggests there might have been a 500 error. Have you checked the server's error logs to see if it reports any errors?
If there is no validation, the only error that can occur is an SQL error (from invalid input) - other than a configuration error of course.
If you have a link to the page so I can take a look and debug it, that would be useful.
Allan