-
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
Get valid fields from the Form #196
Comments
Setting I could add that public function validFields()
{
return $this->validator->valid();
} and than use it as you put in your update example. Please test it out and let me know how it works. |
The fillable's settings are not working for us, because its using many permission levels, and in each level the attributes are different. The validator valid() command is also not working in our use because it unfortunately returning the items which are not defined in the form. I was thinking something like these, which is mostly working well for us: public function validFields()
{
$validFields = [];
$fieldParse = function ($fields) use (&$fieldParse, &$validFields) {
foreach ($fields as $field => $fieldObj) {
if ($fieldObj instanceof CollectionType) {
foreach ($fieldObj->getChildren() as $child) {
$fieldParse($child->getForm()->getFields());
}
} else {
$validFields[] = $fieldObj->getNameKey();
}
}
};
$fieldParse($this->fields);
return $validFields;
} |
How is this method determining if field is valid or not? |
Sorry, the method name is not correct. Rather use: getFormFieldNames. It just collects the fields defined in the form. Validation executes beforehand. :) My completed form class: public function getFormFieldNames()
{
$fieldsTmp = [];
$fieldParse = function ($fields) use (&$fieldParse, &$fieldsTmp) {
foreach ($fields as $field => $fieldObj) {
if ($fieldObj instanceof CollectionType) {
foreach ($fieldObj->getChildren() as $child) {
$fieldParse($child->getForm()->getFields());
}
} else {
$fieldsTmp[] = $fieldObj->getNameKey();
}
}
};
$fieldParse($this->fields);
return $fieldsTmp;
}
public function getOnlyFormFieldsFromRequest()
{
return $this->getRequest()->only($this->getFormFieldNames());
} Controller example: public function update($id)
{
...
$form = $this->form(ExampleEditForm::class);
if (!$form->isValid()) {
return redirect()->back()->withErrors($form->getErrors())->withInput();
}
$data = $form->getOnlyFormFieldsFromRequest();
$model->fill($data);
$result = $model->save();
...
} |
Just an idea ... I think we need a method that return the name of the fields in the Form. This process should only return the items we really need to process.
Example:
But I send these fields:
Then, the following code will save everything:
Example:
If it's like that, it does not:
Example:
The validFields would return with the fields specified in the form.
Do You think that's possible? Perhaps the Collection and ChildForm would be problematic.
The text was updated successfully, but these errors were encountered: