I have a pb with my form.
here is the definition of my property
/**
* #var integer $nbpointsTe
* #Assert\Type(type="integer")
* #Assert\Range(min=0)
*
* #ORM\Column(name="NBPOINTS_TE", type="integer", nullable=true)
*/
private $nbpointsTe;
here is the way I create the field in form
$builder->add('nbPointsTe', null, array(
'label' => 'Nombre de points',
'required' => false,
));
My problem is :
The error(s) of this field appear in my form and not next to my field.
I try to add 'error_bubbling' => false, but it doesn't change anything and the error keep going in the parent form. In addition this field doesn't display as a 'number' field automaticly (I know I could specify it in my builder if I replace null with 'number' but every other fields are correctly displayed without specifying that :/ )
The thing i don't understand is that I do the exact same things for others field and they display their errors without problem.
So if anybody sees something strange, or has a solution, feel free to comment :)
Thanks
Edit :
As asked my twig template: (nothing special here ^^ )
{{ form_row(form.nbPointsTe) }}
I tried 'error_bubbling' => false, errors still display in my parent form :/
ok, so I found what was the pb.
Symfony is case sensitive, so I added 'nbPointsTe' in my form and my class member was 'nbpointsTe' (Missing one Uppercase), and so nothing works. I just change one letter, and now everything is ok.
:)
Related
I'm not quite sure about the way of doing.
The challenge:
I call an addAction which shows a form. The point of calling the addAction gives two routing parameters, say value1 and value 2 separated by an "-".
I need value 1 and value 2 to search a pk in a table which will be saved as a foreignkey value by the addAction. I take both values give it to a method and get the key I need, that is tested and ok so far.
My problem.
In the first call of addAction I get the routing parameters and find the key. But afterwards of course it is forgotten. How can I remember the found value, so that I can use it for my saveModel method?
What would be the best way?
Idea 1:
Can I give it to the form and set it as value to the hidden keyfield?
For example:
class PadForm extends Form
{
public function __construct($name = null, $unitpartid)
{
parent::__construct('pad');
$this->add([
'name' => 'UnitPartPadID',
'type' => 'hidden',
'value' => $unitpartid,
]);
Would this be working? And would this be an accepted, proper way?
Idea 2:
Or would I try to use an instance variable in my controllerclass, like $this->smtg; ?
In both cases I get an understandable error
Trying to get property of non-object
Questions:
what would be the best way?
and
how to do it, perhaps somebody could give a short example.
EDIT:
I really would appreciate to learn about the best way. I now tried to give a variable to my form and then fill in some field, but that doesn't work.
Part of Controlleraction
(the action works if I set a constant value for the related field)
$parameter = $this->params()->fromRoute('id');
// echo $parameter;
$partnumber =substr($parameter,0,strpos($parameter,"-"));
// echo $partnumber;
$unitid=substr($parameter, strpos($parameter,"-")+1, strlen($parameter));
// echo $unitid;
$test=$this->unitparttable->getUnitPartID($unitid, $partnumber);
echo $test->UnitPartID;
$form = new PadForm(NULL, $test->UnitPartID);
Then in the Formclass:
public function __construct($name = null, $unitpartid)
{
// We will ignore the name provided to the constructor
parent::__construct('pad');
// $this->db=$db;
$this->add([
'name' => 'UnitPartPadID',
'type' => 'hidden', //hidden
]);
$this->add([
'name' => 'UnitPartID',
'type' => 'text', //hidden
'value' => $unitpartid,
]);
The question is now, how to fill the formfield UnitPartID with the value of $unitpartid given within the constructor.
I also tried $form->populate but it is unknown, I used it in ZEND1 before, but probably it doesn't exist anymore.
any help appreciated!
Could anyone tell me why error related to form collection is displaying outside the particular field and how to move it to place like you see in image included below?
Code of this field:
/**
* #Assert\Valid
* #ORM\OneToMany(
* targetEntity="PageFile",
* mappedBy="page",
* cascade={"persist","remove"},
* orphanRemoval=true
* )
* #var PageFile[]
* #Assert\Count(max="1")
*/
private $pageFiles;
Config:
- property: 'pageFiles'
type: 'collection'
type_options:
entry_type: 'Notimeo\PageBundle\Form\Type\MyFileType'
by_reference: false
error_bubbling: false
I'm using EasyAdminBundle and here's my whole project: https://github.com/ktrzos/SymfonyBasic. Problem applies to "Notimeo\PageBundle".
I see other errors are places above the input fields, so unless this is somehow positioned using CSS (which is very unlikely) it looks like the error is related to the form itself and not the input field. That's the same type of error like invalid CSRF token for example.
Your issue is probably related to Form Collection error bubbling where poster asks basically the same question as you.
The recommendation is to set:
cascade_validation' => true
Or, if you are using Symfony 3:
error_bubbling => false
I've got a problem with an extbase extension I wrote some months ago:
Normaly everything works fine, but at some point, the system updates ALL DateTime info of a certain field back to the 1.1.1970, in everey entry of the table.
There is a model for courses. Those contain three DateTime fields. The date, the begin and the end of the course. The Problem happens only in the date field. In the model, it is defined as so:
/**
* date
*
* #var \DateTime
*/
protected $date = NULL;
**
* Returns the date
*
* #return \DateTime $date
*/
public function getDate() {
return $this->date;
}
/**
* Sets the date
*
* #param \DateTime $date
* #return void
*/
public function setDate(\DateTime $date) {
$this->date = $date;
}
In the TCA, the definition is:
'date' => array(
'exclude' => 1,
'label' => 'shortend for example',
'config' => array(
'type' => 'input',
'size' => 7,
'eval' => 'date',
'checkbox' => 1,
'default' => time()
),
),
In the database (MySQL), the field is set to INT(11).
There is no cron job active
I don't set the date anywhere manual (only over the extbase dependency injection mechanism)
I don't know why this happens or by what it's fired. It happend three times since the launch of the page in november.
A furthermore strange thing: Evendo all 'date'-values are updated and show now a datevalue for the 01.01.1970, the values are different (those are integers) and display exponentiations of 2 (starting at 128) or combinations of those (e.q. 1'792 (1024 + 512 + 256))
The only thing I can reproduce:
When an courseitem is moved from one system folder to another, the date for this item (but not for all, like in the other error) drops to the 1.1.1970
I would be very glad if someone could help me, even with a hint or a suspicion, what could be wrong or why that could happen. The feeling of a ticking timebomb, everytime after import another backup, isn't that shiny at all ;)
Thanks in advance
xan
I think your last remark points to the solution:
A furthermore strange thing: Evendo all 'date'-values are updated and show now a datevalue for the 01.01.1970, the values are different (those are integers) and display exponentiations of 2 (starting at 128) or combinations of those (e.q. 1'792 (1024 + 512 + 256))
I suspect that in the ctrl section of your table setup, you set the date field as the sorting column. Therefore, when moving a record, TYPO3 will change the corresponding value to ensure a correct ordering of records e.g. in the backend lists or when selecting records for frontend output. To ease inserting a record somewhere (and not having to reorder everything then), TYPO3 by default uses multiples of two for that.
I'd to add it as an comment, but I'm not allowed to (because of my reputation).
I expect the issue with the DateTime object from php and how it is stored within the repository. The mysql field is an int. DateTime from php is no integer, but an object. Maybe you need to convert the DateTime to a timestamp (= integer) before saving it to the database.
I have tried creating a validation for a field which stores unit price of a product, I come across validation showing how to check for integer, but I can't found one for floating point number something with a format like 2032.95 in YII2. Thanks in advance.
*
After Abilay instructions I tried
[['quantity','round_off'],'number','numberPattern'=>'[-+]?[0-9]*.[0-9]+|[0-9]+'],
but it shows error in console.
I think, redefining of numberPattern of NumberValidator class will help in your case.
If you use AJAX Validator on your form:
In Model:
[['quantity','round_off'], 'number',
'numberPattern' => '/^\d+(.\d{1,2})?$/'],
In View:
Ensure that you enabled AJAX Validation when created form
$form = ActiveForm::begin([
'id' => 'my-form',
'enableAjaxValidation' => true,
]);
and change field to default input
$form->field($model, 'quantity')
In Controller:
Include files:
use yii\web\Response;
use yii\widgets\ActiveForm;
Past this code at the beginning of action, after
$model is loaded or created
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
I have a form that triggers on event in __construct method to load some items from another modules . So far so good , a field set is loaded from the other module and added to the form and in the request->getPost() I have the data for the elements inside the fieldset , but the $form->getData() doesn't have the data for the fieldset.
I am calling $form->getInputFilter() before adding this fieldsets to the form and it seems that calling the $form->getInputFilter() dosn't creates the filters for the newly added elements . so how can i create inputfilters for the dynamic events without recreating the hole filters again ?
Or should i just delay calling $form->getInputFilter() untill all of the elemnts have been added to the form ?
I also added some elements to the form later what was ignored by the input filter.
My solution is most likely not exactly the best one, but as you haven't received any other answers yet, here's what I did:
I added
use Zend\InputFilter\Factory as InputFactory;
in the class where I'm validating the form data and then used
$factory = new InputFactory();
$form->getInputFilter()->add($factory->createInput(array(
'name' => 'title_str',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
)));
#Afterdark017 that works and also i think it is possible to reset the filters.
protected function resetFilters(){
$this->filter = null;
$this->hasAddedInputFilterDefaults = false;
}
but i have not tested this yet.