2

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)

Aaron Belchamber
  • 1,480
  • 1
  • 16
  • 20
Cyberpks
  • 1,401
  • 6
  • 21
  • 51

2 Answers2

1

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'
avisheks
  • 1,178
  • 10
  • 27
  • thanks buddy, that's a cool idea. I really did not thought that way. I just had to set the default value of the column in my database and CRUD will be generated automatically – Cyberpks Jul 29 '14 at 06:50
0

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
Andreea Onica
  • 315
  • 5
  • 13