Cyberpks
Cyberpks

Reputation: 1421

Setting a default value of control in Grocery CRUD + Code Igniter

I need to set a default value of the field in Grocery CRUD.

Basically, the respective data column IsActive is set to NOT NULL.

I need to see that while adding the record, IsActive should be set to true or false but not NULL by default.

I looked for a reference all over the Internet but did not got the perfect solution for that.

Current view (in add mode)

enter image description here

So in case I do not set the rules to required, the form will post a NULL to the database.

While I need something like this (by default). enter image description here

So that the user is not required to add a value (it is not mandatory to set the field to true of false)

Upvotes: 2

Views: 1892

Answers (2)

Andreea Onica
Andreea Onica

Reputation: 357

You don't need to add code for this, in the database you have to add default value: 0 for inactive, 1 for active, like this:

ALTER TABLE `name_of_the_table` CHANGE `IsActive` `IsActive` TINYINT(1) NULL DEFAULT '1';  //this will make active be set as default


ALTER TABLE `name_of_the_table` CHANGE `IsActive` `IsActive` TINYINT(1) NULL DEFAULT '0';  //this will make inactive be set as default

Upvotes: 0

avisheks
avisheks

Reputation: 1180

You can set default rules for a column at the database level irrespective of any framework/library. Use DEFAULT keyword.

   IsActive ENUM('active','inactive') NOT NULL DEFAULT 'active'

Upvotes: 1

Related Questions