Set default display value for Symfony2 form field - symfony

I've got a form with some display only fields in it. These fields are usually DateTime values... but when empty/null I would like to display the string "never".
EDIT:
To be more explicit: The field should show the DateTime value from the database and if null the string 'never' should be displayed.
How should I do that?
Thanks in advance

You can use Symfony2 Data Transformers :
In the transform() function you can check if your date is null and then return the 'never' string. Otherwise return a string representation of your date.
In the reverseTransform() function you can check if the string is 'never' and then construct a null DateTime object. Otherwise, you transform the given string into a valid DateTime object with something like 'strtotime()` PHP function.

Related

Why does update_post_meta change the value of my meta key?

When I use the update_post_meta() function to change a value of a key:
when it's a normal string it works
but when the string is the same as a JSON file it appends some extra string.
For example, when I save this string:
a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}
It is saved on the wp_postmeta table as:
s:162;"a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}"
When I use a short string there are no problems. How can solve this?
My code:
$edd_files='a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}';
update_post_meta($download_id,'edd_download_files',$edd_files);
The reason why this is happening is because update_post_meta() serializes the value you pass as third parameter (see update_metadata()).
Your $edd_files variable is a serialized array -not a "JSON file"- and update_post_meta() is serializing it again before saving it to the database, hence the reason why your serialized string changed like that.
I don't know why you're assigning $edd_files a serialized string but you can convert it back to an array using the maybe_unserialize() function before saving it as a post meta and then the value will be saved on the database as a serialized string as expected:
$edd_files = 'a:1:{i:1;a:6:{s:5:"index";s:1:"0";s:13:"attachment_id";s:1:"0";s:14:"thumbnail_size";s:0:"";s:4:"name";s:3:"aaa";s:4:"file";s:3:"aaa";s:9:"condition";s:3:"all";}}';
// Convert serialized string back into an array
$edd_files = maybe_unserialize($edd_files);
update_post_meta($download_id, 'edd_download_files', $edd_files);
Result:

Can I cast a string object passed on command line argument to the actual object?

Is it possible to cast a command-line passed string object back to actual object?
I want to do the following, but throwing error can't cast.
Button objPro = (Button) sender;
cProduct cp = (cProduct) objPro.CommandArgument;
If no, then why?
This is what the string holds.
cProduct cpObj = (cProduct)e.Row.DataItem;
Button btnAddProduct = (Button)e.Row.FindControl("btnAddProduct");
if (btnAddProduct != null)
{
btnAddProduct.CommandArgument = cpObj.ToString();
}
You probably can't, because it's a string. It's not a cProduct (whatever that is - consider following .NET naming conventions and naming it Product instead).
Now you could do this if you had a explicit conversion operator in cProduct to create an instance from a string.
You haven't really explained what's in the string, or what's in the type - but if your cProduct type provides a ToString method which contains all the data in a reversible form, then you could easily write a method or a constructor to create the product again:
Product product = new Product(objPro.CommandArgument);
or maybe:
Product product = Product.Parse(objPro.CommandArgument);
You'll have to write that constructor/method, of course.
I would strongly recommend using a constructor or method instead of an operator, just to keep your code clearer - it's very rarely a good idea to write your own conversion operators.
Take a look at CommandArgument on MSDN. The property is a string, when you assign the a value to the property, you aren't casting some complex type to string, you are setting a string value on the property. Can you cast a string back to your object type anyway, regardless of it being a CommandArgument. I doubt it. If the argument is an int you could try int.Parse or similar for other types which have a parse method.

How to pass null as DateTime value?

