Don't show column name when validator returns an error - symfony1

I have the following code:
$this->form->setValidators(array(
'email' => new sfValidatorAnd(
array(
new sfValidatorEmail(array(), array(
'invalid' => 'Enter a valid email address.',)),
new sfValidatorDoctrineUnique(array(
'model'=>'Users',
'column'=>'email',
), array(
'invalid' => 'This email is already being used.',
)),
),
array(),
array(
'required' => 'Required',
)
),
//...
and when the email is not unique, it returns the following error: email: This email is already being used. But I don't want that email: in front of the message.
Is there a way I can stop showing the column name?
Can I get some help?

This is a special of sfValidatorDoctrineUnique, so it may be necessary to test uniqueness over more than one column.
You have to override the doClean() method to prevent this line (at the end) :
throw new sfValidatorErrorSchema($this, array($columns[0] => $error));
Further informations :
sfValidatorDoctrineUnique source
http://oldforum.symfony-project.org/index.php/m/91044/

You can pass throw_global_error option to the sfValidatorDoctrineUnique, in order to throw your error and don't display the <column>::
new sfValidatorDoctrineUnique(
array('throw_global_error' => true, 'column' => 'email'),
array('invalid' => 'Custom message')
)
Hope it's helpful.
Best regard.

Related

Laravel 8 not translating validation attributes

I am trying to localise the messages in my form validation using Laravel's validate() function, as follows:
$validated = $request->validate([
'email' => 'bail|required|email',
'g-recaptcha-response' => 'required|captcha',
]);
In my lang/en.php etc files I have translations for 'required', as follows:
'required' => 'The :attribute field is required.',
And I have an entry in the attributes array, as follows:
'attributes' => [
'g-recaptcha-response' => 'Recaptcha',
],
However, the translated attribute does not get called in any of the languages.
What am I doing wrong?
This works if you change the validator to facade.
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'email' => 'bail|required|email',
'g-recaptcha-response' => 'required|captcha',
]);
And return the erros with $validator->messages()->getMessages();.

Custom elements in standard palette Contao

