validate country creation

This commit is contained in:
Manuel Raynaud
2013-10-07 12:46:40 +02:00
parent 0f1dee978c
commit 65d23db854
12 changed files with 70 additions and 14 deletions

View File

@@ -22,11 +22,13 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Action; namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Country\CountryCreateEvent; use Thelia\Core\Event\Country\CountryCreateEvent;
use Thelia\Core\Event\Country\CountryDeleteEvent; use Thelia\Core\Event\Country\CountryDeleteEvent;
use Thelia\Core\Event\Country\CountryUpdateEvent; use Thelia\Core\Event\Country\CountryUpdateEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\Country as CountryModel;
/** /**
@@ -39,6 +41,17 @@ class Country extends BaseAction implements EventSubscriberInterface
public function create(CountryCreateEvent $event) public function create(CountryCreateEvent $event)
{ {
$country = new CountryModel();
$country
->setIsocode($event->getIsocode())
->setIsoalpha2($event->getIsoAlpha2())
->setIsoalpha3($event->getIsoAlpha3())
->setLocale($event->getLocale())
->setTitle($event->getTitle())
->save();
$event->setCountry($country);
} }

View File

@@ -116,6 +116,11 @@
<tag name="kernel.event_subscriber"/> <tag name="kernel.event_subscriber"/>
</service> </service>
<service id="thelia.action.country" class="Thelia\Action\Country">
<argument type="service" id="service_container"/>
<tag name="kernel.event_subscriber"/>
</service>
</services> </services>
</config> </config>

View File

@@ -415,7 +415,7 @@
<default key="_controller">Thelia\Controller\Admin\CountryController::createAction</default> <default key="_controller">Thelia\Controller\Admin\CountryController::createAction</default>
</route> </route>
<route id="admin.configuration.countries.update" path="/admin/configuration/countries/update/{country_id}" methods="get"> <route id="admin.configuration.countries.update" path="/admin/configuration/country/update/{country_id}">
<default key="_controller">Thelia\Controller\Admin\CountryController::updateAction</default> <default key="_controller">Thelia\Controller\Admin\CountryController::updateAction</default>
<requirement key="country_id">\d+</requirement> <requirement key="country_id">\d+</requirement>
</route> </route>

View File

@@ -93,11 +93,20 @@ class CountryController extends AbstractCrudController
/** /**
* Hydrate the update form for this object, before passing it to the update template * Hydrate the update form for this object, before passing it to the update template
* *
* @param unknown $object * @param \Thelia\Model\Country $object
*/ */
protected function hydrateObjectForm($object) protected function hydrateObjectForm($object)
{ {
// TODO: Implement hydrateObjectForm() method. $data = array(
'id' => $object->getId(),
'locale' => $object->getLocale(),
'title' => $object->getTitle(),
'isocode' => $object->getIsocode(),
'isoalpha2' => $object->getIsoalpha2(),
'isoalpha3' => $object->getIsoalpha3(),
);
return new CountryModificationForm($this->getRequest(), 'form', $data);
} }
/** /**

View File

@@ -39,11 +39,14 @@ class CountryCreationForm extends BaseForm
"for" => "title" "for" => "title"
) )
)) ))
->add("area", "text", array( ->add("locale", "text", array(
"constraints" => array( "constraints" => array(
new NotBlank() new NotBlank()
), ),
"label" => Translator::getInstance()->trans("Country area *"), "label_attr" => array("for" => "locale_create")
))
->add("area", "text", array(
"label" => Translator::getInstance()->trans("Country area"),
"label_attr" => array( "label_attr" => array(
"for" => "area" "for" => "area"
) )

View File

@@ -28,6 +28,8 @@ use Thelia\Core\Translation\Translator;
class CountryModificationForm extends CountryCreationForm class CountryModificationForm extends CountryCreationForm
{ {
use StandardDescriptionFieldsTrait;
protected function buildForm() protected function buildForm()
{ {
parent::buildForm(true); parent::buildForm(true);
@@ -35,6 +37,9 @@ class CountryModificationForm extends CountryCreationForm
$this->formBuilder $this->formBuilder
->add("id", "hidden", array("constraints" => array(new GreaterThan(array('value' => 0))))) ->add("id", "hidden", array("constraints" => array(new GreaterThan(array('value' => 0)))))
; ;
// Add standard description fields, excluding title and locale, which a re defined in parent class
$this->addStandardDescFields(array('title', 'locale'));
} }
public function getName() public function getName()

View File

@@ -1071,6 +1071,10 @@ abstract class Country implements ActiveRecordInterface
$modifiedColumns = array(); $modifiedColumns = array();
$index = 0; $index = 0;
$this->modifiedColumns[] = CountryTableMap::ID;
if (null !== $this->id) {
throw new PropelException('Cannot insert a value for auto-increment primary key (' . CountryTableMap::ID . ')');
}
// check the columns in natural order for more readable SQL queries // check the columns in natural order for more readable SQL queries
if ($this->isColumnModified(CountryTableMap::ID)) { if ($this->isColumnModified(CountryTableMap::ID)) {
@@ -1140,6 +1144,13 @@ abstract class Country implements ActiveRecordInterface
throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e); throw new PropelException(sprintf('Unable to execute INSERT statement [%s]', $sql), 0, $e);
} }
try {
$pk = $con->lastInsertId();
} catch (Exception $e) {
throw new PropelException('Unable to get autoincrement id.', 0, $e);
}
$this->setId($pk);
$this->setNew(false); $this->setNew(false);
} }
@@ -1439,7 +1450,6 @@ abstract class Country implements ActiveRecordInterface
*/ */
public function copyInto($copyObj, $deepCopy = false, $makeNew = true) public function copyInto($copyObj, $deepCopy = false, $makeNew = true)
{ {
$copyObj->setId($this->getId());
$copyObj->setAreaId($this->getAreaId()); $copyObj->setAreaId($this->getAreaId());
$copyObj->setIsocode($this->getIsocode()); $copyObj->setIsocode($this->getIsocode());
$copyObj->setIsoalpha2($this->getIsoalpha2()); $copyObj->setIsoalpha2($this->getIsoalpha2());
@@ -1475,6 +1485,7 @@ abstract class Country implements ActiveRecordInterface
if ($makeNew) { if ($makeNew) {
$copyObj->setNew(true); $copyObj->setNew(true);
$copyObj->setId(NULL); // this is a auto-increment column, so set to default value
} }
} }

