How Does Editor Handle Underlying Database Constraints?

How Does Editor Handle Underlying Database Constraints?

ayzayz Posts: 63Questions: 23Answers: 1

Playing around with my local Editor files, I edited the 'datatables_demo' table to make 'email' field Not Null (i.e. required) and removed the default '' value. Then from PHPMyAdmin I tried to create a new entry without entering an email and it gave an error.

However, when I visit /examples/simple/simple.html (which POSTS to /examples/php/staff.php and has no field to enter the email) the record is successfully inserted into the database.

2 puzzling things:

staff.php has

        Field::inst( 'email' )
            ->validator( Validate::email( ValidateOptions::inst()
                ->message( 'Please enter an e-mail address' )   
            ) ),

so how come its letting through a blank email field?

Secondly, how on earth is it bypassing the database constraint of Not Null on the email field?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    so how come its letting through a blank email field?

    The default validation is allowEmpty = true and optional = true which is how it will be allowed through. You could use the required validator or change the config options for the validator if needed.

    Secondly, how on earth is it bypassing the database constraint of Not Null on the email field?

    My guess (I can't see the page, so I'm not certain) is that the Editor Javascript form has an email field as a text input. That will always default to be an empty string, and thus that empty string is being submitted and the default for the validator is to allow that to be written to the database (thus it is not null, allowing the db to write it in).

    Allan

  • ayzayz Posts: 63Questions: 23Answers: 1

    You're right, the table has blank fields instead of NULL in the email column. A great level of caution really is needed when working with these.

This discussion has been closed.