Iam using oneo custom elements .I need to add a field for content.Please check my code
<?php
return array(
'label' => array(
'en' => array('Feature boxe', 'Create an area with text, image/video and background image'),
'de' => array('Feature-Box', 'Erzeugt einen Bereich mit Text, Bild/Video und Hintergrundbild'),
),
'types' => array('content'),
'standardFields' => array('cssID', 'headline'),
'fields' => array(
array(
'inputType' => 'group',
'label' => array(
'en' => array('Image settings'),
'de' => array('Bildeinstellungen'),
),
),
'image' => array(
'label' => array(
'en' => array('Feature image or video', 'Choose an image or multiple video files.'),
'de' => array('Feature-Bild oder -Video', 'Wählen Sie ein Bild oder mehrere Videodateien aus.'),
),
),
);
The new field is appear in a new palette. But I need to add the new one in the same palette as cssID resides.Is it possible ???
I think you mean RockSolid Custom Elements. And no, that is not possible. Custom fields can only be added in the standard group of the custom element - or to custom groups that you define yourself.

Collection type migration from sf2.8 to sf3

Let's take into account this example: I've got a form type where I embed another type in a "collection fashion" as follows
->add('foo', CollectionType::class, [
'entry_type' => new FooType(),
'error_bubbling' => false,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'label' => ' ',
'entry_options' => [
'bar' => $options['bar'],
],
'by_reference' => false,
])
and all works fine (I can add and remove elements easily).
Since I'm upgrading the framework from 2.8 to 3.0, I've read changelog and noticed
The option type of the CollectionType has been removed in favor of the
entry_type option. The value for the entry_type option must be the
fully-qualified class name (FQCN).
So I've changed my form to
->add('foo', CollectionType::class, [
'entry_type' => FooType::class,
'error_bubbling' => false,
'allow_add' => true,
'allow_delete' => true,
'required' => false,
'label' => ' ',
'entry_options' => [
'bar' => $options['bar'],
],
'by_reference' => false,
])
but when I add elements to collection, it seems that ModelData are collapsed into the same element (the last one of the collection).
So, imagine that I post entities with ids [1,2,3], ViewData is correct whereas ModelData has an array collection with three elements: three foo(s) with id 3, so the same foo.
Does anyone understand what's going on here?
Edit
This is FooType meaningful code
$resolver->setDefaults([
'data_class' => 'Vendor\Bundle\Entity\Foo',
'empty_data' => new Foo(),
]);
It seems that this is a symfony bug (unconfirmed; read more) or a misuse of combination of FQCN in entry_type and new on empty_data of embedded form.
I'll update my answer properly when and if SF core team reply me in the issue.

Zend Framework 2 Form Element Label No Escape

I would like to know how to disable escaping on Form Checkbox Element label ?
I would like to have my label html, this is my current code :
$score = new \Zend\Form\Element\MultiCheckbox('score');
$score->setLabel('Score');
$score->setOptions(array('value_options' => array(1 => '<b>Test 1</b>', 2 => '<b>Test 2</b>')));
$score->setAttributes(array('escape' => false));
$this->add($score);
$score->setLabelOptions(array('disable_html_escape' => true));
You don't have a way to short-circuit the HTML escaper. The code on the Multicheckbox view helper does not allow for a conditional escape. You would need to create your own view Helper in order to render the HTML.
If all you're looking to do is set the labels to bold, however, you can accomplish that with CSS. The 'value_options' option can be an array of arrays where the second level contains options for each individual input in the Multicheckbox. For example:
'value_coptions' => array(
'test_1' => array(
'value' => '1',
'label' => 'Test 1',
'label_attributes' => array( 'style' => 'font-weight: bold' ),
'selected' => true,
),
),

Symfony 1.4 can't get submitted files

must be some thing simply resolving, but as a beginner, i've already spend 2 days trying to find out. I'm having a simple form ("enctype="multipart/form-data") with few inputs of text and file. I can't get what's wrong, i can get user's text data through getParameter(), but cannot get file getFiles(). i'm on ubuntu 11.04 php5.3
code:
form class:
public function configure()
{
$this->setWidgets(array(
'vendor' => new sfWidgetFormInputText(array(), array('class' => 'form-text')),
'image' => new sfWidgetFormInputFile(array(), array('class' => 'form-text')),
'city' => new sfWidgetFormInputText(),
'email' => new sfWidgetFormInputText(array('default' => 'simple#sample.org')),
'contacts' => new sfWidgetFormTextarea(),
'description' => new sfWidgetFormTextarea(),
));
$this->setValidators(array(
'vendor' => new sfValidatorString(array('required' => false)),
'image' => new sfValidatorFile(array(
'required' => false,
'path' => sfConfig::get('sf_upload_dir') . '/images',
'mime_types' => 'web_images')),
'city' => new sfValidatorString(array('required' => false)),
'email' => new sfValidatorString(array('required' => false)),
'contacts' => new sfValidatorString(array('required' => false)),
'description' => new sfValidatorString(array('required' => false)),
));
$this->widgetSchema->setNameFormat('pv[%s]');
}
action:
public function executeCreate(sfWebRequest $request)
{
$this->forward404Unless($request->isMethod(sfRequest::POST));
return $this->renderText(var_dump($request->getParameter('pv'))); //this array include vars from form
//return $this->renderText(var_dump($request->getFiles('pv'))); //empty array
}
You should provide the actual code you are trying with. You could also look further into the forms component, which has a section on file uploads with example code here (bottom of page):
http://www.symfony-project.org/forms/1_2/en/02-Form-Validation
This part of the docs is still relevant for symfony 1.4.
Does your form widget configuration look anything like the following?
$this->setWidget('attachment', new sfWidgetFormInputFileEditable(array(
'file_src' => '/upload/' . $this->getObject()->getAttachment(),
'edit_mode' => true,
'with_delete' => true,
)));
$this->setValidator('attachment', new sfValidatorFile(array(
'path' => sfConfig::get('sf_upload_dir'),
'required' => false
)));
$this->setValidator('attachment_delete', new sfValidatorPass());
Also, check if you have permissions on your upload folder:
chmod 777 web/upload/
Are you binding the files as well as the form parameters, like so?
$this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
you must add the enctype="multipart/..." attribute to your form

Resources