Validation formulair + affichage des erreurs

This commit is contained in:
Clement 2016-03-22 15:07:42 +01:00
parent c18a861147
commit 13aadf4f4f
7 changed files with 75 additions and 17 deletions

View file

@ -2,6 +2,8 @@
namespace CoursEndingBundle\Controller;
use CoursEndingBundle\Form\Type\FormType;
use Doctrine\ORM\Query\Expr\From;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
@ -19,12 +21,13 @@ class MainController extends Controller
*/
public function indexAction(Request $request)
{
$forms = ['forms' => FormQuery::create()->find()];
$form = $this->createForm(FormsType::class, $forms);
if ($form->isValid()) {
die("ok");
//die("ok");
}
return [

View file

@ -2,6 +2,7 @@
namespace CoursEndingBundle\Controller;
use CoursEndingBundle\Model\Form;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
@ -63,7 +64,14 @@ class RestController extends FOSRestController
$form->handleRequest($request);
return View::create($form, 400);
if ($form->isValid()) {
$forms['forms']->save();
echo "Valid";
print_r($form->isValid());
die();
}
return View::create($form, 200);
}
// public function postQuestionsAction(Request $request)

View file

@ -5,6 +5,7 @@ namespace CoursEndingBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Propel\Bundle\PropelBundle\Form\BaseAbstractType;
use Symfony\Component\Validator\Constraints\NotBlank;
class FormType extends BaseAbstractType
{
@ -20,6 +21,9 @@ class FormType extends BaseAbstractType
'id',
TextType::class,
array(
'constraints' => array(
new NotBlank(),
),
)
);
@ -27,6 +31,9 @@ class FormType extends BaseAbstractType
'label',
TextType::class,
array(
'constraints' => array(
new NotBlank(),
),
)
);
@ -34,6 +41,9 @@ class FormType extends BaseAbstractType
'active',
TextType::class,
array(
// 'constraints' => array(
// new NotBlank(),
// ),
)
);
}

View file

@ -7,7 +7,7 @@
<behavior name="timestampable"/>
<behavior name="fos_rest">
<behavior name="CoursEndingBundle\Propel\Behavior\FOSRestBehavior">
<parameter name="id" value="" />
<parameter name="label" value="" />
<parameter name="active" value="" />

View file

@ -1,4 +1,6 @@
new Vue({
Vue.config.debug = true;
Vue.config.devtools = true;
var x = new Vue({
el: '#forms-render',
data: {
@ -12,21 +14,46 @@ new Vue({
methods: {
fetchForms: function() {
this.$http.get(Routing.generate('api_get_forms'), function(forms) {
for (var i = 0; i < forms.data.length; i++) {
forms.data[i].errors = {};
}
this.$set('forms', forms.data);
});
},
save: function() {
var that = this;
var data = {
forms: {
forms: this.forms
}
};
this.$http.post(
Routing.generate('api_post_forms'),
data
);
this.$http.post(Routing.generate('api_post_forms'), data)
.success(function (data) {
console.log(data);
})
.error(function (data) {
console.log(data);
if (data.hasOwnProperty("code") && data.code === 400
&& data.hasOwnProperty('message') && data.message === "Validation Failed" ) {
var k = 0;
for (var i = 0; i < data.errors.children.forms.children.length; i++) {
var form = data.errors.children.forms.children[i];
//console.log(form);
for ( var j in form ) {
var errorsInputs = form[j];
//console.log(errorsInputs);
that.forms[k].errors = errorsInputs;
//that.forms[k].label = 'toto';
k++;
}
}
console.log(that.forms);
//that.$set('forms', that.forms); //change rien avec ou sans
}
});
}
}
})

View file

@ -2,12 +2,13 @@
{% block body %}
{{form_row(form.forms)}}
{{ form_rest(form) }}
<div id="foo"></div>
{#{{ form_row(form.forms) }}#}
{##}
{#{{ form_rest(form) }}#}
<div id="foo">
<br><hr><br>
</div>
<div id="forms-render" class="collection">
<div class="collection-item" v-for="form in forms">
{% raw %}
@ -16,11 +17,20 @@
{{ form.label }}
</div>
<hr />
<div class="collection-item-content">
{{ form.active ? 'ACTVITÉ' : 'NON ACTIVÉ' }}
<input type="checkbox" v-model="form.active">
</div>
<div class="collection-item-errors">
<div v-for="errors in form.errors">
<p>{{ errors.errors }}</p>
</div>
</div>
<!--<div class="collection-item-questions">
<div v-for="questions in form.questions.data">
{{ questions | json }}
</div>
</div>-->
<hr />
{% endraw %}
</div>

View file

@ -12,7 +12,7 @@ use Symfony\Component\Debug\Debug;
// Feel free to remove this, extend it, or make something more sophisticated.
if (isset($_SERVER['HTTP_CLIENT_IP'])
|| isset($_SERVER['HTTP_X_FORWARDED_FOR'])
|| !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1')) || php_sapi_name() === 'cli-server')
|| !(in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1','192.168.33.1', 'fe80::1', '::1')) || php_sapi_name() === 'cli-server')
) {
header('HTTP/1.0 403 Forbidden');
exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');