In a database, there is a field that saves a closure date. This date can be NOT NULL only if the case has been closed. If the case is not closed, it has to be NULL. How can I pass null value to a DateTime object?
Tried this but it doesn't work.
DateTime closure= dateDatumIspisa.SelectedDate ?? null;
DateTime closure= dateDatumIspisa.SelectedDate ?? DateTime.Parse("");
DateTime closure= dateDatumIspisa.SelectedDate ?? DBNull.Value;
DateTime closure= dateDatumIspisa.SelectedDate ?? DateTime.Parse(DBNull.Value.ToString());
Also tried GetValueOrDefault() but it inserts DateTime.Min value, while I need this field left empty.
Any suggestions?
Just make closure a DateTime? instead of a DateTime. The whole point of Nullable<T> is to make a nullable type from a non-nullable one.
Now, you haven't shown the type of SelectedDate - but if it's already a DateTime? then you don't need to use ?? at all. Just:
DateTime? closure= dateDatumIspisa.SelectedDate;
How familiar are you with nullable value types in general? You might want to read up on the MSDN coverage of them.
Declare
DateTime ? closure = dateDatumIspisa.SelectedDate;
no need here to use the ?? in this line !

ConvertEmptyStringToNull=”false” and yet the conversion still happens

DetailsView is bound to ObjectDataSource. Inside Detailsview’s EditItemTemplate are two TextBoxes ( T1 and T2 ). T1 is mapped to update parameter of type String, while T2 is mapped to update parameter of type DateTime.
Assuming both TextBoxes contain an empty string, then when I try to update the data source by clicking on DetailsView’s Update button, ODS ( or is it perhaps DetailsView ) automatically converts T1’s empty string to null, while T2’s empty string doesn’t get converted to null. I’ve tried to prevent ODS from converting T1’s empty string to null by setting T1’s update parameter’s ConvertEmptyStringToNull property to false ( I ‘ve also set <asp:TemplateField ConvertEmptyStringToNull=”false” …>, but to no effect.
a)Any idea why T1’s empty string gets converted, while T2’s doesn’t?
b) Also, how can I prevent the conversion( BTW - I realize I could convert null back to empty string inside update method )?
thanx
a)Any idea why T1’s empty string gets
converted, while T2’s doesn’t?
T2 is a DateTime which is a value type. Value types can't be null. Well unless you use the Nullable type
b) Also, how can I prevent the
conversion( BTW - I realize I could
convert null back to empty string
inside update method )?
EDIT: I've tried to duplicate the problem above, but I could only duplicate the problem when I didn't specify ConvertEmptyStringToNull="false" in the <asp:TemplateField> of the bound control AND the <asp:Parameter> of the <asp:ObjectDataSource>. If you leave either out then you will get the null value on an empty field. With the ConvertEmptyStringToNull="false" defined in both places it does not convert the empty string to a null value. The empty string is passed correctly. You said that you did try it in both places, so I'm not sure why it's not working for you. Maybe you could show us your datasource and detailsview markup.
With that said I think it is still a good idea to make the check described below in your business class. Like you said you can convert null back to an empty string. This is how I have done it:
I have a helper class, lets call it BizObject, that contains this method:
protected static string ConvertNullToEmptyString(string input)
{
return (input == null ? "" : input);
}
Then in my business class's Insert/Update method I call ConvertNullToEmptyString on each string parameter:
public static bool UpdateSource(string sourceName, DateTime sourceDate)
{
sourceName = BizObject.ConvertNullToEmptyString(sourceName);
...
bool ret = UpdateSource(record);
return ret;
}

What's the difference between String(value) and value as String?

Just to make this clear - what is the difference between:
String(value)
and
value as String
What are the cases where you would use one over the other? They seem interchangeable...
Casting with Type(variable) can cause a runtime exeception (RTE), while "variable as type" will return null instead of throwing an exception.
See http://raghuonflex.wordpress.com/2007/07/27/casting-vs-the-as-operator/ for more explanations.
String (value) creates a new String object from a string literal. If the constructor argument is not a string literal, I assume it calls the argument object's .toString() method.
value as String will simply pass back value IF value is a String or a subclass of String. It will pass back null if value is not of type String.
The important thing to note is that String(val) creates a new object whereas value as String simply refers to value (and tests for compatibility to String).
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/String.html
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/operators.html#as

Resources