Currently i'm working with PHP 5.6.2 in my laptop and the server has 5.5.0 both has configurated timezone to "America/Bogota"
the entity has a field like
/**
* #var \DateTime
* #Column(name="start_date", type="datetime")
* #NotNull()
*/
protected $startDate;
and the entity type is defined like
$builder
->add('startDate', 'date', array(
'input' => 'datetime',
'widget' => 'single_text',
));
the data passed is formatted like 2015-01-15T06:11:37-0500 in my laptop that date is correct but in the server is invalid
I don't know why you are facing this problem, but I would advice you to specify the date format in the form like follows:
Also change date to datetime.
$builder
->add('startDate', 'datetime', array(
'input' => 'datetime',
'widget' => 'single_text',
'format' => 'yyyy-MM-ddThh:ii:ss'
));
Hope this helps.
Related
I am working on php 7.4.30, symfony 4.4.39, sonata-project/admin-bundle 3.98.2
I have a form, with some "required" fields. Some fields are required by default
$form
->tab('Main')
->with('Main', ['class' => 'col-md-8'])
->add('name', null, ['label' => 'Event name'])
->add('title', null, ['label' => 'Title'])
->add('startDatetime', DateTimePickerType::class, ['label' => 'Start date'])
->add('endDatetime', DateTimePickerType::class, ['label' => 'End date'])
->add('language', null, ['label' => 'Language'])
->add('description', SimpleFormatterType::class, [
'label' => 'Description',
'required' => true,
'format' => 'richhtml',
'ckeditor_context' => 'short',
'ckeditor_image_format' => 'material',
'attr' => [
'style' => 'height: 400px'
]
])
->add('place', ModelListType::class, ['label' => 'Place', 'help' => 'Event place'])
->add('link', null, ['label' => 'Ссылка', 'help' => 'Page link'])
...
Entity:
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=false)
*/
private string $description;
/**
* #var Place
*
* #ORM\ManyToOne(targetEntity="Place")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="place_id", referencedColumnName="id", nullable=false)
* })
*/
private Place $place;
If form fields are empty, then when "Submit" button is submitted, frontend validation is not working for fields 'description' and 'place'. For other empty fields ('name','title','startDatetime','endDatetime','link') frontend validation is working good and tooltips are appearing on required form fields after click on "Submit" button
So after wrong submitting with empty field, there are server Exceptions appearing, such as:
When attempt to edit item:
TypeError InvalidArgumentException
Expected argument of type "string", "null" given at property path "description".
TypeError InvalidArgumentException
Expected argument of type "App\Entity\Place", "null" given at property path "place".
When attempt to creating new item:
PDOException Exception NotNullConstraintViolationException ModelManagerException
Failed to create object: App\Entity\Event
I know if I change entities properties with null type default - error will not throw. And then it possible to check empty fields on server after submitting. But I need exactly frontend validation. Is it possible?
Expected:
Frontend validation is working good for fields 'description' and 'place', and tooltips are appearing for these required form fields after click on "Submit" button
I have a classic form, with a few 'entity' type fields and one collection type fields. Those aren't causing any issue.
When I put data in all the field, except the description field, as I want it to be null or empty, and submit, my form is processed but the new entity not added to the database, as if the description field needed to be field.
Then I'm redirected to the same form with all data entered gone, as if it had been added in the database.
I've checked the field mapping, which is set to nullable :
/**
* #var string
*
* #ORM\Column(name="description_activite", type="text", nullable=true)
*/
private $descriptionActivite;
public function getDescriptionActivite(){return $this->descriptionActivite;}
public function setDescriptionActivite($value){$this->descriptionActivite=$value;return $this;}
And the field description in the formType file :
->add('descriptionActivite', 'textarea', array(
'label' => 'Description',
'attr' => array(
'class' => 'form-control',
// 'required' => false
)
))
I've also checked the database just in case, the field is created as a may be null field, I really don't know where that problem is coming from. Anyone ran into this already? Thanks
For those who meet the same problem, I solved it by :
Checking database if field may be null (was not the issue but would have been later)
Checking annotation, see if the field is set as nullable
AND LAST
->add('descriptionActivite', 'textarea', array(
'label' => 'Description',
'required' => false,
'attr' => array(
'class' => 'form-control',
)
))
the required option was place in the 'attr' array(), when it should not have, my bad.
I'm trying to add an entity field in a symfony2 form but it's always giving me the same error: '500 (Internal Server Error) '.
This is the class i'm using to create the form. It was automatically programmed with doctrine and CRUD.
class ClientType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('companyName', 'text' , array( 'attr' => array( 'class' => 'companyname' ) ) )
->add('contactUserName','text' , array( 'attr' => array( 'class' => 'contactusername' ) ))
->add('phone','text' , array( 'attr' => array( 'class' => 'phone' ) ))
->add('subdomain','text' , array( 'attr' => array( 'class' => 'subdomain' ) ))
->add('email','text' , array( 'attr' => array( 'class' => 'email' ) ))
->add('website','text' , array( 'attr' => array( 'class' => 'website' ) ))
;
}
This works fine, but then, i try something like this:
->add('client', 'entity', array(
'class' => 'BackendBundle:Client'));
'500 (Internal Server Error)'
I tried so many different ways to do this, but it's always the same error.
The thing is, i can add or remove the fields that were created at the beggining when this class was done by the doctrine CRUD but if i try adding more fields with different types, it won't let me.
Should i make my own Type class so i can customize my forms or is there a way to modify the form doctrine made?
TY
The thing is, i can add or remove the fields that were created at the
beggining when this class was done by the doctrine CRUD but if i try
adding more fields with different types, it won't let me.
This is because the command that created your ClientType.php did so, based upon the structure of your BackendBundle\Entity\Client.php file. The form is mapped to the entity that you intend to create. If you want more fields on the form, you will need to add the fields as properties in your BackendBundle\Entity\Client.php, then run:
php bin/console doctrine:generate:entities <VENDOR>/<BUNDLE>/Entity/Client
or if using Symfony 2 < version 2.5
php app/console doctrine:generate:entities <VENDOR>/<BUNDLE>/Entity/Client
To generate the getters and setters for that field, and then
php bin/console doctrine:schema:update --force
or if using Symfony 2 < version 2.5
php app/console doctrine:schema:update --force
To add the new field(s) to the database table.
Now you can try to add the field as you were, ensuring that the first argument in the add() method exactly matches how you names your property in the entity.
I have a user object with a birthday column:
/**
* Birthday
*
* #ORM\Column(type="date", nullable=true)
*/
protected $birthday;
In the registration form i add the birthday widget as followed:
->add('birthday', 'birthday')
But when I send the form, I get This value is not valid error for that field.
I've tried some alternatives, but none of them worked:
->add('birthday', 'date')
->add('birthday', 'date', array('input' => 'string'))
Does anyone have a solution?
Dumb mistake..!!
My timezone settings in php.ini was invalid which caused the date transformer to throw an exception
In you want to insert string as input into a date field, you should use the option : 'widget' => 'single_text'
$builder->add('date_created', 'date', array(
'widget' => 'single_text',
'format' => 'YYYY-MM-dd',
));
I'm currently trying to create a form with fields filled with data from my database by default.
I use:
$infos = $this->getDoctrine()
->getRepository('TestMyBundle:My')
->find($id);
$form = $this->createForm(new TestType(), $infos);`
But i always get this error message: Expected argument of type "Boolean", "string" given on Symfony2. the second parameter $infos is probably the reason of this message but it's the way to add some defaults values in a form field with Symfony2.
Can anyone help me please ?
Here is the code of my TestType.php class:
class TestType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('name');
$builder->add('description');
$builder->add('access', 'checkbox', array(
'label' => 'private access: ',
'required' => false,));
$builder->add('visibility', 'checkbox', array(
'label' => 'private group: ',
'required' => false,));
$builder->add('invitation', 'checkbox', array(
'label' => 'ask: ',
'required' => false));
$builder->add('wall', 'checkbox', array(
'label' => 'wall: ',
'required' => false,));
}
I was getting the same error after I reloaded my form with a checked value that was already persisted.
/**
* #var integer
*
* #ORM\Column(name="is_recibido", type="integer", nullable=true)
*/
private $isRecibido=null;
Then I changed it to
/**
* #var integer
*
* #ORM\Column(name="is_recibido", type="boolean", nullable=true)
*/
private $isRecibido=null;
The field in database is integer but doctrine understand and convert it to boolean to the form and an Integer when It goes to the database.
Now everything started to work smooth and nice