-
Notifications
You must be signed in to change notification settings - Fork 297
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
Migrated to new validation system; old tests don't pass because of empty request #149
Comments
Can you please provide code where you do the validation with form? Did you try setting the request manually with |
Hey Kristijan, thanks for the reply. Not working public function update($id, Request $request)
{
$form = $this->form(AddressForm::class);
if (!$form->isValid()) {
return $this->redirectWithErrors($form);
}
else
{
}
} Working public function update($id, Request $request)
{
$form = $this->form(AddressForm::class);
$form->setRequest($request);
if (!$form->isValid()) {
return $this->redirectWithErrors($form);
}
else
{
}
} However, sometimes I need to set the Controller: public function create(FormBuilder $formBuilder)
{
$form = $formBuilder->create(FeeForm::class, [
'method' => 'POST',
'url' => route('fee.store')
]);
return view('fee.create', compact('form'));
}
public function store(Request $request)
{
$form = $this->form(FeeForm::class);
$form->setRequest($request);
if (!$form->isValid()) {
return $this->redirectWithErrors($form);
}
else
{
[...]
}
}
public function edit($id, FormBuilder $formBuilder)
{
$form = $formBuilder->create(FeeForm::class, [
'method' => 'PUT',
'url' => route('fee.update', $id),
'model' => Fee::findOrFail($id)
]);
return view('fee.edit', compact('form'));
}
public function update($id, Request $request)
{
$fee = Fee::findOrFail($id);
$form = $this->form(FeeForm::class, [
'model' => $fee
]);
if (!$form->isValid()) {
return $this->redirectWithErrors($form);
}
else
{
[...]
}
} FeeForm: public function buildForm()
{
$id = $this->model && $this->model->id ? $this->model->id : null;
$this
->add('name', 'text', [
'required' => true,
'label' => trans('label.name'),
'rules' => 'required|max:32|unique:fees,name,' . $id,
])
->add('display_name', 'text', [
'required' => true,
'label' => trans('label.display_name'),
'rules' => 'required|max:64',
])
->add('description', 'text', [
'required' => true,
'label' => trans('label.description'),
'rules' => 'required|max:255',
])
->add('module_id', 'select', [
'choices' => Module::modules(),
'required' => true,
'label' => trans('label.module'),
'rules' => 'required|integer'
])
->add('price', 'number', [
'required' => true,
'label' => trans('label.price'),
'attr' => [
'min' => '0.00',
'step' => '0.01'
],
'rules' => 'required|numeric'
])
->add(trans('label.submit'), 'submit', [
'attr' => ['class' => 'btn btn-primary']
]
)
;
} |
Hmm, that's strange. Every time form is instantiated current instance of request is used. That is probably not handled properly in tests because Request for tests are changed in the meantime. Can you try dumping both requests in the update method and see what you get in the tests? To check if they are the same, or what's different, and so on. Something like this: public function store(Request $request)
{
$form = $this->form(FeeForm::class);
var_dump($form->getRequest());
var_dump($request);
} Also i'm not sure if this is only issue with the Jeffrey's testing library that is part of 5.1, or it also happens in plain phpunit tests. I would really appreciate if you could test these things and let me know what you get. |
I did this on the store method and this is what I got: https://www.diffnow.com/?report=hfdba (didn't know the best way to provide them both). The update method is handled the same way: https://www.diffnow.com/?report=klwn8 Let me know, if you need any more examples, code or whatever! |
It's definitely different request instance in the tests, that's why it's not working. I would suggest you stick with public function store(Request $request)
{
$form = $this->form(FeeForm::class)->setRequest($request);
if (!$form->isValid()) {
return $this->redirectWithErrors($form);
}
else
{
[...]
}
} I will probably add a bit more readable method for validating specific request, instead of setter. Will probably use Symfony's handleRequest name. Something like this: $form = $this->form(FeeForm::class);
$form->handleRequest($request);
if (!$form->isValid()) {
return $this->redirectWithErrors($form);
} I will also add this issue to the docs for other users. Thanks for reporting and for help :) |
Yes, I'll do so for now. Also I thought about adding Request to form options as optional parameter, so that you don't have to chain, but since it's mostly on store/update methods, it might only make sense with unique keys, etc. Anyway! Thanks for you great work! It really helps a lot! |
@marcoraddatz Just want to let you know that this is fixed on |
Thanks Kristijan! The correct request is now used. Btw. if you add a field and remove it later on by a condition, an exception is thrown (removed some code for easier understanding):
→
Prior to |
@marcoraddatz do you know what version u were using before updating to dev-master? I'll probably remove that exception and just ignore remove if the field does not exist. |
I like the idea and currently use 1.6.31. |
I migrated all validation rules to your new system, which I like a lot. However, my old tests won't pass, because I'm told, the required fields haven't been filled out – but they are.
Here's a small example:
AddressForm (I have two more fields and a submit button in this class, but have cut them for this example):
AddressController → update:
Test:
Test output:
You can see that the data exists. Also, if I test in the browser, everything works fine. While debugging, I found out, that
$this->getRequest()->all()
is empty (Form.php, Line 922). Do you guys have any clue, what it could be?Thanks you guys for this amazing package!
The text was updated successfully, but these errors were encountered: