Validation in extbase using regex - extbase

I want to use validation of model properties in extbase via regex and using the following syntax:
/**
*
*#var string $telephone
*#validate RegularExpression('/^[0-9]+$/')
*/
$protected $telephone;
But I keep getting a validation error, irrespective of the value of $telephone variable.What am I doing wrong?

You could simply do something like
/**
*
* #var string $telephone
*
*/
$protected $telephone;
in this case you only get integer values to var telephone.
the other way is to add the right validation syntax
#validate $telephone notEmpty, regularExpression(regularExpression="/^[0-9]+$/")

Related

Symfony - Validation: The type of the attribute must be bool, string given

I have an entity and a column with type boolean
/**
* #ORM\Column(type="boolean", nullable=false)
* #Assert\NotBlank()
*/
private $isActive;
When I try to add a string to this column (just to test) I get this error message
The type of the attribute must be bool, string given
So, I add the validation type
* #Assert\Type(
* type="boolean",
* message="The value {{ value }} is not a valid {{ type }}."
* )
but always the message error launched, so, I try the second solution by creating my own Asset validation
if(false === is_bool($user->getIsActive())){
$this->context->buildViolation($constraint->message)->atPath('isActive')->addViolation();
}
but always the code crushed and the message appears.
PS: if I change the column type to string the validation work correctly, but I want to use the bool type with validation, is there any solution?
I fixed this issue by adding this line :
/**
* #ApiResource(
* denormalizationContext={
* "disable_type_enforcement"=true
* }
* )
*/

Strange Issue With Entity Relationship in Symfony 4

This is a very odd one, i'll try to explain with couple of sample entities.
class Property{
/**
* #var string
* #ORM\Column(type="string", length=10)
*/
private $postCode;
/**
* #var InstructionToSell
* #ORM\OneToOne(targetEntity="App\Entity\InstructionToSell")
* #ORM\JoinColumn(nullable=true)
*/
private $instructionToSell;
}
class InstructionToSell{
/**
* #var Property
* #ORM\OneToOne(targetEntity="App\Entity\Property")
* #ORM\JoinColumn(nullable=true)
*/
private $property;
}
So two sample entities, the property can have an instruction to sell entity and vice versa. I then have a very basic post code search method in the repo:
/**
* #param string $postCode
* #return array|null
*/
public function searchByPostcode(string $postCode) : ?array{
$builder = $this->createQueryBuilder('p');
$builder->where(
$builder->expr()->like('p.postCode',':postCode')
)->setParameter('postCode',str_replace(' ','','%'.$postCode.'%'));
return $builder->getQuery()->getResult();
}
It all works fine except for one very strange thing. If say a property had the Uk post code of "YO12 7YA" and we run a search for "YO127YA" then it's bringing back a result, but if we use "YO12 7YA" it's bringing back a result but the instructionToSell is null on the property, but it's not null if i remove the space from the search term.
I'm aware this search isn't the best as it stands, need to strip spaces out of the column as well, but the point i am making is, the same code runs for "YO12 7YA" and "YO127YA" which brings back the same property, but one of them has the relationship matched to the instruction to sell, the other it's null.
Why would this happen? It's exactly the same property it's bringing back.

Symfony Validation dependent on another property

Trying to validate if one field is not empty (length > 0) then the length of the field being validated must be a certain length (2 characters). It seems like an "Assert\Expression" might work in this situation but I am having trouble trying to find the length of the properties. It seems like you cannot call php functions within the Expression. The expression documentation mentions functions but maybe I do not understand it... Do I need to register my own function that simply return a strlen(). If so how do you register your own functions? Can someone explain if there is a way to do this, or maybe there is a better way than using Expression that I am overlooking...
/**
* #var string
*
* #ORM\Column(name="plate", type="string", length=10)
*/
private $plate;
/**
* #var string
*
* #ORM\Column(name="state", type="string", length=2)
* #Assert\Expression(
* "strlen(this.getPlate()) == 0 or (strlen(this.getPlate()) > 0 and strlen(value) == 2)",
* message="Must be 2 characters"
* )
*/
private $state;
In the above case I get an error The function "strlen" does not exist around position 1
Looks like you will need to register your own function. Have a look at the docs: https://symfony.com/doc/current/components/expression_language/extending.html#registering-functions
There is an example on lowercase, strlen should be very similar.
EDIT:
You can also use a callback validator.
/**
* #Assert\Callback()
*/
public function validateState(ExecutionContextInterface $context)
{
if (!empty($this->plate) && mb_strlen($this->state) !== 2) {
$context->buildViolation('State must be 2 characters long')
->atPath('state')
->addViolation();
}
}
But if you are planning to using this kind of validation in multiple places, you can write and register your own validator.

symfony2 set request parameter to custom annotation

I want use request parameter in my annotation in Symfony2
I want set $id (it is parameter from request) to MyAnnotation, as it described below
/**
* #MyAnnotation($id)
* #Route('something/{id}')
*
*/
When I set $id in this way, I have an error. How to pass this parameter to annotation?
Other way is:
/**
* #MyAnnotation("id")
* #Route('something/{id}')
*
*/
And I can get value of "id" parameter in annotation class, in constructor:
public function __construct($options)
{
// get key
$key = $options['value'];
// get value of key
$id = $request->get($key);
}
But I dont know its possible to set $id in annotation and dont write code in constructor.

createFormBuilder entity type dont set curent value

i setup form createFormBuilder
$form = $this->createFormBuilder($ob)
->add('mkeywordsId', 'entity',['label'=>'rodzaj','class' => 'Miejsce\ObiektyBundle\Entity\Mkeywords'])
and this select dont have curent option selected, how to do this?
when i put ->add('mkeywordsId', 'text') i see text witch 1445 value , and this key exists in Mkeywords select list so why is not selected ?
I have only mapping from Mmiejsce, mkeywords dont have mappings because is not only one entity use keywords. In this situation Mmiejsce have connection one Mmiejsce to one Mkeyword.
Maybe i can map in createFormBuilder->add ?
Mmiejsce.mkeywordsId = Mkeywords.id and want get Mkeywords.name
class Mmiejsce
/**
* Rodzaj
* #ORM\ManyToOne(targetEntity="Mkeywords")
* #ORM\JoinColumn(name="keyword_id", referencedColumnName="id")
* #var integer
*/
private $mkeywordsId;
}
class Mkeywords
{
/**
* #var string
*/
private $name;
/**
* #var integer
*/
private $mkeywordsId;
Sounds like the relationships between your entities are not mapped correctly. Can you post the contents/schema of the entity class for this form?

Resources