I would like to validate a number to be between 10 and 20, or 30 and 40 or equal to 99.
I could imagine the code to be something similar to this, but it does not work:
In Entity\NameOfFile.php:
/*
* #Assert\Range(
* min=10,
* max=20
* )
*
* #Assert\Range(
* min=30,
* max=40
* )
*
* #Assert\IdenticalTo(value = 99)
*/
private $myVariable;
Or maybe something similar to this:
/*
* #Assert\Choice({
* Range({min = 10, max = 20}),
* Range({min = 10, max = 20}),
* 99
* )}
*/
private $myVariable;
I also added min and max messages.
In the first option, apparently only the first Assert is taken into consideration and the second one is ignored. The second option, does not work at all.
Ps: Please, a solution without Regex
EDIT:
Following the advice of M Khalid Juanid, the code looks like this:
/**
* #Assert\Expression(
* "this.getmyVariable() in 10..20 or this.getmyVariable() in 30..40 or this.getmyVariable() == 99",
* message="Your error message", groups{"groupA"}
* )
*private $myVariable;
*/
***
if($appendForm->isValid(array($groupA))
{
***
}
It works fine, but only when the validation is not assigned to groupA. How can, the validation be assigned to a group, in this case?
You can use #Assert\Expression for your mulitple criteria
/**
* #Assert\Expression(
* "this.checkRange()",
* message="Your error message"
* )
*/
class YourEntityName
{
public function checkRange(){
if(
($this->yourPorperty >= 10 && $this->yourPorperty <= 20)
|| ($this->yourPorperty >= 30 && $this->yourPorperty <= 40)
|| ($this->yourPorperty == 90)
){
return true;
}
return false;
}
}
As per docs The expression option is the expression that must return true in order for validation to pass
Even more simpler according to the documentation
/**
* #Assert\Expression(
* "this.getYourPorperty() in 10..20 or this.getYourPorperty() in 30..40 or this.getYourPorperty() == 90",
* message="Your error message"
* )
*/
You can use a custom validation constraint. To create one you can refer to the official documentation: http://symfony.com/doc/3.4/validation/custom_constraint.html
You will have to put all your constraints in the validate function of the Validator class.
Since Symfony 5.1, there is a new validator that can check if at least one of several constraints is met.
AtLeastOneOf
/**
* #Assert\AtLeastOneOf({
* #Assert\Range(
* min=10,
* max=20
* ),
* #Assert\Range(
* min=30,
* max=40
* ),
* #Assert\IdenticalTo(value = 99)
* })
*/
private $myVariable;
Related
I'm trying to write some code to give me an output that looks like:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Given my limited r experience the closest I got was:
for(i in 1:5) {
print(strrep("*", i))
}
for(i in 4:1) {
print(strrep("*", i))
}
which gives me:
# *
# **
# ***
# ****
# *****
and
# ****
# ***
# **
# *
Any guidance would be really helpful.
Thanks in advance!
Pad extra space
for(i in c(1:5, 4:1))
cat( paste0(strrep(" ", 5 - i), strrep("* ", i), "\n") )
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
I basically have a number, say 100.
I can increase it by 10 percent every time. So, :
1st run it would become 110,
2nd 121,
3rd 133 and so on.
I can have count of how much the value was increased. But how to expotentialy decrease the amount knowing the number of times it has been increased back to 100?
Is there a way to do it with simple math op like ** instead of looping the current value amount of times it has been altered by 10 percents?
I know I can just store it in additionl column like base_number=100 or something when I need the basic value, but I would like to know if its possible to do by one-liner calculations?
So your basic question is, how do you invert and find x_0 given a known n and:
x_n = x_0 * 1.1^n
Looks like we can simply divide through by 1.1^n
x_n/(1.1^n) = x_0
So you can either calculate 1.1^n with pow(1.1, n) and divide x_n (your "increased" value) by that, or just loop and reduce like you increased:
//1.
$original = $increased/pow(1.1, n);
//2.
$original = $increased;
for ($i = 0; $i < n; $i++) {
$original = $original / 1.1;
}
So in your example, let's say our $increased is known to be 133, and n=3. Then using the first method:
$original = 133 / (1.1^3) = 133 / 1.33 = 100
Let's make a simple example and try to find a formula :
100 * 1.10 = 110;
110 * 1.10 = 121;
121 * 1.10 = 133.1;
So right now we have :
basic_number (will be bn) * increase_value (will be iv) = basic_number2;
bn2 * iv = bn3;
bn3 * iv = bn4;
We can write it too :
bn * iv = bn2;
bn * iv * iv = bn3;
bn * iv * iv * iv = bn4;
And so we have the beginning of a formula :
bn * iv^3 = bn4;
Now what you will have as data according to your post is :
X : the number of increase
bnX : the basic number increase X time
iv : the increase value
And you want to find bn according to those value :
bn * iv^X = bnX;
bn = bnX / iv^X;
bn = bnX / (iv * iv * iv ... * iv); // X time
So with PHP it could look like this :
$X = /* the number of incease */;
$bnX = /* the basic number increase X time */;
$iv = /* the increase value */;
for($i = 0; $i < $X; $i++) {
$bnX = $bnX / $iv;
}
This way you will if you echo $bnX; at the end of the loop, you should get your basic number !
You can try to make a php formula to do it and use it every time :
// 1/ Using a loop
function getBasicNumber($bnX, $X, $iv) {
for($i = 0; $i < $X; $i++) {
$bnX = $bnX / $iv;
}
return $bnX;
}
EDIT
// 2/ Using pow
function getBasicNumber($bnX, $X, $iv) {
return $bnX / pow($X, $iv);
}
// 3/ Using '**'
function getBasicNumber($bnX, $X, $iv) {
return $bnX / $X**$iv;
}
This way you just have to do :
echo getBasicNumber(133.1, 3, 1.10); // 100 here for example
Hope it's clear? But still, it's more a maths problem than a programming one
I run multiphaseInterfoam, and I have trouble with the inlet being non-constant (I want it to be constant.)
Here are my alpha-files\
/--------------------------------- C++
-----------------------------------\
FoamFile {
version 2.0;
format ascii;
class volScalarField;
location "0";
object alpha.air; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 0 0 0 0 0 0];
internalField uniform 0;
boundaryField {
//- Set patchGroups for constraint patches
#includeEtc "caseDicts/setConstraintTypes"
inlet
{
type alphaContactAngle;
thetaProperties
(
( freshWater air ) 90 0 0 0
( saltWater air ) 90 0 0 0
( freshWater saltWater ) 90 0 0 0
);
value uniform 0;
}
outlet
{
type alphaContactAngle;
thetaProperties
(
( freshWater air ) 90 0 0 0
( saltWater air ) 90 0 0 0
( freshWater saltWater ) 90 0 0 0
);
value uniform 0;
}
atmosphere
{
type inletOutlet;
inletValue uniform 1;
value uniform 1;
}
barge
{
type alphaContactAngle;
thetaProperties
(
( freshWater air ) 90 0 0 0
( saltWater air ) 90 0 0 0
( freshWater saltWater ) 90 0 0 0
);
value uniform 0;
} }
alpha.freshwater:
/--------------------------------- C++
-----------------------------------\
FoamFile {
version 2.0;
format ascii;
class volScalarField;
location "0";
object alpha.freshWater; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 0 0 0 0 0 0];
internalField uniform 0;
boundaryField {
//- Set patchGroups for constraint patches
#includeEtc "caseDicts/setConstraintTypes"
inlet
{
type fixedValue;
value $internalField;
}
outlet
{
type variableHeightFlowRate;
lowerBound 0;
upperBound 1;
value $internalField;
}
atmosphere
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
barge
{
type zeroGradient;
} }
alpha.saltWater
FoamFile {
version 2.0;
format ascii;
class volScalarField;
location "0";
object alpha.saltWater; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 0 0 0 0 0 0];
internalField uniform 0;
boundaryField {
//- Set patchGroups for constraint patches
#includeEtc "caseDicts/setConstraintTypes"
inlet
{
type fixedValue;
value $internalField;
}
outlet
{
type variableHeightFlowRate;
lowerBound 0;
upperBound 1;
value $internalField;
}
atmosphere
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
barge
{
type zeroGradient;
} }
//
************************************************************************* //
alphas
/--------------------------------- C++
-----------------------------------\
FoamFile {
version 2.0;
format ascii;
class volScalarField;
location "0";
object alphas; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 0 0 0 0 0 0];
internalField uniform 0;
boundaryField {
//- Set patchGroups for constraint patches
#includeEtc "caseDicts/setConstraintTypes"
inlet
{
type fixedValue;
value $internalField;
}
outlet
{
type variableHeightFlowRate;
lowerBound 0;
upperBound 1;
value $internalField;
}
atmosphere
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
barge
{
type zeroGradient;
} }
//
************************************************************************* //
The above gives the following, wanted, distribtion of fluids for timestep 1
enter image description here
However, after several time the above changes, also at the inlet:
enter image description here
I really don't understand the contactAngle functino used in alpha.air above. I have tried with the following alpha.air
/--------------------------------- C++
-----------------------------------\
FoamFile {
version 2.0;
format ascii;
class volScalarField;
location "0";
object alpha.air; } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
dimensions [0 0 0 0 0 0 0];
internalField uniform 0;
boundaryField {
//- Set patchGroups for constraint patches
#includeEtc "caseDicts/setConstraintTypes"
inlet
{
type fixedValue;
value $internalField;
}
outlet
{
type variableHeightFlowRate;
lowerBound 0;
upperBound 1;
value $internalField;
}
atmosphere
{
type inletOutlet;
inletValue $internalField;
value $internalField;
}
barge
{
type zeroGradient;
} }
//
************************************************************************* //
With the alpha.air above I get a long error message that includes stuff that is interpreted as internet links so that I am not allowed to pulblish them here. The error message can be seen on this link to the CDF-online forum, where I have also have asked this question.
Does anybvody know how to modify the above files to make the distribution of alpha's at the inlet constant?
In the last version of alpa.air I changed from "internalField uniform 0" to "internalField uniform 1", and now the inlet is constant. I think the internalFIeld value has to equal the default value set in setFieldsDict.
I'm just curious why row starts with 1 and column with 0. Is there any special reason or am I missing something important? Why not just both start with 0 or 1 (consistency).
/**
* Set a cell value by using numeric cell coordinates
*
* #param string $pColumn Numeric column coordinate of the cell (A = 0)
* #param string $pRow Numeric row coordinate of the cell
* #param mixed $pValue Value of the cell
* #param bool $returnCell Return the worksheet (false, default) or
the cell (true)
* #return PHPExcel_Worksheet|PHPExcel_Cell
Depending on the last parameter being specified
*/
public function setCellValueByColumnAndRow($pColumn = 0,
$pRow = 1,
$pValue = null,
$returnCell = false)
{
$cell = $this->getCellByColumnAndRow($pColumn, $pRow)->setValue($pValue);
return ($returnCell) ? $cell : $this;
}
The reason is backward compatibility with earlier versions after what was originally a bad decision based on the old PEAR SEW that also had zero-based column IDs
I have a percentage field in my form and I want it have values between 0 - 100 as number. How can I force it and return validation otherwise?
Currently I can even enter 100.10 which is wrong.
Note: 0.00 and 100.00 is allowed
Thanks
ENTITY:
/**
* #var decimal
* #ORM\Column(type="decimal", precision=5, scale=2)
*/
protected $vatRate;
FORM TYPE:
->add(
'vatRate',
'percent',
array('type' => 'integer', 'attr' => array('min' => '0', 'max' =>` '100'))
)
Add a constraint to the entity
use Symfony\Component\Validator\Constraints as Assert;
/* ... */
/**
* #var decimal
* #ORM\Column(type="decimal", precision=5, scale=2)
* #Assert\Range(
* min = 0,
* max = 100,
* minMessage = "Min % is 0",
* maxMessage = "Max % is 100"
* )
*/
protected $vatRate;
Use Range constraint in your entity:
http://symfony.com/doc/current/reference/constraints/Range.html
use Symfony\Component\Validator\Constraints as Assert;
/**
* #var decimal
* #ORM\Column(type="decimal", precision=5, scale=2)
* #Assert\Range(
* min = 0,
* max = 100
* )
*/
protected $vatRate;