I am have a model where I have using validatesPresenceOf() to check the presence of id column in my table.
<cfscript>
component extends="Model"
{
public function init()
{
validatesPresenceOf(property="id", message="error msg");
table(mytable);
}
}
</cfscript>
Now the id column in my table is a auto generated field via a trigger, so I don't really need to see if the id is NOT NULL except in the below situation.
<select name=data>
<option value=""></option>
<option value=data.id></option>
</select>
Above, I am using cfwheel validation to check if the use submits a option value with an id and not value that is null. So here I am using the validatesPresenceOf() function to see if a id is submitted.
But in another location I have to save data to this table and my code is as below:
data = model(tbl.others).new();
data.name = name;
data.save();
The above code doesn't insert the record to the table because of validatesPresenceOf() on the id column, and as you can see I am not setting the id on my insert data because it is a auto generate field in my table.
My question is there a work around this, maybe I can temporary turn off that particular id validation or may I can temporary validate the id and then remove that validation condition afterwards.
I tried the following and it doesn't work:
public function custom_save(name)
{
automaticValidations(false);
data = model(mytable).new();
data.name = name;
automaticValidations(true);
return data;
}
you could try not validating at all (using the model)
http://docs.cfwheels.org/v1.4/docs/save
Model Class Functions using save() such as create() and updateAll() use a validate parameter that defaults to true. You could set it to false and do other validations manually.
Related
In my table I have a field that needs to be the same value as the id field which is auto increment and then the value is concatenated with certain strings.
How do I do this in store function of my controller?
I'm thinking of doing update right after insertion but I don't know how to get the inserted id, and that might be a problem if two or more users doing the same insertion into database at the same time.
Thanks
UPDATE:
in my store controller:
public function store(Request $request)
{
$kartukeluarga = new kartukeluarga();
$kartukeluarga->nomorkk = $request->input("nomorkk");
$kartukeluarga->kepalakeluarga = $request->input("kepalakeluarga");
$kartukeluarga->alamat = $request->input("alamat");
$kartukeluarga->save();
return redirect()->route('kk.index')->with('message', 'Item created successfully.');
}
the new field I want to add is 'rekamMedik' which doesn't need an input form but automatically concatenate the id and this string "kk-value of the id"
You can save details and then update that column.
If you have model Test.
$test = new Test();
$test->column_1 = "abc";
$test->column_2 = "def";
$test->column_x = ""; // Set Empty to the column where you will save id later
$test->save();
$test->column_x = "string".$test->id;
$test->save();
This might work.
I have a spring form for a complex object that contains a List of email address objects within a One-To-One relationship object that contains a One-To-Many to email addresses. The use-case is as follows: user wants to add new email addresses to the existing ones in the form. This is done via a pop up window and a jQuery that appends a new option element inside the select statement as such:
<option value="0">new.email#address.com</option>
All the prepopulated options in the loaded form of course already have an ID set, such as:
<option value="1">existing.email#email.com</option>
In my controller, the submitted form contains the ID field (1 for the prepopulated object and 0 for the 'new' email address), but the actual value in the form is null - i.e. first#email.com is not submitted with the form and bound to the emailAddress field in the EmailAddress object:
This code:
List<EmailAddress> emailAddress = batchJob.getDestinationExtras().getMailsInternal();
for (EmailAddress e : emailAddress) {
logger.info(e.getEmailAddressId() + " "+e.getEmailAddress());
}
Results into output:
1 null
0 null
This is not a problem for the existing objects because with the ID they can simply be retrieved from the database using hibernate either in the controller or already during the parse stage (I am using a formatter implementation for the EmailAddress object). But for the 'new' objects created dynamically by the user I have a problem because they do not yet exist in the database and thus the actual email address value can not be retrieved in this way.
What could be a solution to this problem?
The JSP tag is as follows:
<form:select multiple="true" path="destinationExtras.mailsInternal" id="internalMailsList">
<form:options items="${internalMailsList}" itemValue="emailAddressId" itemLabel="emailAddress" />
</form:select>
The relevant parts of the objects involved below - omitting other fields and ID fields where not relevant:
#Entity
#Table(name="batchjob")
public class BatchJob {
(...) id + other fields
#ManyToOne(cascade=CascadeType.ALL)
#JoinColumn(name="destinationextrasid")
private DestinationExtras destinationExtras;
(...) getters and setters
}
#Entity
#Table(name="destinationextras")
public class DestinationExtras {
#OneToMany(cascade = CascadeType.ALL)
#JoinColumn(name="emailaddressid")
private List<EmailAddress> mailsInternal;
}
#Entity
#Table(name="emailaddresses")
public class EmailAddress {
#Id
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ_EMAILADDRESSID")
#SequenceGenerator(name="SEQ_EMAILADDRESSID",sequenceName="SEQ_EMAILADDRESSID",allocationSize=1)
private int emailAddressId;
#Column(name="email_address")
private String emailAddress;
}
When it goes wrong, think about the low-level HTTP protocol ... JQuery is very clever, so is Spring, but they can only communicate through HTTP ! And when submitting a form containing a <select> field, the only thing that is sent is the value of the selected option(s).
So if you want Spring to use the text of the new email, you could try to put it in the value of the <option> field, like :
<option value="new.email#address.com">new.email#address.com</option>
but I think you will have to change all the select to systematically use the text as value and change it also in your controller, to avoid mixing integer ids and texts. Your options would then be :
<form:options items="${internalMailsList}" itemValue="emailAddress" itemLabel="emailAddress" />
You can also just add a hidden field with the help of jQuery, with the email text as value and use it if you find a value of 0, like that :
<input type="hidden" name="newmail" value="new.email#address.com"/>
<select ...>
<option value="0">new.email#address.com</option>
Personnally, I would prefere that second way, but it really depends on you controller structure.
Codeigniter check unique value while editing form.
When edit the form it checks for current row, and it give username already exist. so how to avoid it.and don't check for current record.
So how can i pass id to is_unique function in CI_Form_Validation class function and add condition?
See my below code:
$this->custom_validation->set_rules('username','Username',
'trim|required|max_length[30]|is_unique[adminuser.username]');
Simply put a unique validation only if value of that field is changed ie.
$old_value='old value';
if ($posted_value!=$old_value) {
$this->custom_validation->set_rules('username','Username',
'trim|required|max_length[30]|is_unique[adminuser.username]');
} else {
$this->custom_validation->set_rules('username','Username',
'trim|required|max_length[30]);
}
I have a ddl which is populated with hours of day 01-23. This is on a form which is used to book an item of equipment. The hour is populated to a db field. The issue is this, when the booking form is opened to alter the time the ddl shows the hour that was booked, when changed though and the form is submitted the value passed on post is the initial value from db not the new selected hour.
this is the basic pieces of code. any idea why the newly selected ddl value is not passed to the model??
View
<%= Html.DropDownList("ddl_Hour", Model.ddlHour,
new { #class = "DropDown", style = "width: 40px” })%>
Model
private string _ddlHourSelectedValue = "0";
public SelectList ddlHour
{
get
{
return (new System.Web.Mvc.SelectList(_ddlHour, "intValue", "Text", Convert.ToInt32(_ddlHourSelectedValue)));
}
}
public string ddlHourSelectedValue
{
get
{
return _ddlHourSelectedValue;
}
set
{
_ddlHourSelectedValue = value;
}
}
param[6] = new SqlParameter("#Timeslot", ddlHourSelectedValue);
The field in your view is called "ddl_Hour" However is there a variable in your Model with the same name? Otherwise the MVC framework will not automatically populate the value in the model.
Two ways you could go about this.
1
In your controller methods that accepts a post, you can add the parameter: FormCollection fc to the method. This key value pair collection will allow you to fetch results from fields in the post data like so:
string selectedValue = fc["ddl_Hour"];
2
Or you can modify your model to include a variable with the same name as the drop down list so that it is automatically populated for you.
public string ddl_Hour { get; set; }
You should then be able to access the result of the drop down list selection on post from that variable.
I have a Dynamic Data column that I want to be populated by a field from the Session collection. (So that when the user chooses to manipulate data related to that column, where necessary, they will see information based on that field, which is stored in session).
Is there an attribute I need to use, or create?
I have had success creating an extended property in the metadata :
public partial class MyTable
{
[ScaffoldColumn(true)]
public string MyValueFromSession
{
get
{
return Session["theAttribute"].ToString();
}
}
}
then you can specify a new column called MyValueFromSession in the MyTable metadata.
Hope this helps.