View File

@@ -167,7 +167,7 @@ class CountryTableMap extends TableMap
$this->setPhpName('Country'); $this->setPhpName('Country');
$this->setClassName('\\Thelia\\Model\\Country'); $this->setClassName('\\Thelia\\Model\\Country');
$this->setPackage('Thelia.Model'); $this->setPackage('Thelia.Model');
$this->setUseIdGenerator(false); $this->setUseIdGenerator(true);
// columns // columns
$this->addPrimaryKey('ID', 'Id', 'INTEGER', true, null, null); $this->addPrimaryKey('ID', 'Id', 'INTEGER', true, null, null);
$this->addForeignKey('AREA_ID', 'AreaId', 'INTEGER', 'area', 'ID', false, null, null); $this->addForeignKey('AREA_ID', 'AreaId', 'INTEGER', 'area', 'ID', false, null, null);
@@ -466,6 +466,10 @@ class CountryTableMap extends TableMap
$criteria = $criteria->buildCriteria(); // build Criteria from Country object $criteria = $criteria->buildCriteria(); // build Criteria from Country object
} }
if ($criteria->containsKey(CountryTableMap::ID) && $criteria->keyContainsValue(CountryTableMap::ID) ) {
throw new PropelException('Cannot insert a value for auto-increment primary key ('.CountryTableMap::ID.')');
}
// Set the correct dbName // Set the correct dbName
$query = CountryQuery::create()->mergeWith($criteria); $query = CountryQuery::create()->mergeWith($criteria);

View File

