I want to setup my validation rules in codeigniter such as a field starts with character 'P' or 'S', other wise it is invalid. How can I do that using Codigniter validation library?
Test Case 1: input: A145874 ------- invalid Must start with P or S
Test Case 2: input: P258741 ------- valid
Test Case 3: input: P45KK91 ------- invalid Must not contain Letters in other positions rather the first one.
Test Case 4: input: S457821 ------- valid
You would need to write a custom validation rule. Something like this:
public function check_first_char($str) {
$first_char = substr($str, 0, 1);
if ($first_char != 'P' || $first_char != 'S') {
$this->form_validation->set_message('check_first_char', 'The %s field must begin with P or S!');
return FALSE;
} else {
return TRUE;
}
}
Then you would add that validation rule like this:
$this->form_validation->set_rules('field_name', 'Field Name', 'callback_check_first_char');
The documentation explains it all pretty clearly.
Related
I have some very large rptdesign report definition files.
I would like to do something like in the example below:
<expression name="expression">dataSetRow["WORK_DESCRIPTION"].replace(new RegExp('<', 'g'), '<');</expression>
But for any occurrence of string in any dataset in any cell in any row.
Is this possible to do in rptdesign?
Or is there other way to accomplish this task?
One way you could do this is to create a style (use predefined data style) and add a map to it. Put a script in first expression like:
importPackage(Packages.java.lang);
if( _jsContext.getContent().getValue() instanceof String ){
if( _jsContext.getContent().getValue() == "S18_1749" ){
_jsContext.getContent().setValue(_jsContext.getContent().getValue()+"--");
}
}
true;
This will always return true. Set the second expression to false, so the map never occurs. It is a bit ugl
1. Overall Task:
I want to customize the Java.stg to modify the token display format in the comment of the code block for an alternative of grammar rule.
2. Context:
one rule in my current grammar is:
temporal returns [String ret]:
NEXT disj
{$ret= $ret= "X ".concat($disj.ret);}
| EVENTUALLY disj
{$ret= "F ".concat($disj.ret);}`
;
The corresponding generated code block (in parser) is as follows:
switch (alt2) {
case 1 :
// RERS.g:26:7: NEXT disj
{
match(input,NEXT,FOLLOW_NEXT_in_temporal204);
pushFollow(FOLLOW_disj_in_temporal206);
disj2=disj();
state._fsp--;
ret = ret = "X ".concat(disj2);
}
break;
case 2 :
// RERS.g:28:7: EVENTUALLY disj
{
match(input,EVENTUALLY,FOLLOW_EVENTUALLY_in_temporal222);
pushFollow(FOLLOW_disj_in_temporal224);
disj3=disj();
state._fsp--;
ret = "F ".concat(disj3);
}
break;
3. My Goal:
Change the comment from format like // RERS.g:26:7: NEXT disj to NEXT_disj, i.e., from <fileName>:<description> to <MyOwnAttribute>
4. Attempt so far:
I tried to modify the template "alt(elements,altNum,description,autoAST,outerAlt,treeLevel,rew)" as follows:
alt(elements,altNum,description,autoAST,outerAlt,treeLevel,rew) ::= <<
/* <elements:ExtractToken()> */
{
<#declarations()>
<elements:element()> // as I understand, it's just an template expansion to apply the sub templates in each elements
<rew>
<#cleanup()>
}
>>
I checked that in this context, the value of attribute elements is something like {el=/tokenRef(), line=26, pos=7}{el=/ruleRef(), line=26, pos=12}{el=/execAction(), line=27, pos=7}.
I think I should "overload" the "tokenRef" template to spit out tokens formatted like "NEXT_disj"
5. Questions:
How to "overload" an existing template? I want to do that because I will have to modify the value of "elements" otherwise.
How can I only apply a template to a specific element in attribute "elements", instead of applying it to every element (like what template "element()" does)?
I think there should be some convenient way to achieve my goal. Any suggestion?
Thanks in advance.
E.g. I have this line in my language form_validation file:
$lang['min_length'] = "%s must be at least %s characters in length.";
And I want the output to be like this:
$lang['min_length'] = "This field must be at least %s characters in length.";
However, the problem is that it looks like this when echoed:
This field must be at least Your name characters in length.
which is wrong because it takes the first %s instead of the second %s.
How can I force CI to take the second %s? Is it possible?
Since the min_length is a standard function in the Form Validation Library and treated through this library, you can only do so by changing the core library.
But there is an easier way - using a callback function with the Form Validation Library:
$this->form_validation->set_rules('your_field', 'The Field label', 'callback_my_min_length[10]');
Then within your controller, add this:
public function username_check($str, $min_length)
{
if ( mb_strlen($str) >= $min_length)
{
return TRUE;
}
else
{
$this->form_validation->set_message('my_min_length', 'This field must be at least '.$min_length.' characters in length.');
return FALSE;
}
}
This is how I would do it.
In CodeIgniter, how do i validate phone numbers containing '+' and '-' symbols?
You cannot enter a number with "-" since you defined integer as the validation rule. Therefore, the validation will fail. You need to work with RegEx so that you can create more complex validation. See the topic on validations in the CI manual for more info.
Your validation rule:
$this->form_validation->set_rules('foo', 'Number', 'callback_number_validation');
Note how the keyword callback_ is used to identify CI your function for validation.
Your callback function:
//$str will be the value you want to verify
function number_validation($str) {
return preg_match("your_regex", $str) ? true: false;
}
I'm using T4 to generate some class definitions and find that I'm getting an underscore in front of my field names.
I have set
code.CamelCaseFields = true;
just to be safe (even though I understand that's the default) but still end up with _myField rather than myField.
How can I generate a field name without the '_' character?
Also, where is the documentation for T4? I'm finding plenty of resources such as
Code Generation and Text Templates and numerous blogs, but I have not found the class-by-class, property-by-property documentation.
You're probably talking about EF4 Self Tracking Entities. The CodeGenerationTools class is included via the <## include file="EF.Utility.CS.ttinclude"#> directive, which you can find at "[VSInstallDir]\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\EF.Utility.CS.ttinclude".
The FieldName function is defined as such:
private string FieldName(string name)
{
if (CamelCaseFields)
{
return "_" + CamelCase(name);
}
else
{
return "_" + name;
}
}
The "_" is hardcoded in the function. Coding your own shouldn't be difficult. Note that the CodeGenerationTools class is specific to this ttinclude file and isn't a generic and embedded way to generate code in T4.
I've written the following method to make first character upper case, remove spaces/underscores and make next character upper case. See samples below. Feel free to use.
private string CodeName(string name)
{
name = name.ToLowerInvariant();
string result = name;
bool upperCase = false;
result = string.Empty;
for (int i = 0; i < name.Length; i++)
{
if (name[i] == ' ' || name[i] == '_')
{
upperCase = true;
}
else
{
if (i == 0 || upperCase)
{
result += name[i].ToString().ToUpperInvariant();
upperCase = false;
}
else
{
result += name[i];
}
}
}
return result;
}
input/output samples:
first_name = FirstName,
id = Id,
status message = StatusMessage
This is good advice however it doesn't help you in knowing WHERE the right place to put such a function is...
Is there any guidance on DECOMPOSING the EF .tt files or stepping through the output generation to see how it builds the output?
I was able to use the above function successfully by plugging it into a function called
(Ef4.3)
public string Property(EdmProperty edmProperty)
Which appears to be used to output the lines like "public int fieldname { get; set; }"
and changed the 3rd (index {2}) param to the formating to wrap with the function to modify the name, like this:
_typeMapper.GetTypeName(edmProperty.TypeUsage), //unchanged
UnderScoreToPascalCase(_code.Escape(edmProperty)), //wrapped "name"
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)), // unchanged
This is not perfect, eg: it doesn't keep existing "Ucasing" and doesn't care about things like this:
customerIP
outputs: Customerip
which IMO is not very readable...
but its better than what I WAS looking at which was a nightmare because the database was intermingled mess of camelCase, PascalCase and underscore separation, so pretty horrific.
anyway hope this helps someone...