Laravel 5.1: Null field being converted to blank in the function create - laravel-5.1

The Model.php has the following function:
public static function create(array $attributes = [])
{
$model = new static($attributes);
$model->save();
return $model;
}
The attributes parameter has null value for each field when they are not filled by the user. However, after call new static($attributes) one field is converted to "". I do not understand. Even when all fields are null, only one field is converted to "" (blank).
I do not want this convertion to blank. Anyone knows what is happening in this case?
Thanks.
Best regards.

I found the problem: In my setPhoneAttribute function (located in my Model class) I was calling a function to remove the format of the phone. So, as the phone field was null, after my removeFormat function the field is converted to "". Problem solved"!

Related

How do you get strip RTF formatting and get actual string value using DXL in DOORS?

I am trying to get the values in "ID" column of DOORS and I am currently doing this
string ostr=richtext_identifier(o)
When I try to print ostr, in some modules I get just the ID(which is what I want). But in other modules I will get values like "{\rtf1\ansi\ansicpg1256\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}{\f1\froman\fcharset0 Times New Roman;}} {*\generator Riched20 10.0.17134}\viewkind4\uc1 \pard\f0\fs20\lang1033 SS_\f1\fs24 100\par } " This is the RTF value and I am wondering what the best way is to strip this formatting and get just the value.
Perhaps there is another way to go about this that I am not thinking of as well. Any help would be appreciated.
So the ID column of DOORS is actually a composite- DOORS builds it out of the Module level attribute 'Prefix' and the Object level attribute 'Absolute Number'.
If you wish to grab this value in the future, I would do the following (using your variables)
string ostr = ( module ( o ) )."Prefix" o."Absolute Number" ""
This is opposed to the following, which (despite seeming to be a valid attribute in the insert column dialog) WILL NOT WORK.
string ostr = o."Object Identifier" ""
Hope this helps!
Comment response: You should not need the module name for the code to work. I tested the following successfully on DOORS 9.6.1.10:
Object o = current
string ostr = ( module ( o ) )."Prefix" o."Absolute Number" ""
print ostr
Another solution is to use the identifier function, which takes an Object as input parameter, and returns the identifier as a plain (not RTF) string:
Declaration
string identifier(Object o)
Operation
Returns the identifier, which is a combination of absolute number and module prefix, of object o as a string.
The optimal solution somewhat depends on your underlying requirement for retrieving the object ID.

xamarin android Intent.getstringextra multiple variables

i created 4 variables namely, "id", "name", "address", "age"
why does the value duplicates whenever i try intent.putextra
Context context = view.Context;
Intent intent = new Intent(context, typeof(activity4));
intent.PutExtra(activity4.id, joblist[position].id);
intent.PutExtra(activity4.name, joblist[position].name);
intent.PutExtra(activity4.address, joblist[position].address);
intent.PutExtra(activity4.age, joblist[position].age);
now the problem is when I do this.
string userId= Intent.GetStringExtra(id);
string userName= Intent.GetStringExtra(name);
string userAddress= Intent.GetStringExtra(address);
string userAge= Intent.GetStringExtra(age);
when I put those strings in a textview, all four textviews would show the value for "age". as if all the data that was passed is only age. can anyone answer this? the output is like this
id= 12
name= 12
address= 12
age= 12
Intent.Extras don't work like that when you use the typeOf() keyword
typeOf is a C# keyword that is used when you have the name of the class. It is calculated at compile time and thus cannot be used on an instance, which is created at runtime. GetType is a method of the object class that can be used on an instance
Since it does not initialize like that your variables that you are assigning here are currently all null so the intent carries all the data in correspondence to null, Since the first value you enter with null is id you always get id
I would suggest you save the strings in either resource strings and reuse it or do something like this :
First Activity:
Intent intent = new Intent(_context, typeOf(SecondActivity));
intent.PutExtra("Variable Name",string_to_be_sent);
startActivity(intent);
Second Activity:
//Receiving data inside onCreate() method of Second Activity
String value =Intent.GetStringExtra("Variable Name");

grails and double values saving

In my Grails project I have a Domain Class with a double field as follows:
double totalAmount;
the value of this field are calculated by a sum done after selecting values in a multiple select. the function for sum values is in the controller, as follows:
def sumReceiptItems(){
params.list("receiptItemsSelected").each {i-> println("element "+i)}
def appList = params.list("receiptItemsSelected")
List<ReceiptItem> allSelectedIds = ReceiptItem.findAllByIdInList(params.receiptItemsSelected.split(',')*.toLong())
def totalAmount = allSelectedIds.amount.sum()
println("totalAmount is = "+totalAmount)
render totalAmount as Double
}
the function works well. After function calling, to update the field in GSP page, I use a javascript method as follows:
function updateTotalAmount(name, data, presentNullAsThis){
if(data !=null)
document.getElementById(name).value= data;
else
document.getElementById(name).value=presentNullAsThis;
}
The javascript works and I see the updating of the field at runtime, but the double value is shown with a dot, and not with comma to separate decimal values. Infact, after clicking by save button to save the instance of the domain class, the value is saved without separating decimals, for example:
if the value into the fiels is 10.50 it is stored as 1050
In this discussion how can save a double type correctly in grails? I've read a similar problem, but solution is not good for my issue.
Anybody can help me?
Values with decimal separator depends on the current Locale of the user. Normally you use g.formatNumber in the view to display correctly the value.
You can check this topic on how to discover the decimal separator for a Locale.
To get the user's Locale use:
Locale locale = RequestContextUtils.getLocale(request)
I have solved the problem in this way:
I've updated my Javascript as follows:
function updateTotalAmount(name, data, presentNullAsThis)
{
var data2 = data.toString().replace(/\./g, ',');
if(data2 != null) document.getElementById(name).value= data2;
else document.getElementById(name).value= presentNullAsThis;
}
I've removed "type="number"" in the gsp related field

