Victor Victor - 24 days ago 19
PHP Question

CakePHP 3.X: Difference buildSchema X buildValidator

I'm learning CakePHP 3.X version and I'm really confused about Form validation.

In the documentation (http://book.cakephp.org/3.0/en/core-libraries/form.html) is saying that you need to create a class to validate your form and you have two methods to validate the data.

First method:

protected function _buildSchema(Schema $schema)
{
return $schema;
}


Second method:

protected function _buildValidator(Validator $validator)
{
return $validator;
}


What is the really difference between them?

In the documentation example is saying this:


_buildSchema is used to define the schema data that is used by FormHelper to create an HTML form. You can define field type, length, and precision.

_buildValidator Gets a Cake\Validation\Validator instance that you can attach validators to.


I'm confused because if you look at the examples in the documentation you gonna see that he's validating the input length and things like that in the second method, but in the description of the first method he's saying that you can do that too?

Is it possible to explain to me the really differece between them and show to me an example?

Thanks!

Answer

Database Schema

A database schema of a database system is its structure described in a formal language supported by the database management system (DBMS). The term "schema" refers to the organization of data as a blueprint of how the database is constructed.

The schema describes your data structure, your table layout. A blue print.

Data Validation

In computer science, data validation is the process of ensuring that a program operates on clean, correct and useful data. It uses routines, often called "validation rules" "validation constraints" or "check routines", that check for correctness, meaningfulness, and security of data that are input to the system. The rules may be implemented through the automated facilities of a data dictionary, or by the inclusion of explicit application program validation logic.

Validation is the process of checking the data you put into the database, into the schema that describes the structure for your data.

Conclusion

You need to describe your schema first. The data you put into it through the form is validated.

Also the documentation is pretty clear on that as well, I think you just misunderstood it: You can define field type, length, and precision. Defining something is not validating something. :)

Comments