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
Related
I was trying to use the org.eclipse.core.databinding plugin to bind my TableViewer input change.
When I tried to add the binding by below code:
1. TableViewer tableViewer = new TableViewer(parent);
2. IViewerObservableValue<Target> target = ViewerProperties.input(TableViewer.class).observe(tableViewer);
3. UpdateValueStrategy<String, Target> updateValueStrategy = new UpdateValueStrategy<>();
updateValueStrategy.setConverter(...);
4. this.bindingContext.bindValue(target, source, new UpdateValueStrategy<>(UpdateValueStrategy.POLICY_NEVER),
updateValueStrategy);
But, at line number 2, I'm getting an compiler error that 'The method observe(Viewer) is ambiguous for the type IViewerValueProperty<Viewer,Object>' compiler error.
When I look into the source of IViewerObservableValue, There are 2 methods similar, I tried type casting with Viewer or Object for tableViewer variable passed, but, I'm still getting the error.
`/**
* Returns an {#link IViewerObservableValue} observing this value property
* on the given viewer
*
* #param viewer
* the source viewer
* #return an observable value observing this value property on the given
* viewer
*/
public IViewerObservableValue<T> observe(Viewer viewer);
/**
* This method is redeclared to trigger ambiguous method errors that are hidden
* by a suspected Eclipse compiler bug 536911. By triggering the bug in this way
* clients avoid a change of behavior when the bug is fixed. When the bug is
* fixed this redeclaration should be removed.
*/
#Override
public IObservableValue<T> observe(S viewer);`
what I'm doing wrong?
Sorry Everyone, I have figured it out, We can do by following:
IViewerValueProperty<TableViewer, Target> target = ViewerProperties.<TableViewer, Target>input();
IObservableValue<Target> observe = target.observe(tableViewer);
I had forgot to add generic classes to the input() method call, which would have identified the specific observer(viewer) method.
Previously, as I had not provided the generics, the compiler was unable to distinguish between the observe(viewerType) and observe(S).
Thanks,
Pal
So, I'm trying to understand Symfony forms. I'm searching the core code for "allow_delete" option to see how it works under the hood, but the only place where it can be found is in the CollectionType class and I cannot find any logic there.
Documentation states:
If set to true, then if an existing item is not contained in the
submitted data, it will be correctly absent from the final array of
items.
Where in the code exactly it influences the submitted data?
You can find the function in MergeCollectionListener.php beginning on line 91:
// Remove deleted items before adding to free keys that are to be
// replaced
if ($this->allowDelete) {
foreach ($itemsToDelete as $key) {
unset($dataToMergeInto[$key]);
}
}
$dataToMergeInto is set as $dataToMergeInto = $event->getForm()->getNormData();
which refers to a function in which is explained in FormInterface.php:
/**
* Returns the normalized data of the field.
*
* #return mixed When the field is not submitted, the default data is returned.
* When the field is submitted, the normalized submitted data is
* returned if the field is valid, null otherwise.
*/
public function getNormData();
By default the message: This value is not valid. is not sufficient. I am in the process of adding #Assert to each property in my entities for more specific validation.
I am displaying errors globally at the top of the form - not individually - in case it matters in the context of this discussion.
/**
* #ORM\Column(type="decimal", nullable=false, precision=10, scale=2)
* #Assert\Type(message="list price must be a numeric value", type="decimal")
* #Assert\NotBlank(message="list price cannot be empty")
* #Assert\GreaterThanOrEqual(message="list price must be no less than zero", value = 0)
*/
private $listPrice;
This is what I have thus far but the form still invalidates when I enter a value "D44.33"
How do I override the default "type" validator? Note the other validators like GreaterThanOrEqual works as expected - but for the life of me I cannot get the "type" to work???
The type decimal is not a valid type in Symfony, so it's probably looking for a custom decimal type and trying to transform the data and failing (which would coincide with the type of error messages you are getting).
The list of allowable types is here:
http://symfony.com/doc/current/reference/constraints/Type.html#reference-constraint-type-type
Try using this instead:
#Assert\Type(type="float", message="List price must be a numeric value")
#Jason Roman - thanks again for your help
The issue I was facing was solved by using "invalid_message" like so:
->add('listPrice', null, ['error_bubbling' => true, 'invalid_message' => '{{ value }} is not a valid list price'])
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.
:)
Does exist Zend Filter similar to Zend Validator Identical?
The case I should filter input=='test'
$el->addFilter('Identical','test');
The problem that such filter not exist.
Thanks,
Yosef
I'm not sure how this filter should work, since it is not clear from your question. Anyway, I made some custom filter that will check if a value of input filed is equal to some $token. If they are equal than the input value will be empty string.
The filter looks as follows:
// file: APPLICATION_PATH/filters/Identical.php
class My_Filter_Identical implements Zend_Filter_Interface {
/**
* Token with witch input is compared
*
* #var string
*/
protected $_token;
/**
* Set token
*
* #param string
* #return void
*/
public function __construct($token = '') {
$this->_token = $token;
}
/**
* Filtering method
*
* #param string $value value of input filed
* #return string
*/
public function filter($value) {
if ($value !== $this->_token) {
return $value;
}
return '';
}
}
To apply it to a given form element:
require_once (APPLICATION_PATH . '/filters/Identical.php');
$el1->addFilter(new My_Filter_Identical('test'));
Off course instead of require_once it could be added to your resource autoloader, but as an example I think it is not needed right now.
Edit:
Forgot to mention pregReplace filter.
The same what the custom filter above does could be done using pregReplace filter:
$el1->addFilter('pregReplace',array('/test/',''));
But, as I said, I'm not sure how you want your filter to work. If you provide more info maybe I could help more.
Your question isn't all that clear - do you want a filter which removes the word test? Or are you talking about filtering a form input? So taking your example you want to remove from the el input what the test input contains?
If you want to remove test from your input, you could use Zend_Filter_PregReplace
$filter = new Zend_Filter_PregReplace(array('match' => '/test/', 'replace' => ''));
$input = 'What is this test about!';
$filter->filter($input);
Should give you What is this about!
There isn't a filter which would filter identical form input if its been entered into another input I don't think. You could try to create your own input filter and perform your own logic on the input.
It is not that clear what you are trying to do. If you give more explanation that would be good.
I need to remove all input, soo its not good to use regex.
If you just want to clear the data in form elements you can use one of the following:
Clear an element value by setting the value of the element to nothing.
$el->setValue(null);
or reset all form elements
$form->reset();