Jump to content

Form helper validation


CodingJungle

Recommended Posts

this one has bothered me for awhile. I have a few apps that I need to validate against other elements in the form (like check for duplicates). the problem here is, I have had to get pretty crafty cause the validate method doesn't send all the form data. It would be nice if we had access to the form data outside of \IPS\Request (especially when forms are dynamically generated).

Link to comment
Share on other sites

On 6/21/2016 at 3:54 AM, CodingJungle said:

this one has bothered me for awhile. I have a few apps that I need to validate against other elements in the form (like check for duplicates). the problem here is, I have had to get pretty crafty cause the validate method doesn't send all the form data. It would be nice if we had access to the form data outside of \IPS\Request (especially when forms are dynamically generated).

Can you give me an example of where you have had trouble, and what data you were attempting to check/access? What problems did you have referencing \IPS\Request?

Link to comment
Share on other sites

Going to use babble as an example. I recently added in command aliasing, where it takes a list of the commands i have built in. I use a stack for them to do "replacements" for. When i go to validate, I need to make sure that none of the aliases conflict with the base commands (easily done, as that is already in a method that returns an array of that data), but I also want to make sure that the aliases don't conflict with any other alias.

            $validation = function( $data ) use ( $commands, $name )
            {
                if( \is_array( $data ) and \count( $data ) )
                {
                    foreach( $commands as $command )
                    {
                        $lower = \mb_strtolower( $command );
                        $newCommands[ $lower ] = $lower;
                    }
                    
                    $request = [ ];
                    
                    foreach( $newCommands as $command )
                    {
                        $look = "command_" . $command;
                        if( $look != $name and isset( \IPS\Request::i()->{$look} ) )
                        {
                            if( \is_array( \IPS\Request::i()->{$look} ) and \count( \IPS\Request::i()->{$look} ) )
                            {
                                foreach( \IPS\Request::i()->{$look} as $key => $val )
                                {
                                    $lower = \mb_strtolower( \str_replace( [ ' ', '/' ], '', $val ) );
                                    $request[ $lower ] = $lower;
                                }
                            }
                        }
                    }
                    
                    $alias = [ ];
                    foreach( $data as $key => $val )
                    {
                        $lower = \mb_strtolower( \str_replace( [ ' ', '/' ], '', $val ) );
                        
                        if( isset( $alias[ $lower ] ) )
                        {
                            throw new \InvalidArgumentException( 'babble_validate_alias_own_command' );
                        }
                        else
                        {
                            $alias[ $lower ] = $lower;
                        }
                        
                        if( isset( $newCommands[ $lower ] ) )
                        {
                            throw new \InvalidArgumentException( 'babble_validate_alias_core_command' );
                        }
                        
                        if( isset( $request[ $lower ] ) )
                        {
                            throw new \InvalidArgumentException( 'babble_validate_alias_alias_command' );
                        }
                    }
                }
            };

This is what i currently do in this app and a few others to get the other form elements, If anything i was thinking more along the lines of efficiency vs convenience, to have the other form elements data sent to the validation method, instead of me constantly looping over the \IPS\Request::i() to build/retrieve the data. 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...