[英]Setting a default value of control in Grocery CRUD + Code Igniter
I need to set a default value of the field in Grocery CRUD.我需要在 Grocery CRUD 中设置字段的默认值。
Basically, the respective data column IsActive
is set to NOT NULL
.基本上,相应的数据列
IsActive
设置为NOT NULL
。
I need to see that while adding the record, IsActive
should be set to true
or false
but not NULL
by default.我需要看到,在添加记录时,
IsActive
应该设置为true
或false
,但默认情况下不是NULL
。
I looked for a reference all over the Internet but did not got the perfect solution for that.我在整个互联网上寻找参考,但没有得到完美的解决方案。
Current view (in add mode)当前视图(在添加模式下)
So in case I do not set the rules
to required
, the form will post a NULL
to the database.因此,如果我没有将
rules
设置为required
,表单会将NULL
发布到数据库。
While I need something like this (by default).虽然我需要这样的东西(默认情况下)。
So that the user is not required to add a value (it is not mandatory to set the field to true of false)这样用户就不需要加值了(不强制设置字段为真或假)
You can set default rules for a column at the database level irrespective of any framework/library.无论任何框架/库如何,您都可以在数据库级别为列设置默认规则。 Use DEFAULT keyword.
使用 DEFAULT 关键字。
IsActive ENUM('active','inactive') NOT NULL DEFAULT 'active'
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:您不需要为此添加代码,在数据库中您必须添加默认值:0 表示非活动,1 表示活动,如下所示:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.