@@ -93,7 +93,7 @@ DROP TABLE IF EXISTS `country`;
CREATE TABLE `country` CREATE TABLE `country`
( (
`id` INTEGER NOT NULL, `id` INTEGER NOT NULL AUTO_INCREMENT,
`area_id` INTEGER, `area_id` INTEGER,
`isocode` VARCHAR(4) NOT NULL, `isocode` VARCHAR(4) NOT NULL,
`isoalpha2` VARCHAR(2), `isoalpha2` VARCHAR(2),

View File

@@ -75,7 +75,7 @@
<behavior name="timestampable" /> <behavior name="timestampable" />
</table> </table>
<table name="country" namespace="Thelia\Model"> <table name="country" namespace="Thelia\Model">
<column name="id" primaryKey="true" required="true" type="INTEGER" /> <column autoIncrement="true" name="id" primaryKey="true" required="true" type="INTEGER" />
<column name="area_id" type="INTEGER" /> <column name="area_id" type="INTEGER" />
<column name="isocode" required="true" size="4" type="VARCHAR" /> <column name="isocode" required="true" size="4" type="VARCHAR" />
<column name="isoalpha2" size="2" type="VARCHAR" /> <column name="isoalpha2" size="2" type="VARCHAR" />

View File

@@ -71,7 +71,7 @@
<td class="actions"> <td class="actions">
<div class="btn-group"> <div class="btn-group">
{loop type="auth" name="can_change" roles="ADMIN" permissions="admin.configuration.countries.change"} {loop type="auth" name="can_change" roles="ADMIN" permissions="admin.configuration.countries.change"}
<a class="btn btn-default btn-xs country-change" title="{intl l='Change this country'}" href="{url path="/admin/configuration/countries/update/{$ID}"}"> <a class="btn btn-default btn-xs country-change" title="{intl l='Change this country'}" href="{url path="/admin/configuration/country/update/{$ID}"}">
<span class="glyphicon glyphicon-edit"></span> <span class="glyphicon glyphicon-edit"></span>
</a> </a>
{/loop} {/loop}
@@ -121,7 +121,7 @@
{form_field form=$form field='success_url'} {form_field form=$form field='success_url'}
{* on success, redirect to the edition page, _ID_ is replaced with the created object ID, see controller *} {* on success, redirect to the edition page, _ID_ is replaced with the created object ID, see controller *}
<input type="hidden" name="{$name}" value="{url path='/admin/country/update' country_id='_ID_'}" /> <input type="hidden" name="{$name}" value="{url path='/admin/configuration/country/update/_ID_'}" />
{/form_field} {/form_field}
{form_field form=$form field='title'} {form_field form=$form field='title'}
@@ -160,6 +160,12 @@
<input type="text" id="{$label_attr.for}" name="{$name}" class="form-control" value="{$value}" title="{intl l="{$label}"}" placeholder="{intl l='Alpha code 3'}"> <input type="text" id="{$label_attr.for}" name="{$name}" class="form-control" value="{$value}" title="{intl l="{$label}"}" placeholder="{intl l='Alpha code 3'}">
</div> </div>
{/form_field} {/form_field}
{loop type="lang" name="default-lang" default_only="1"}
<input type="hidden" name="edit_language_id" value="{$ID}" />
{form_field form=$form field='locale'}
<input type="hidden" name="{$name}" value="{$LOCALE}" />
{/form_field}
{/loop}
{module_include location='country_create_form'} {module_include location='country_create_form'}

View File

@@ -92,13 +92,13 @@
{form_field form=$form field='title'} {form_field form=$form field='title'}
<div class="form-group {if $error}has-error{/if}"> <div class="form-group {if $error}has-error{/if}">
<label for="{$label_attr.for}" class="control-label">{intl l="{$label}"} : </label> <label for="{$label_attr.for}" class="control-label">{intl l="{$label}"} : </label>
<input type="text" id="{$label_attr.for}" name="{$name}" class="form-control" value="{$value}" title="{intl l="{$label}"}" placeholder="{intl l='Country title'}"> <input type="text" id="{$label_attr.for}" name="{$name}" class="form-control" value="{$TITLE}" title="{intl l="{$label}"}" placeholder="{intl l='Country title'}">
</div> </div>
{/form_field} {/form_field}
{form_field form=$form field='short-description'} {form_field form=$form field='chapo'}
<div class="form-group {if $error}has-error{/if}"> <div class="form-group {if $error}has-error{/if}">
<label for="{$label_attr.for}" class="control-label">{intl l="{$label}"} : </label> <label for="{$label_attr.for}" class="control-label">{intl l="{$label}"} : </label>
<textarea id="{$label_attr.for}" name="{$name}" class="form-control" title="{intl l="{$label}"}" placeholder="{intl l='Country short description'}"></textarea> <textarea id="{$label_attr.for}" name="{$name}" class="form-control" title="{intl l=""}" placeholder="{intl l='Country short description'}"></textarea>
</div> </div>
{/form_field} {/form_field}
{form_field form=$form field='description'} {form_field form=$form field='description'}