0

I have a couple of fields in the <form>... $fieldset is array of the fields name, it loop them with isset() checking.

I want to apply validation (eg: required input, email) to a few fields, how to apply this from my code logic?

public function actionProfile($id = null) {

        $profileModel = new ProfileModel;

        // <input> fields name
        $fieldset['name'] = array('FirstName', 'LastName');
        $fieldset['address'] = array('HouseNumber', 'StreetName', 'Town', 'Location');

        $formError = array();
        if (isset($_POST['profile'])) {
            // Process input event
            foreach ($fieldset as $legend => $fields) {
                foreach ($fields as $field) {
                    if (!isset($_POST['profile'][$field])) {
                        $formError[$legend] = $field;
                    } else {
                        $form[$legend][$field] = $_POST['profile'][$field];
                    }
                }
            }

            if (count($formError) == 0) {
                if ($profileModel->saveAddress($form['address'])) {
                    //Saved to the database.
                }
            } 
        }


       // Get data from the database
       $data['profile'] = $profileModel->find($id);
       $view = new View($this->layout, $data)->render();
}

In the view file, it would look something like this:

<input type='text' value=<?php echo $profile['first_name'] name='profile[FirstName]' ?>
<input type='text' value=<?php echo $profile['last_name'] name='profile[LastName]' ?>

Edit: When editing the record via form.. If there is an error (validation) - I want to put user input value back into <input> value instead of value from the database. How can it be done from my code?

3 Answers 3

2

You are currently putting validation logic inside the controller. That should go in the Domain Business Object (read more: here and here).

Also, "model" is not a class. Model is a layer in MVC architecture. This layers mostly consists of two types of instances: Domain Objects and Data Mappers. Each with quite different responsibilities.

Sign up to request clarification or add additional context in comments.

Comments

0

I think it would be wise to split the verification code from the actual update function.

Have it run through a validator first, checking for length and required inputs. When that passes, you can send all that (formatted) data to the action. If it doesn't pass the validation, return it to the view with additional error information so you can guide the user to fix the problem.

I hope you understand what I'm trying to explain to you. :-).

Comments

0

Use the PHP Filter functions:

http://www.php.net/manual/en/ref.filter.php

Use the Variable Handlers

https://www.php.net/manual/en/ref.var.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.