mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 13:54:23 +00:00
add a required field to custom fields type
ref Chill-project/Chill-CustomFields#17
This commit is contained in:
parent
b88bb50b56
commit
3230659a4b
@ -94,7 +94,7 @@ class CustomFieldChoice implements CustomFieldInterface
|
||||
$options = array(
|
||||
'multiple' => $customFieldOptions[self::MULTIPLE],
|
||||
'choices' => $choices,
|
||||
'required' => false,
|
||||
'required' => $customField->isRequired(),
|
||||
'label' => $this->translatableStringHelper->localize($customField->getName()));
|
||||
|
||||
//if allow_other = true
|
||||
|
@ -60,6 +60,12 @@ class CustomField
|
||||
*/
|
||||
private $ordering;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var bolean
|
||||
*/
|
||||
private $required = FALSE;
|
||||
|
||||
|
||||
const ONE_TO_ONE = 1;
|
||||
const ONE_TO_MANY = 2;
|
||||
@ -246,6 +252,34 @@ class CustomField
|
||||
$this->slug = $slug;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* alias for isRequired
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function getRequired()
|
||||
{
|
||||
return $this->isRequired();
|
||||
}
|
||||
|
||||
/**
|
||||
* return true if the field required
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isRequired()
|
||||
{
|
||||
return $this->required;
|
||||
}
|
||||
|
||||
public function setRequired($required)
|
||||
{
|
||||
$this->required = $required;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -66,6 +66,11 @@ class CustomFieldType extends AbstractType
|
||||
|
||||
$builder
|
||||
->add('ordering', 'number')
|
||||
->add('required', 'checkbox', array(
|
||||
'required' => false,
|
||||
//'expanded' => TRUE,
|
||||
'label' => 'Required field'
|
||||
))
|
||||
->add('type', 'hidden', array('data' => $options['type']))
|
||||
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event)
|
||||
{
|
||||
|
@ -20,6 +20,7 @@ use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
|
||||
use Chill\CustomFieldsBundle\Form\DataTransformer\CustomFieldDataTransformer;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldTitle;
|
||||
|
||||
class CustomFieldType extends AbstractType
|
||||
{
|
||||
@ -50,6 +51,7 @@ class CustomFieldType extends AbstractType
|
||||
$this->customFieldCompiler
|
||||
->getCustomFieldByType($cf->getType())
|
||||
->buildForm($builder, $cf);
|
||||
$builder->get($cf->getSlug())->setRequired($cf->isRequired());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,8 @@ Chill\CustomFieldsBundle\Entity\CustomField:
|
||||
type: float
|
||||
options:
|
||||
type: json_array
|
||||
required:
|
||||
type: boolean
|
||||
lifecycleCallbacks: { }
|
||||
manyToOne:
|
||||
customFieldGroup:
|
||||
|
35
Resources/migrations/Version20151210155904.php
Normal file
35
Resources/migrations/Version20151210155904.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Application\Migrations;
|
||||
|
||||
use Doctrine\DBAL\Migrations\AbstractMigration;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your needs!
|
||||
*/
|
||||
class Version20151210155904 extends AbstractMigration
|
||||
{
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function up(Schema $schema)
|
||||
{
|
||||
// this up() migration is auto-generated, please modify it to your needs
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
|
||||
|
||||
$this->addSql('ALTER TABLE customfield ADD required BOOLEAN DEFAULT FALSE');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Schema $schema
|
||||
*/
|
||||
public function down(Schema $schema)
|
||||
{
|
||||
// this down() migration is auto-generated, please modify it to your needs
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
|
||||
|
||||
$this->addSql('ALTER TABLE CustomField DROP required');
|
||||
|
||||
}
|
||||
}
|
@ -39,6 +39,9 @@ General informations: Informations générales
|
||||
Options: Options
|
||||
Custom fields group: Groupe de champ personnalisé
|
||||
Ordering: Ordre d'apparition
|
||||
Required field: Champs requis
|
||||
An answer is required: Une réponse est requise
|
||||
Any answer is required: Aucune réponse n'est requise
|
||||
Back to the group: Retour au groupe de champs personnalisé
|
||||
Slug: Identifiant textuel
|
||||
The custom field has been created: Le champ personnalisé est créé
|
||||
|
@ -29,6 +29,7 @@
|
||||
{{ form_row(edit_form.customFieldsGroup) }}
|
||||
{% endif %}
|
||||
{{ form_row(edit_form.ordering) }}
|
||||
{{ form_row(edit_form.required) }}
|
||||
{% if edit_form.options is not empty %}
|
||||
<h2>{{ 'Options'|trans }}</h2>
|
||||
{% for option in edit_form.options %}
|
||||
|
@ -30,6 +30,7 @@
|
||||
{{ form_row(form.customFieldsGroup) }}
|
||||
{% endif %}
|
||||
{{ form_row(form.ordering) }}
|
||||
{{ form_row(form.required) }}
|
||||
{% if form.options is not empty %}
|
||||
<h2>{{ 'Options'|trans }}</h2>
|
||||
{% for option in form.options %}
|
||||
|
@ -51,7 +51,10 @@ class CustomFieldsGroupControllerTest extends WebTestCase
|
||||
private function editCustomFieldsGroup(Client $client)
|
||||
{
|
||||
$crawler = $client->request('GET', '/fr/admin/customfieldsgroup/');
|
||||
$crawler = $client->click($crawler->selectLink('modifier')->link());
|
||||
$links = $crawler->selectLink('modifier');
|
||||
$crawler = $client->click($links->last()->link());
|
||||
|
||||
$this->assertEquals(200, $client->getResponse()->getStatusCode());
|
||||
|
||||
$form = $crawler->selectButton('Update')->form(array(
|
||||
'custom_fields_group[name][fr]' => 'Foo',
|
||||
|
@ -22,6 +22,8 @@ namespace Chill\CustomFieldsBundle\Tests;
|
||||
use Chill\CustomFieldsBundle\CustomFields\CustomFieldNumber;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomFieldsGroup;
|
||||
use Chill\CustomFieldsBundle\Form\CustomFieldsGroupType;
|
||||
|
||||
/**
|
||||
* Test CustomFieldsNumber
|
||||
@ -139,5 +141,51 @@ class CustomFieldsNumberTest extends \Symfony\Bundle\FrameworkBundle\Test\WebTes
|
||||
$this->assertEquals(1, count($form['default']->getErrors()));
|
||||
}
|
||||
|
||||
public function testRequiredFieldIsFalse()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber(array(
|
||||
'min' => 1000,
|
||||
'max' => null,
|
||||
'scale' => null,
|
||||
'post_text' => null
|
||||
));
|
||||
$cf->setRequired(false);
|
||||
|
||||
$cfGroup = (new \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup())
|
||||
->addCustomField($cf);
|
||||
|
||||
$form = static::$kernel->getContainer()->get('form.factory')
|
||||
->createBuilder('custom_field', array(), array(
|
||||
'group' => $cfGroup
|
||||
))
|
||||
->getForm();
|
||||
|
||||
$this->assertFalse($form['default']->isRequired(),
|
||||
"The field should not be required");
|
||||
}
|
||||
|
||||
public function testRequiredFieldIsTrue()
|
||||
{
|
||||
$cf = $this->createCustomFieldNumber(array(
|
||||
'min' => 1000,
|
||||
'max' => null,
|
||||
'scale' => null,
|
||||
'post_text' => null
|
||||
));
|
||||
$cf->setRequired(true);
|
||||
|
||||
$cfGroup = (new \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup())
|
||||
->addCustomField($cf);
|
||||
|
||||
$form = static::$kernel->getContainer()->get('form.factory')
|
||||
->createBuilder('custom_field', array(), array(
|
||||
'group' => $cfGroup
|
||||
))
|
||||
->getForm();
|
||||
|
||||
$this->assertTrue($form['default']->isRequired(),
|
||||
"The field should be required");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user