Override Grails Error Messages to format Dates and Numbers

I have created a domain with a Double field. When the validation occurs it throws the error message with size value showing the number with commas. Following are the detials
Groovy Class
class Quote {
String content;
Double size;
static constraints = {
content(maxSize:1000, blank:false)
size(min: 0.00D, max:999.99D)
}
}
Value entered "11111", error obtained "Size 11,111 is exceeded the limit". I have added the property key/value pair in messages.properties.
Here, I would like to get the message back without the commas. My main aim is to take the key and format the message returned based on my requirements. I require this as I have other fields that need conversion. For example, a date is validated but when showing the error the Gregorian date needs to be converted to an Islamic date and shown to user.
Does anyone know if I can do something to make this work.
I have tried the solution provided in http://ishanf.tumblr.com/post/434379583/custom-property-editor-for-grails but this did not work.
I have also tried modifying the messages values, but this is not flexible in case of my date issue. Example, for a key value pair, instead of using {2} as a place holder I could use {2, date, mm/dd/yyyy}, but for Islamic dates I want to format and show things differently.
Also, please note I have created a separate key for default date formatting for my application.
Would appreciate the help.
In grails, the return of a constrain is an already translated string.
You can create a taglib to format that, or enhance the
Another option would be custom validators. A custom validator can return false or a key when failing.
For example in your domain class, to vaildate a field:
myDateField validator: {val, obj -> obj.myShinyDateValidatorMethod(val) }
private myShinyDateValidatorMethod() {
if (isNotValidDate(val) {
return [the_message_key, val.formatedAsYouWand]
}
}
and, in your properties file you have to have defined the key:
the_message_key=This date: {3} is not valid
The trick here is that in the return from the validator, first string is the key and the rest are parameters for that key, but grails already uses {0}, {1}, {2} placeholders for className, fieldName and value, and the first parameter that you pass will be used as {3} placeholder.
Hope this helps

how to check null value of Integer type field in ASP.NET MVC view?

I have integer type field in database which is having property "Not Null".
when i create a view & do a validation, if i left that field blank, it will consider it as 0 so i can not compare it with 0 because if someone insert a value 0 then it will be considered as error!
one another problem is that i am using Model error as described in the book "ASP.NET MVC 1.0" # Scott Gu blog. And I am checking the value in partial class of object (created by LINQ-To-SQL). i.e
public partial class Person
{
public bool IsValid
{
get { return (GetRuleViolations().Count() == 0); }
}
public IEnumerable<RuleViolation> GetRuleViolations()
{
if (String.IsNullOrEmpty(Name))
yield return new RuleViolation("Name is Required", "Name");
if (Age == 0)
yield return new RuleViolation("Age is Required", "Age");
yield break;
}
partial void OnValidate(ChangeAction action)
{
if (!IsValid)
throw new ApplicationException("Rule violations prevent saving");
}
}
There is also problem with range.
Like in database if i declared as smallint i.e. short in c#, now if i exceed that range then it gives error as "A Value is reguired".
so finally is there any best way for validation in ASP.NET MVC?
you can change your code with a nullable type?
Dim i As Nullable(Of Integer)
MsgBox(i.HasValue)
'result is false
i = 1
MsgBox(i.HasValue)
'result is true
i = Nothing
MsgBox(i.HasValue)
'result is false
I'm not realy up to speed with the details of MVC, but isn't there a way to change the default value of age to -1? This way you have a value to check on and people can still enter 0.
Another solution could be to use a nullable int. You can do this by appending a ? to the type.
HTH.
Jonathan
I would validate the form on the client first. If the age is 0 then let the user know that it's not a valid age. On the server change the message, it's misleading:
if (Age == 0)
yield return new RuleViolation("0 years is not a valid age", "Age");
I know that because you bind to a model property from your Person entity it gets 0 as the default value - why don't you change that in the database then? Set a default in the database to a value that you consider reasonable for the user age, so that it no longer displays as 0 for new entities.
If you don't want to change the db values, use jQuery to set the textbox values to empty strings on your Create page when it loads. If the user tries to submit such a form you can check on the client to disallow it and on the server you don't have to do anything, the user will get a "A value is required" message back.
By the way, you wrote:
i can not compare it with 0 because if
someone insert a value 0 then it will
be considered as error
I don't see a problem in this particular domain model. Do you think that age of 0 is a good value or not? Ask yourself.
check it with Utilities.GetNullableInt32(textbox1.Text.Trim());
if u check with this GetNullableInt32() function if the user entered value '0' it will saved as NUll in your table

Resources