-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Laravel validation #13
Comments
So I can add the required rule when the |
https://laravel.com/docs/5.4/upgrade#upgrade-5.4.0
|
When no rules defined:
This is my composer dependencies:
Temporary solution: comment lines 49-51 in
|
Ah, nullable doesn't exist in 5.2 we should probably add a check indeed. |
Should be fixed with latest v0.3.3 |
Since #30 got closed, I'd like to mention that I got an issue with validation using the CollectionType in my parent form. Figured out that constraints (validation by Symfony Form itself) is working well. It seems that rules (implemented by @barryvdh ) don't work on child forms... Any hint @barryvdh? I have no clue XD Regards, Bob |
Do you have a test case? |
I'll post some code tomorow. Or do you prefer unit tests? |
Nee a simple thing I can put in my own Laravel app is fine |
What about this? Simplified it but should work... Parent<?php
namespace App\Core\Http\Forms\Types;
use Illuminate\Validation\Rule;
use Laravel\Nova\Fields\Number;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class ParentFormType extends AbstractType
{
public function __construct()
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$auth = auth();
$builder
->add('sizeTo', NumberType::class, [
'required' => true,
'rules' => [
'min:00',
'max:99'
]
])
->add('weight', NumberType::class, [
'required' => false,
'rules' => [
'numeric'
]
])
->add('weightUnit', TextType::class, [
'required' => false,
'rules' => [
Rule::in(['kg', 'ha'])
]
])
->add('childs', CollectionType::class, [
'label' => false,
'entry_type' => ChildType::class,
'allow_add' => true,
])
->add('submit', SubmitType::class);
}
} Child<?php
namespace App\Core\Http\Forms\Types;
use Illuminate\Validation\Rule;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ChildType extends AbstractType
{
public function __construct()
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$auth = auth();
$builder
->add('class', TextType::class, [
'required' => true,
])
->add('size', NumberType::class, [
'required' => true,
'rules' => [
'min:00',
'max:99'
]
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => ParentData::class,
]);
}
} |
I recreated this and added a level deeper. I made some changes (see 746fae0) and now I get this rule set: It should add the validation for the children when at least 1 of the children is in the form input. You can test this with |
Great work Barry! I'll check it out Monday morning! Have a nice weekend. |
Hi there! It's me again :) last week I got some new validation issues with collections combined with Laravel's rules. I'll dig into this and I'll try to create a reproduction. |
To make it easy to validate, we can use the Laravel validator with Symfony forms:
This would run the validator post-submit, so you can check
$form->isValid()
after submitting the form. When failed, the errors on the form are added.This seems to work already with this commit: 811ca4c
Next step would be to:
required
option based on the rule, but this would change the default (Symfony default is required = true), to make front-end always in sync with back-end validationThe text was updated successfully, but these errors were encountered: